read from file problems

This is a discussion on read from file problems within the C++ Programming forums, part of the General Programming Boards category; hello im trying to read data from a file the file appears like Code: 1 Dog Tim Male 4 4.7 ...

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    91

    read from file problems

    hello
    im trying to read data from a file
    the file appears like
    Code:
    1
    Dog
    Tim
    Male 4 4.7 5
    0 0 0 0 0
    3
    Cat
    Missy
    Female 3 4.0 2
    1 0 0 0 0
    how can i read these in ?
    Code:
    ifstream c(file.c_str());
    c.getline(id,4);
    c.getline(type,50);
    c.getline(name,50);
    c >> gender;
    c >> age;
    c >> weight;
    c >> total_visits;
    while( c >>points[x] )
       x++;
    what happens is it reads in the Dog entry fine, but when it gets to the Cat entry, it doesnt seem to be reading in the Cat ID which is 3..
    ?

  2. #2
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    University of Waterloo
    Posts
    1,904
    I believe your problem is here:
    Code:
    while( c >>points[x] )
       x++;
    You keep reading in the points until nothing is read in, when what you should be doing is reading in only <total_visits> number of points (I'm assuming total_visits is the number of points directly below).

    So just change your code to match that methodology and see if that fixes it.

  3. #3
    Registered User
    Join Date
    Jul 2004
    Posts
    91
    mmm?
    that while loop is trying to read in these 5 numbers
    1 0 0 0 0

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    if those 5 numbers are always five numbers then you can do this:
    Code:
    x = 0;
    while(x < 5)
      cin >> points[x++];
     
    because you know the number of points to read in.  As jverkoey said 
     
    while(cin >> points[x])
      x++;
    will read in all data until end of file or an error occurs. You only want to read in 5 data points, not the rest of the file, potentially. In your case, when the program has read the five ints, it will attempt to read the three, which it does successfully, but now it tries to put the 3 in points[], and depending on the size of points[] it may overwrite the end of the array, and it may crash the program. If the program doesn't crash there, it will then try to read the C in cat, which isn't an int, and that may well cause the program to crash, if points is an array of ints as opposed to an array of char.

    In addition, you have the classic problem that happens when mixing >> and getline() in the same program. It turns out that data doesn't go directly from the keyboard/file to variables. It goes to an intermediate repository affectionately known as the input buffer, where it waits for the input method/operator to retrieve it. The >> operator will leave the terminating char in the input buffer, and ignore it when it goes back next time for another piece of data. getline() removes the terminating char from the input buffer. The default terminating char for getline() is the newline char. In the Dog data, getline() comes before >>. However, after the 5 numbers at the end of Dog are read in by >>, the terminating char for the 5th number is probably a new line char, which will be left in the input buffer. You then go back to the top of a loop, I'm sure, and call getline() to start reading data for Cat. However, the first thing getline() sees is a newline char, so it doesn't put anything in id. It removes the newline char, and then reads in the next line, which is Cat, and everything is okay again, except nothing got put into id.

    To fix this problem, it's not a bad idea to always clear the input buffer before any call to getline(). There are a number of ways to do that. They all involve ignore() method. A reasonably effective way is like this:

    cin.ignore(256, '\n');

    again the second parameter, which is the terminating char, defaults to newline so you could do this if that's what you want.

    cin.ignore(256);

    There are more sophisticated ways to do the same thing, but this syntax works the vast majority of the time.

  5. #5
    Registered User
    Join Date
    Jul 2004
    Posts
    91
    thanks elad!

  6. #6
    Registered User
    Join Date
    Jul 2004
    Posts
    91
    i've tried using the cin.ignore
    but i get this error
    Code:
     error: `ignore' undeclared (first use this function)
    ??
    i've included the header file <iostream>
    do i need to include any otheres?

  7. #7
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    Errr... posting the code would help.

    Did you type cin.ignore, or just ignore?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 01:44 PM
  2. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 05:14 AM
  3. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 02:19 PM
  4. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 09:39 AM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 11:12 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21