Solved

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

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
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 be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

744 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now