?
Solved

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

Posted on 2004-10-06
4
Medium Priority
?
263 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
  • 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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying 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

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
Suggested Courses

850 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