Solved

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

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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
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 how to clear a vector as well as how to detect empty vectors in C++.

777 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