Checking Output of File Operations: How to Open & View a Text File

Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
4 replies · 3K views
jyothsna pb
Messages
42
Reaction score
0
how to see the output of a file operation?

eg void main()
{ int a;
cin>>a;
ofstream o("a.txt");
o<<a;
o.close();
}

does the text file "a" come into existence? can we open it?
 
Physics news on Phys.org
The snippet you post there will create a file named a.txt in the "present working directory", sometimes called the "current working directory". What this directory is will depend on how and on what operating system you ran the program. If you ran the program by clicking on a .exe or something, the cwd will probably be the same directory as the exe. If you ran the program by selecting something like "Build and run" in an IDE, the cwd will be unpredictable and depend on what IDE you are using. Probably it will be inside a directory in your project directory named "build" or "debug" or something.

A program can get its own cwd but the way you do this depends on whether you are on Windows or a UNIX...
 
If you run the program from the Unix command line, the output file will probably be in whatever your cwd is when you run the program.

Oh wow, are there still books out there that teach people to use void main() instead of int main()?
 
Last edited:
thanx 4 all the replies i did open the file
we just started with file operations in our school so the doubts may sound a lil silly :P
 
what if we get an initial error invalid drive or directory while opening a saved c++ program?