S
sherifffruitfly
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;
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.name,STRSIZE);
//This works for getting multiword input from cin,
//why not for my inputFile object?
inputFile >> myDonors.name;
inputFile >> myDonors.amount;
inputFile >> myDonors.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.name << endl;
cout << "Amount: " << myDonors.amount << endl;
cout << "Location: " << myDonors.location << endl << endl;
}
//Close file when done with it.
inputFile.close();
//Free allocated space when done with it.
delete [] myDonors;
return 0;
}
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;
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.name,STRSIZE);
//This works for getting multiword input from cin,
//why not for my inputFile object?
inputFile >> myDonors.name;
inputFile >> myDonors.amount;
inputFile >> myDonors.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.name << endl;
cout << "Amount: " << myDonors.amount << endl;
cout << "Location: " << myDonors.location << endl << endl;
}
//Close file when done with it.
inputFile.close();
//Free allocated space when done with it.
delete [] myDonors;
return 0;
}