<<edit: split from here>>

Hi. My i ask something?,,can you help me please to solve this Problem?
How Can i call Full Record using strtok and strcmp in text file

Please!!!!

101, Carlito Caballero, 500.00
102, tina gamboa, 560.34
103, angel caballero,750.55
104, kyla shane, 850.32

Here's the code

#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <string>

using namespace std;

int main()

{

string IDNum;
string fname;
string lname;
string cost;



ifstream record;

record.open("myRecord.txt");


if (!record)


{
while (!record.eof() )
{

for(int i = 5; !record.eof(); i++)
{
    cout<<"Enter ID Number: ";
    cin>>IDNum;
getline(record, IDNum, ' ' );
cout<<"IDNum: "<<IDNum<<endl;

getline(record, fname, ' ');
cout<<"fname: "<<fname<<endl;

getline(record, lname, ' ');
cout<<"lname: "<<lname<<endl;

getline(record, cost, '\n');
cout<<"cost: "<<cost<<endl;

cout<<"---------------------------------"<<endl;

}
}
}

record.close();
}

All i want is to get only one record in my text file

Hi,

If I have understood your question correctly you wish to modify your code to use strtok and strcmp.
I can see why you need strtok but I don't fully understand what you want strcmp for? are you looking to check for the strings '101' '102' etc etc or are you looking for a particular name in the file?

I will assume that you are looking for the ID value 103 for this example:

char* buffer;
//copy contents of the file to buffer
//...
//once copied use strtok to split the sections using , as the delim
char* section = strtok(buffer,',');
while (section != NULL){
   if (strcmp(section,'103') == 0){ //strcmp returns 0 if values are identical
      //person found
      IDNum = atoi(section);
      fName = strtok(NULL,' ,'); //include a space in the delim here
      lname = strtok(NULL,' ,');
      cost = atof(strtok(NULL.',');
   }
   //Use NULL as the input to continue with previous input
   section = strtok(NULL,',');
}

This code is untested so sorry if I have made any mistakes

Regards,
Andy

Can you give me how to access the text file because there is a runtime error when i use ifstream. it says that it has incomplete type.thanks.

No worries

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

int main () {
  FILE * pFile;
  long lSize;
  char * buffer;
  int IDNum;
  string fname;
  string lname;
  float cost;

  pFile = fopen ( "myfile.txt" , "r" );

  // obtain file size:
  fseek (pFile , 0 , SEEK_END);
  lSize = ftell (pFile);
  rewind (pFile);

  // allocate memory to contain the whole file:
  buffer = (char*) malloc (sizeof(char)*lSize);

  // copy the file into the buffer:
  fread (buffer,1,lSize,pFile);

  //once copied use strtok to split the sections using , as the delim
  char* section = strtok(buffer,',');
  while (section != NULL){
     if (strcmp(section,'103') == 0){ //strcmp returns 0 if values are identical
        //person found
        IDNum = atoi(section);
        fName = strtok(NULL,' ,'); //include a space in the delim here
        lname = strtok(NULL,' ,');
        cost = atof(strtok(NULL.','));
     }
     //Use NULL as the input to continue with previous input
     section = strtok(NULL,',');
  }

  // terminate
  fclose (pFile);
  free (buffer);
  return 0;
}

Hope this helps :D

Colezy

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.