File output

  • #1
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?
 

Answers and Replies

  • #2
560
1
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...
 
  • #3
jtbell
Mentor
15,765
4,011
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()? :yuck:
 
Last edited:
  • #4
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
 
  • #5
what if we get an initial error invalid drive or directory while opening a saved c++ program?
 

Related Threads on File output

  • Last Post
Replies
1
Views
5K
  • Last Post
Replies
7
Views
40K
Replies
1
Views
4K
Replies
6
Views
1K
  • Last Post
Replies
5
Views
1K
Replies
4
Views
2K
Replies
7
Views
29K
Top