Mar 31, 2011 at 3:07am UTC
how to open a .txt file?????
Am i doing anything wrong on this function?????
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
int openFiles(ifstream& inFile, ofstream& outFile)
{
inFile.open("example.txt" );
if (!inFile)
{
cout << "Unable to open the file." <<endl;
system("PAUSE" );
return 1;
}
outFile.open("exampleOut.txt" );
outFile << fixed << showpoint << setprecision(2);
}
the way it is it's not opening the file. I do not know what i did wrong???
Anyone with some advice or hints please....
Thanks in advance everyone
Last edited on Mar 31, 2011 at 3:27am UTC
Mar 31, 2011 at 3:36am UTC
Well I don't know why you would pass in references to fstreams... and have your file names hard coded... is there an "example.txt" where you think there is? What happens, does the compiler yell at you?
Mar 31, 2011 at 3:40am UTC
yes there is a file with that name and is inside my procject that i created.
Also im not sure if this helps but i do get "exampleOut.txt" file but it is blank.
Last edited on Mar 31, 2011 at 3:42am UTC
Mar 31, 2011 at 3:42am UTC
I would expect the output.txt to come out, is the other file in the same place?
Mar 31, 2011 at 3:43am UTC
yes the input file is in the same place (folder)
Mar 31, 2011 at 3:46am UTC
Haha alright =)
when you say it doesn't open it, is your message popping up, or what?
Mar 31, 2011 at 3:46am UTC
outFile << fixed << showpoint << setprecision(2);
What do you expect that line to write to outFile? Those are just manipulators, there's no data.
Mar 31, 2011 at 3:46am UTC
yes my message is showing.
Last edited on Mar 31, 2011 at 3:49am UTC
Mar 31, 2011 at 3:48am UTC
filipe, thanks for the reply but that is just some part of the code.
Sory about that
Mar 31, 2011 at 3:52am UTC
Well, obviously we won't be able to tell what's going on if the part you posted works as desired.
You're not reusing those streams, are you? You really should instantiate them inside the function instead of passing them.
Mar 31, 2011 at 3:56am UTC
sorry
Last edited on Apr 1, 2011 at 12:29am UTC
Mar 31, 2011 at 4:02am UTC
//initialize(inFile, outFile);
Mar 31, 2011 at 4:05am UTC
thats a type error....not supposed to be there
Mar 31, 2011 at 4:09am UTC
Then you need to call initialize() from main() after you open the files. As it is, the program should work just as you described.
Mar 31, 2011 at 4:19am UTC
it still does the same...
Mar 31, 2011 at 4:54am UTC
i got it
Last edited on Apr 1, 2011 at 12:27am UTC