Advertisement

Newbie creating files in c++

Started by May 15, 2004 04:42 PM
7 comments, last by programmer2030 20 years, 4 months ago
I would like to know how to create a new file in c++ on Linux. I''ve tried using fopen() but this will not create the file if it doesn''t exist. I need a function to create the file if it doesn''t exist. Any help would be appreciated. ----------------------------------------------------------- I am me. I think. Or do I. What is thinking?
-------------------------------------------------------------Programmer2030I am me. I think. Or do I. What is thinking?<IMG SRC="http://web3.foxinternet.net/sixflowers/my sign.png"
What open mode are you using for fopen()? fopen() should create a new file when using "w", "a", "w+", and "a+".

Though if you''re using C++ and not C, you might consider using std::ofstream instead of C style file I/O functions.
Advertisement
it may be working, and you''re jsut looking in the wrong place

current working directory semantics are a lot different on linux than on windows.
Thank you, I totally forgot about fstream.
-------------------------------------------------------------Programmer2030I am me. I think. Or do I. What is thinking?<IMG SRC="http://web3.foxinternet.net/sixflowers/my sign.png"
quote: Original post by C-Junkie
current working directory semantics are a lot different on linux than on windows.


How? Both have a current working directory, and will create files there if you don''t specify the path from the (or in Windows'' case ,a) root.

True, there is a kind of kludgy dos-mode "keep working directories independently for different drive letters", but as long as you never use paths in the form c:x (which is a bad idea in general) you won''t notice it.

Mark
quote: Original post by markr
quote: Original post by C-Junkie
current working directory semantics are a lot different on linux than on windows.

True, there is a kind of kludgy dos-mode "keep working directories independently for different drive letters", but as long as you never use paths in the form c:x (which is a bad idea in general) you won''t notice it.

ahem.
quote: on linux than on WINDOWS

Windows applications almost always are started with a current working directory that has the executable in it. Linux almost always does not.

In fact, nautilus (iirc) always runs programs with the home directory as working directory, if you run them by clicking on the executable.

the only situation where the windows working directory is NOT the folder the program is in is when you are either setting the CWD with the shortcut properties thing, or when you are (rarely) using the command line.

Advertisement
So in fact the semantics of the working directory are actually the same, but the common behaviour of the shell is a bit different.

I say "a bit" different, because if you are invoking them from the command line, it''s the same. Only that the gui shell behaves in its own way.

Anyway if it''s a real problem just do chdir(dirname(argv[0])) at the beginning of main()

Mark
quote: Original post by markr
Anyway if it''s a real problem just do chdir(dirname(argv[0])) at the beginning of main()


Seeing that most executables on linux end up in a directory which is not writable (/usr/bin,/usr/local/bin,...) for normal users, this would not be a good idea.

"THE INFORMATION CONTAINED IN THIS REPORT IS CLASSIFIED; DO NOT GO TO FOX NEWS TO READ OR OBTAIN A COPY." , the pentagon
quote: Original post by George2
quote: Original post by markr
Anyway if it''s a real problem just do chdir(dirname(argv[0])) at the beginning of main()


Seeing that most executables on linux end up in a directory which is not writable (/usr/bin,/usr/local/bin,...) for normal users, this would not be a good idea.


Is also just plain does not work. I guess I edited it out of my post, but linux almost never has the full path in argv[0] either.

This topic is closed to new replies.

Advertisement