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

AI Thread Summary
The discussion centers on how to verify the output of a file operation in C++. A code snippet is provided that writes an integer input to a file named "a.txt". The file is created in the current working directory (cwd), which varies based on how the program is executed—either from an executable or an IDE. The cwd can be determined by the method of execution and the operating system used, with specific paths for Windows and UNIX systems. Additionally, there is a mention of the outdated use of "void main()" instead of "int main()", and a query about potential errors like "invalid drive or directory" when attempting to open a saved C++ program, indicating a concern for beginners in file operations.
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?
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
What percentage of programmers have learned to touch type? Have you? Do you think it's important, not just for programming, but for more-than-casual computer users generally? ChatGPT didn't have much on it ("Research indicates that less than 20% of people can touch type fluently, with many relying on the hunt-and-peck method for typing ."). 'Hunt-and-peck method' made me smile. It added, "For programmers, touch typing is a valuable skill that can enhance speed, accuracy, and focus. While...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...

Similar threads

Replies
12
Views
10K
Replies
9
Views
3K
Replies
33
Views
2K
Replies
2
Views
2K
Replies
1
Views
1K
Replies
75
Views
6K
Replies
4
Views
5K
Replies
8
Views
4K
Replies
9
Views
5K
Back
Top