0
#include <iostream>
#include <fstream>

using namespace std;

int main()
{
    char ch;
    ofstream outf("ITEM.txt");
    
    cout << "Enter item: ";
    char name[30];
    cin >> name;
    
    outf << name << "\n";
    
    cout << "Enter item cost: ";
    float cost;
    cin >> cost;
    outf << cost;
    cout << "written to file item.txt\n";
    outf.close();
    

    
    ifstream inf("ITEM.txt");
    cout << "current location of pointer: " << inf.tellg() << endl;
    inf.seekg(0);
    while(!inf.eof())
    {
        inf.get(ch);
        cout << ch;
    }
    cout << "\nreads from file item.txt";
    inf.close();
    cin.ignore(numeric_limits< streamsize >::max(),'\n');
    cin.get();
    return 0;
}

can you tell me why do i get a wrong output when I try to read the file created
ouput is like :

Enter item: soap
Enter item cost: 25
written to file item.txt
soap
255 //I am getting an extra 5 here.why?
reads from file item.txt

Edited by munitjsr2: n/a

2
Contributors
2
Replies
6
Views
5 Years
Discussion Span
Last Post by munitjsr2
0

Because you are using .eof to control your loop. See this. feof() is exactly the same.

:cool:
hmm.. i get it so its reading the EOF and goining inside the loop.
nice link walt,I am going to read that now
thanks

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.