#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

Recommended Answers

All 2 Replies

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

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.