ok guys, i can not figure out the following problem that i faced just a while ago while trying out some filing in c++.

the code is as follows:

#include<iostream>
#include<fstream>
#include<string>
using namespace std;

int main()
{


ifstream readFile;
readFile.open("Input.txt");
string sample;

readFile>>sample;
cout<<"Here is a line: "<<sample<<endl;



ofstream outputfile;
outputfile.open("file.txt");

string word;
string word1;



cin>>word1;

outputfile<<word1;

ifstream inputfile;
inputfile.open("file.txt");




inputfile>>word;


cout<<endl<<word;

inputfile.close();

return 0;
}

when i take input from the user and then output that into a file it works, but when i take input from the file immediately from the file i cant do it. it just doesnt work. but when i take input from the file without asking for the input and sending the output to the file, the input from the file works and the value is saved in the variable.

howevere, this problem is happening only when i get input and give output to and from the same file. if get input from a different file it works.

is this a problem in c++ itself or am i just wasting my time trying to figure out stuff which i should not?
__________________

Recommended Answers

All 4 Replies

Try closing outputfile before attempting to open it as an input file.

thanx it worked.

where is the "file.txt" file is..?? i want to ask what we will write in that file...???

commented: It fell down the back of the sofa, did you check there for your missing file? -4

where is the "file.txt" file is..?? i want to ask what we will write in that file...???

What the hell are you talking about? Why did you reply to this 1.5 year old thread?

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.