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

Click For Summary

Discussion Overview

The discussion revolves around file operations in C++, specifically how to create and view a text file generated by a program. Participants explore the implications of the code snippet provided and the context in which it runs, including considerations of the current working directory (cwd) and potential errors encountered when opening files.

Discussion Character

  • Exploratory, Technical explanation, Homework-related

Main Points Raised

  • One participant inquires about the existence of the text file "a.txt" after executing a file operation in C++.
  • Another participant explains that the file will be created in the current working directory, which varies depending on how the program is executed (e.g., from an IDE or command line).
  • A further comment notes that running the program from the Unix command line will result in the output file being located in the cwd at that time.
  • One participant expresses surprise at the use of "void main()" instead of "int main()", suggesting a potential outdated practice.
  • A later reply acknowledges successfully opening the file and mentions that the discussion is part of their learning process in school.
  • A question is raised about encountering an "invalid drive or directory" error when attempting to open a saved C++ program.

Areas of Agreement / Disagreement

Participants generally agree on the creation of the file and its location being dependent on the cwd, but there is no consensus on the implications of using "void main()" versus "int main()" or the error mentioned in the last post.

Contextual Notes

Participants have not fully explored the implications of the cwd in different operating systems or the specifics of error handling related to file operations.

Who May Find This Useful

Students learning about file operations in C++, educators teaching programming concepts, and individuals interested in understanding file handling in different environments.

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?
 
Technology 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?
 

Similar threads

  • · Replies 12 ·
Replies
12
Views
11K
  • · Replies 9 ·
Replies
9
Views
4K
  • · Replies 33 ·
2
Replies
33
Views
3K
  • · Replies 29 ·
Replies
29
Views
4K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 75 ·
3
Replies
75
Views
7K
  • · Replies 4 ·
Replies
4
Views
5K
  • · Replies 8 ·
Replies
8
Views
5K
  • · Replies 9 ·
Replies
9
Views
5K