954,504 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

file i/o

#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

munitjsr2
Light Poster
27 posts since Apr 2011
Reputation Points: 9
Solved Threads: 0
 

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

WaltP
Posting Sage w/ dash of thyme
Moderator
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 
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

munitjsr2
Light Poster
27 posts since Apr 2011
Reputation Points: 9
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: