reading a text file into a structure

I have a seemingly basic task that is eluding me. I need to read a text file into a structure in the format <company name>,<address>, <city,state,zip>. I also have some parts information that needs to go into a separate structure in the format <# of items on invoice>, <item name>,<item code #>, <item price>, <item quantity>. I can't find an example in my textbook (How to Program C++, 2nd ed., Deitel & Deitel). All the information is in a single text file in the order given. My compiler is MSVC++ 5.0. Thanks for any help I can get!!  
Who is Participating?
piano_boxerConnect With a Mentor Commented:
If every field (<company name>, <address>....) allways is separated by a comma, then you could read a single line of text into a local buffer, and extract the fields (tokens) one by one.

char* pToken;
char buf[1024];
FILE* pFile;

pFile = fopen("file.txt", "r");
    while(fgets(buf, sizeof(buf), pFile))
        pToken = strtok(buf, ",");
            TRACE("Token found = %s\n", pToken);
            pToken = strtok(NULL, ",");

What is the format of the text file?  What delimits the differnet fields?  That is how can you tell the address from the company name?  What is the format of the structures  How long ar these strings or can they be any length?
I have a sample app for reading comma separated values from a file and displaying etc.

The key routine is

CString GetField(CString& ref, int nIndex, TCHAR ch = _T(','))
  CString strReturn;
  LPCTSTR pstrStart = ref.LockBuffer();
  LPCTSTR pstrBuffer = pstrStart;
  int nCurrent = 0;
  int nStart = 0;
  int nEnd = 0;
  int nOldStart = 0;
  while (nCurrent <= nIndex && *pstrBuffer != _T('\0'))
    if (*pstrBuffer == ch)
      nOldStart = nStart;
      nStart = nEnd+1;
  if (*pstrBuffer == _T('\0'))
    TRACE("Warning: Couldn't find it.\n");
    return strReturn;
  return ref.Mid(nOldStart, nEnd-nOldStart-1);

this extract a given field (by field number) from the string.

For points I could send you the full sample code :-)
Take a look at and and see if this is what you are loong for.
Hello I'm new here, I having problem reading a text file. it looks like this:
Peter,sydney australia,1223
I know how to open the file and read the lines, but how can I separate the values that are text delimited and put them into an array?
//My struc look like this
struct Sighting{
      string firstName;
      string city;
      int postcode;

//this is the body"textfile.txt");
  if (
        cout<<"File Open Error";
  int i=0;

  for (int j=0; j<i-1; j++)
void getFileEntry(ifstream &infile, Sighting &anEntry)
void displayEntry(Sighting anEntry)
cout << setw(20) << anEntry.firstName << setw(20) << anEntry.surName << " ";
cout << setw(4) << setfill('0') << anEntry.extension << setfill(' ') << endl;

// function to sort an array of Phone Entries into descending order
void selectionSortStruct(Sighting entryArray[], int length){
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.