On 24 Apr 2006 12:35:48 -0700,
sherifffruitfly@gmail.com wrote:
[color=blue]
>Hi,
>
>I've a got a little (exercise) program that reads data from a file and
>puts it into struct members. I run into trouble when one of the data
>pieces is comprised of several words (eg "john doe", with a space in
>it).
>
>For console input, cin.getline(var, howMuchIWant) or cin.get() has done
>the trick for me in the past. It doesn't seem to work for me nearly so
>well with a file stream. I wouldn't have thought cpp regarded
>file/console streams as significantly different, so I assume I'm doing
>something wrong. What am I doing wrong?
>
>thanks for letting me in on the joke! :)
>
>cdj
>
>======example data txt file========
>4
>john
>2000
>seattle
>peter
>4000
>san francisco
>paul
>100
>greenlake
>mary
>10000
>seattle
>======works fine without the "san" part though======
>
>====== code ======
>/*
>declare struct type
>open file
>get # of structs required from file
>dynamically make the array of structs
>display them
>*/
>
>//This routine works fine when the name and location are one word long
>only.
>//Need to figure out how to utilize inputFile.get or inputFile.getline
>to
>//read multiword names, locations, and the like.
>
>#include <iostream>
>#include <fstream>
>#include <cstdlib>
>
>using namespace std;
>
>const int STRSIZE = 60;
>const int FILESIZEMAX = 1000;
>
>int testfunction(int arg);
>
>struct donors {
> char name[STRSIZE];
> double amount;
> char location[STRSIZE];
>};
>
>int main()
>{
> char filename[STRSIZE];
> ifstream inputFile;
>
> cout << "File name: ";
> cin.getline(filename,STRSIZE);
>
> inputFile.open(filename);
>
> if (!inputFile.is_open())
> {
> cout << "Couldn't open " << filename << endl;
> cout << "Terminating execution\n";
> exit(EXIT_FAILURE);
> }
>
> cout << filename << " successfully opened." << endl;
>
> int numDonors;
> inputFile >> numDonors;[/color]
This leaves the newline character. use inputFile.get() after this call
to extract and throw away the newline character.
[color=blue]
>
> cout << "Number of donors in " << filename << ": " << numDonors <<
>endl << endl;
>
> if (numDonors==0)
> {
> cout << "Exiting from \'no donors\' door.\n\n";
> exit(EXIT_FAILURE);//Apparently no donor data to read
> }
>
> donors * myDonors = new donors[numDonors];
> //Now I've allocated structured space for my data
> cout << "Donors struct array created with " << numDonors << "
>elements." << endl << endl;
>
> //cout << "Name: ";
> //cin.getline(myDonors[0].name,STRSIZE);
> //cout << "You entered: " << myDonors[0].name << endl; - works fine
>for console
>
> for (int i=0; i<numDonors; i++)
> {
> cout << "Reading record " << i+1 << "... ";
>
> //inputFile.getline(myDonors[i].name,STRSIZE);
> //This works for getting multiword input from cin,
> //why not for my inputFile object?
>[/color]
After putting the inputFile.get() after the extraction operator the
above getline should work.
[color=blue]
> inputFile >> myDonors[i].name;
> inputFile >> myDonors[i].amount;[/color]
This wil also leave the newline character. use inputFile.get() after
this call to extract and throw away the newline character.
and then use the getline() function to get the location.
[color=blue]
> inputFile >> myDonors[i].location;
> //These work fine for single word items
> //Doesn't work for multiword items
>
> cout << "done!" << endl;
> }
>
>
> cout << endl;
>
> for (int i=0; i<numDonors; i++)
> {
> cout << "Record #" << i+1 << ":" << endl;
> cout << "Name: " << myDonors[i].name << endl;
> cout << "Amount: " << myDonors[i].amount << endl;
> cout << "Location: " << myDonors[i].location << endl << endl;
> }
>
> //Close file when done with it.
> inputFile.close();
>
> //Free allocated space when done with it.
> delete [] myDonors;
>
>
> return 0;
>}[/color]
The extraction operator ( >> ) does not remove the newline character
from the stream so the next call to getline() will only get the
newline character. To remove the newline character use inputFile.get()
after using the extraction operator (inputFile >> ).