Thread: using fstream

    #1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2002
    Location
    Ireland
    Posts
    26
    Rep Power
    0

    using fstream


    I am using fstream to process txt files.
    I use a loop that looks like
    while(inputFile)
    {
    processline(inputFile);
    }

    It all works fine except that the loop does not end at the end of the file. It decides to go around again, and then my program crashes.

    when i put in these lines into the loop:
    {
    char c;
    c = inputFile.peek();
    cout << "\n" << c;
    /* As well as: */ processline(inputFile);
    }

    it will print out the symbol y(with an umlaut over it) as the character it has peeked when there should be no character to peek.

    I have also used (!inputFile.eof()) as the guard, but it has the same results.

    Does anyone know why my loop doesnt stop at what I would think is the end of the file?
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Oct 2000
    Location
    Back in the real world.
    Posts
    5,966
    Rep Power
    191
    !inputFile.eof() is the right one.
    if you test "inputFile", it will run as long as opening the file didnīt fail and read over the end.

    but without more of your code, i canīt tell why it does not work for you. your code snippet does not read from the stream. so it would loop forever...
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2002
    Location
    Blacksburg VA/Philly PA
    Posts
    38
    Rep Power
    13
    when reading to the end of a file you need something like this

    int main()
    {
    ifstream infile;
    infile.open("whatever.txt.");

    infile >> (random variable);

    while(infile)
    {
    do some stuff
    infile >> (random variable again);
    }
    }
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2003
    Posts
    37
    Rep Power
    12
    I kinda of have an idea of what I think your trying to do. I am assuming your text file has multiple records or line. but first things first.

    when writing a record to a text file you have to convert numeric to stirng using atoi or atof etc function but back to your probelm here is a sample code I have that might help.

    this a loop that reads a record from a text file and writes it to a report file:

    while(ins.ReadOneRecord(DataFile))
    /*ReadOneRecord is a member function*/
    {
    ins.WriteReortDetailLine(ReportFile);
    }

    The following code is what I have in a header file but can be used where ever you define your functiont to write/read your txt file

    bool Insure:: ReadOneRecord(ifstream& DataFile)
    {
    //Declare Variables
    char tempTotalSales [7];
    char temp TotalCommission [6];
    char eol;

    //These are tempory character arrays that will be used to convert numeric into string the eol tell when it has reached the end of a record and move to the next one the array size is the field size plus 1

    //Read One Record
    DataFile.getline(employeeNum,5)
    if(DataFile.eof()) return false;
    //Then continue to add the rest of your variables below using the same syntex as the one for employeeNum also remember the second argument is the field size.

    DataFile.getline(tempTotalSales,7);
    DataFile.getline(tempTotalCommision,6);
    DataFile.get(eol);
    //You can not use .getline for the eol (or end of your record). also when you are reading a record you must read them in the order they appear or it could cause problems, so code in in the order they are.

    Now if you need to convert the temp arrays here is the code to do it:

    totalSales=atoi(tempTotalSales);

    return true;
    }

    Now this probably more code then you ask, but your post was very vague so I inlcuded the converting numbers to string in case you run into that probelm but the key thing to tell if your loop when you reached the end of a file is the if statement and the fact that the function datatype is bool meaning it will return a true or false value and if it is false it will return false and exit the loop signifining it is the end of file. hope this helps any
  8. #5
  9. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2002
    Location
    Ireland
    Posts
    26
    Rep Power
    0
    to explain further...
    I use a loop that looks like
    while(!inputFile.eof())
    {
    processline(inputFile);
    }

    where the processline is a member function:
    void process_line(ifstream& inptFile)

    and inside the funtion process_line uses the fstream in the loop:

    while ((inptFile >> strng) && (strng!=":"))
    {
    title = title + " " + strng;
    }


    and the call:

    getline(inptFile, tgpth, '\n');


    the whole thing works if instead of a while loop , i just put in a for loop for the amount of lines i need to process. This soloution is obviously a very temporary one, as the program will not know how many lines it needs to process.
    As far as i can see the while loop should work, but it doesnt. Its almost like it loops once more before it stops. but then again maybe it would never stop, i dont know.
    Is it something to do with the way I pass the stream a parameter to another method(i.e. the process line one).

IMN logo majestic logo threadwatch logo seochat tools logo