?
Solved

I/O reading a text file and assigning to an array C++

Posted on 2004-10-06
4
Medium Priority
?
252 Views
Last Modified: 2010-04-01
Hello: I'm new here, I having problem reading a text file. it looks like this:(separated by commas)
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 struct look like this
struct Sighting{
     string firstName;
     string city;
     int postcode;
};

//this is the body
  file1.open("textfile.txt");
  if (file1.fail())
  {
       cout<<"File Open Error";
       exit(-1);
  }
  int i=0;
  while(!file1.eof())
  {
       getFileEntry(file1,p1[i]);
       i++;
  }

  for (int j=0; j<i-1; j++)
  {
         displayEntry(p1[j]);
  }
file1.close();
 }
void getFileEntry(ifstream &infile, Sighting &anEntry)
{
     infile>>anEntry.firstName;
     infile>>anEntry.surName;
     infile>>anEntry.extension;
}
//display
void displayEntry(Sighting anEntry)
{
cout << setw(20) << anEntry.firstName << setw(20) << anEntry.surName << " ";
cout << setw(4) << setfill('0') << anEntry.extension << setfill(' ') << endl;
}

0
Comment
Question by:manuel_99
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 9

Expert Comment

by:jhshukla
ID: 12245245
use cin.getline(). this is from MSVC6 Documentation.
================================
istream::getline

istream& getline( char* pch, int nCount, char delim = '\n' );
istream& getline( unsigned char* puch, int nCount, char delim = '\n' );
istream& getline( signed char* psch, int nCount, char delim = '\n' );

Parameters
    pch, puch, psch
    A pointer to a character array.

    nCount
    The maximum number of characters to store, including the terminating NULL.

    delim
    The delimiter character (defaults to newline).

Remarks

Extracts characters from the stream until either the delimiter delim is found, the limit nCount–1 is reached, or end of file is reached. The characters are stored in the specified array followed by a null terminator. If the delimiter is found, it is extracted but not stored.

The get function, in contrast, neither extracts nor stores the delimiter.
================================

If you want to keep the comma in the stream, use get() instead with same syntax. Note that you will need to use either C-style strings or do a little trick here. the trick is to resize the stl string to required length and passing the address of string[0]. I am not sure if the trick would really work. The third option is to read the whole line into a C-string and use strtok.
http://www.cplusplus.com/ref/cstring/strtok.html

Jaydutt
0
 
LVL 30

Expert Comment

by:Axter
ID: 12245847
I recommend you use the global std::getline function instead of the iostream::getline function.

The std::getline function can take an std::string.

If you want to read a value delimated by a comma, then use the following method:

void getFileEntry(ifstream &infile, Sighting &anEntry)
{
    std::getline(infile, anEntry.firstName, ',');
    std::getline(infile, anEntry.surName, ',');
    infile >> anEntry.extension;
}
   
   
   
   
0
 

Author Comment

by:manuel_99
ID: 12246191
Thanks Axter  it works perfectly... I am new in c++ but I having this little problem.
in the text that I have to read I have date format on it like this
12/03/1997,Peter,Ramirez Smith,2121.
My problem is that the date is a diferent struct so I have two struct

struct Date{
    int day;
    int month;
    int year;
};
struct Sighting{
   Date aDate;   //I Have added this variable
     string firstName;
     string city;
     int postcode;
};
What I dis was to include a variable Date aDate inside of struct Sighting but I am confuse in how to get the values of date into this function.
void getFileEntry(ifstream &infile, Sighting &anEntry)
{
    std::getline(infile, anEntry.firstName, ',');
    std::getline(infile, anEntry.surName, ',');
    infile >> anEntry.extension;
}
Thanks very much....
How can I read the text


0
 
LVL 30

Accepted Solution

by:
Axter earned 2000 total points
ID: 12251988

12/03/1997,Peter,Ramirez Smith,2121
Example code to read above line:

void getFileEntry(ifstream &infile, Sighting &anEntry)
{
   std::string TmpVar;

   std::getline(infile, TmpVar, '/');
   anEntry.aDate.month = atoi(TmpVar.c_str());

   std::getline(infile, TmpVar, '/');
   anEntry.aDate.day = atoi(TmpVar.c_str());

   std::getline(infile, TmpVar, ',');
   anEntry.aDate.year = atoi(TmpVar.c_str());

   std::getline(infile, anEntry.firstName, ',');
   std::getline(infile, anEntry.surName, ',');
   infile >> anEntry.extension;
}
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question