Solved

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

Posted on 2004-10-06
4
241 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 500 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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

Suggested Solutions

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…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

726 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