Confusion about different file modes - c

If I open a (say) binary file, and I want to append the end of it both of the following ways seem to work for me
fileVar = fopen("FileName", "w+b");
and
fileVar = fopen("FileName", "r+b");
I have read the documentation, but I'm not clear about the difference between these two methods of opening the file. This website says that w+ will overwrite a file if it doesn't exist already, and a+ will append to the end of the file. I haven't tried using a+, but it seems to do the same thing as r+.
Question: What exactly is the difference between the three ways of opening a file, r+, w+ and a+?

r+ starts at beginning of file, but will not create a new file if it doesn't exists.
w+ truncates existing file to zero length if the file exists, otherwise creates a new file.
a+ starts at end of file if file exists, otherwise creates a new file.
Access modes r+, w+ and a+ opens the file in read and write mode, but with the above difference:
Both r+ and w+ we can read ,write on file but r+ does not truncate (delete) the content of file as well it doesn’t create a new file if such file doesn’t exits while in w+ truncate the content of file as well as create a new file if such file doesn’t exists.

Just like the website says:
r+ will open the file for reading & writing, but the file must exist.
w+ will open the file for reading & writing, but if the file exists it will truncate the file (remove its contents).
a+ will open the file for reading & writing, but while reading is allowed at any location, you can only write to the end of the file, i.e. append.

Related

Reading a string from a file with C. Fopen with w+ mode is not working

I made a C program that reads a string from a .txt file, then it encrypts the string, and finally it writes the string in the same file.
The thing is that if I use fopen("D:\\Prueba.txt","w+"), the program doesn't work, it prints garbage like this )PHI N.
I've debugged and I know the error is there in that line, because if I use fopen("D:\\Prueba.txt","r+"), the program works, and it writes what it should.
But I want to use w+ because it will rewrite what the .txt file had. Why is w+ not working?
If you're opening with w+ to first read the content, that's not going to work. From C11:
w+: truncate to zero length or create text file for update.
What's probably happening is that you read data from the now empty file but don't correctly check that it worked. That would explain the weird "content" you see of )PHI N.
One solution is to open the file as with r, open another file with w, and transfer the contents, encrypting them as part of that process. Then close both, delete the original, and rename the new one to the original name. This will allow you to process arbitrarily-sized files since you process them a bit at a time.
If you don't want to use a temporary file, and you're sure you can store the entire content in memory, you could open it r+, get the content, the reopen it with a new mode, such as with:
FILE *readFh = fopen( "myfile.txt", "r+");
// Read in content, massage as needed.
FILE *writeFh = frepoen( NULL, "w+", readFh);
// Provided that worked, you should now have an empty file to write to.
// Write back your massaged data.

does append "a+" automatically put cursor to the end

I want to open a file with c then add some content to it and close it. I just want to know a+ in fopen automatically navigates to the last character of file.
Yes it does.
Why don't you try, or read the manual ?
Here it is :
r Open text file for reading. The stream is positioned at the beginning of the file.
r+ Open for reading and writing. The stream is positioned at the beginning of the file.
w Truncate file to zero length or create text file for writing. The stream is positioned at the beginning of the
file.
w+ Open for reading and writing. The file is created if it does not exist, otherwise it is truncated. The stream is
positioned at the beginning of the file.
a Open for appending (writing at end of file). The file is created if it does not exist. The stream is positioned
at the end of the file.
a+ Open for reading and appending (writing at end of file). The file is created if it does not exist. The initial
file position for reading is at the beginning of the file, but output is always appended to the end of the file.

File Modes in Binary

Well I was looking into ways to open a file in binary and I saw that you can open one for r+b and a+b but whats the difference because this is what it says for definition.
a+b :Open a file for both reading and writing in binary mode.
r+b: Open a file for both reading and writing in binary mode.
a+b
Open for reading and appending (writing at end of file). The file is created if it does not exist. The initial file position for reading is at the beginning of the file, but output is always appended to the end of the file.
r+b Open for reading and writing. The stream is positioned at the beginning of the file.
w+b Open for reading and writing. The file is created if it does not exist, otherwise it is truncated. The stream is positioned at the beginning of the file.
So, a+b will create the file if it does not exist, and it will append to the file when you write to it,
r+b will not create the file if it doesn't already exist (fopen() would fail), and writing to the file will start at the beginning of the file.

Clear contents of a Text File

I am working on a sever/client applicataion. I want to maintain information of all active clients in a text file named "Information.txt".
I update this text file after every 3 seonds. So, I want the text file to clear all of its contents after every 3 seconds without deleting the file.
Is there any way to do it ? :(
I don't want to use freopen().
A problem with clearing the file periodically is that if your process crashes after the file has been cleared but before it has been written, you lose data: the old data is gone, but the new data is not there yet.
A common approach to this problem is to create a new file, writing it, and then moving the new file to replace the old one. This way you always have a file, and sometimes (for very brief periods of time) you have two files.
Try with
fopen(filename, flag)
Open your file with flag= "w" or "wb" and it will be cleared
Just open the file with fopen and setting the flag to w or w+ or wb
From fopen man page
w
Truncate file to zero length or create text file for writing. The
stream is positioned at the beginning of the file.
w+
Open for reading and writing. The file is created if it does not
exist, otherwise it is truncated. The stream is positioned at the
beginning of the file.

is there any option to create an output text file in the name of the input file in c programm?

is there any option to generate an output text file as same as the input file name?
in C i got the file name using:
gets(file_name);
and open it through the:
f1=fopen(file_name,"r");
comment.how do i open the file without entering the format type?
for example for file100.txt i'd like to enter file100 to read the file.
and any option to get the output file as same name as the input file?
You can use
snprintf(new_filename, sizeof new_filename, "%s.%d", File_name, int_val);
For your problem with the file name, you can use e.g. sprintf:
char full_file_name[256];
sprintf(full_file_name, "%s.txt", file_name);
This is not recommended without some validation of the entered file name of course.
For your other problem, from the documentation of fopen:
r+ Open for reading and writing. The stream is positioned at the beginning
of the file.
w+ Open for reading and writing. The file is created if it does not exist,
otherwise it is truncated.
The stream is positioned at the beginning of the file.
a+ Open for reading and appending (writing at end of file). The file is
created if it does not exist.
The initial file position for reading is at the beginning of the file,
but output is always appended to the end of the file.
for creating output file with the same name, simply save your output content into some string. Then, close the file and open it again with write mode("w"). And then, write the content and close the file again.

Resources