Click to See Complete Forum and Search --> : reading files in using ifstream
kuhns_m
February 8th, 2003, 11:40 PM
I am trying to read a file in using ifstream.
The file is comprized of three fields
Name
Age
Address
the file looks like this:
April,Joe
21
126 Freelane, Yourtown, State 88990
Matthews,Jocob
19
3930 4th Blvd, Yourcity, NJ 88710
Garfield,Kitty
24
36 Jon Havey Court, Middle, MO 66222
Lake,Bill
male
21 Ritts, Commack, NY 13231
I read it in like so:
while(ifile.getline(strLine, MAXLINE, '\n'))
{
if(strLine[0] != NULL){
counter++;
if(counter == 1){
strcpy(temp_person.strName, strLine);
} else if (counter == 2){
strcpy(buf, strLine);
} else if (counter == 3){
strcpy(temp_person.strAddress, strLine);
counter = 0;
}
}
}
On the last record I get an GPF (access violation). I remember that you are supposed to read a line in first to get the EOF to work. Does any one remember this.
Thanks in advance
PaulWendt
February 9th, 2003, 01:10 AM
From looking at what you posted, my hunch is that you're putting
10 pounds of ##$@! in a 5 pound bag. In other words, I'm
guessing that the char array you're putting stuff into is smaller
than the amount you're putting into it.
Speaking of which, is there any reason you're not using
std::string for holding your character data? If possible, use the
global version of getline to read your line data. It lets you read
into a std::string; thus, you don't have to worry about the size
of your input and you won't overstep any boundaries. Also, make
your temp_person structure a class; give it member accessor
functions for its data members, which should be std::string's
and not char*'s. If you'll be doing any copying, don't forget to
define a copy constructor and/or operator=.
These are just suggestions; if you want advanced debugging
help with your current code, you'll have to post a little bit more of
it. It's best to offer a main() with as few functions as possible so
that people can download your code and run it. To post code on
here, use [ c o d e ] and [ / c o d e ]; put your code inside those
blocks, but don't have the superfluous amounts of spaces.
--Paul
kuhns_m
February 9th, 2003, 08:36 AM
int main()
{
ifstream ifile;
Person temp_person;
//Person *person_list;
Person *head = NULL;
char strLine[51];
char buf[32];
//char endline;
OpenFile(&ifile);
int counter = 1;
ifile.getline(strLine, MAXLINE, '\n');
strcpy(temp_person.strName, strLine);
while(ifile.getline(strLine, MAXLINE, '\n'))
{
if(strLine[0] != NULL){
counter++;
if(counter == 1){
strcpy(temp_person.strName, strLine);
} else if (counter == 2){
strcpy(buf, strLine);
} else if (counter == 3){
strcpy(temp_person.strAddress, strLine);
counter = 0;
//Insert_Rear(head, temp_person);
// print_list(head);
}
}
}
return 0;
}
Here is my main() I don't think I'm going to write a class. All I am say is that I get an access violation getting a record.
I attached the text file
Thanks again
Andreas Masur
February 9th, 2003, 10:06 AM
Well...the code is missing the definition of 'Person' which I assume is a struct/class. Could you post it as well please...??
PaulWendt
February 9th, 2003, 12:26 PM
One thing that I didn't mention is that you should also define any
external data types of functions; we also don't know what
OpenFile is doing ... though I suspect that it's just opening the
fstream object you pass in. Also, I can only make assumptions
regarding your Person struct definition. Maybe your Person
struct only contains char*'s instead of actual char arrays?
--Paul
Andreas Masur
February 9th, 2003, 04:58 PM
Originally posted by PaulWendt
Also, I can only make assumptions
regarding your Person struct definition. Maybe your Person
struct only contains char*'s instead of actual char arrays?
--Paul
Nice to see that we are thinking the same way.... :cool:
That is basically one reason why I would like to see the 'Person' structure/class...the other one would be to see whether the character arrays (if they are arrays) are big enough... :cool:
kuhns_m
February 9th, 2003, 06:49 PM
I ended up reading the file in as a buffer and parsing through that way. I really don't like fstream functions very much.
Thanks
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.