CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Sep 2001
    Location
    Michigan
    Posts
    41

    reading files in using ifstream

    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

  2. #2
    Join Date
    May 2000
    Location
    Phoenix, AZ [USA]
    Posts
    1,347
    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

  3. #3
    Join Date
    Sep 2001
    Location
    Michigan
    Posts
    41
    Code:
    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
    Attached Files Attached Files
    • File Type: txt a2.txt (218 Bytes, 84 views)

  4. #4
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652
    Well...the code is missing the definition of 'Person' which I assume is a struct/class. Could you post it as well please...??

  5. #5
    Join Date
    May 2000
    Location
    Phoenix, AZ [USA]
    Posts
    1,347
    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

  6. #6
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652
    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....

    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...

  7. #7
    Join Date
    Sep 2001
    Location
    Michigan
    Posts
    41

    What I did

    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured