File Modes in Binary - c

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.

Related

What is an update stream in C?

At n1256 7.19.5.2 paragraph 2 (with my bold):
If stream points to an output stream or an update stream in which the most recent
operation was not input, the fflush function causes any unwritten data for that stream
to be delivered to the host environment to be written to the file; otherwise, the behavior is
undefined.
If there was not the word 'update stream', the whole paragraph would make sense. But I don't know what it is. Standard itself doesn't introduce it. Google search doesn't seem to work. What's the meaning of it?
The term "update stream" simply means a stream that is both readable and writable.
This is specified in §7.19.5.3 point 3 (link, bold mine):
#include <stdio.h>
FILE *fopen(const char * restrict filename,const char * restrict mode);
[...]
The argument mode points to a string. If the string is one of the following, the file is open in the indicated mode. Otherwise, the behavior is undefined(237).
r: open text file for reading
w: truncate to zero length or create text file for writing
a: append; open or create text file for writing at end-of-file
rb: open binary file for reading
wb: truncate to zero length or create binary file for writing
ab: append; open or create binary file for writing at end-of-file
r+: open text file for update (reading and writing)
w+: truncate to zero length or create text file for update
a+: append; open or create text file for update, writing at end-of-file(237)
r+b or rb+: open binary file for update (reading and writing)
w+b or wb+: truncate to zero length or create binary file for update
a+b or ab+: append; open or create binary file for update, writing at end-of-file
(237) If the string begins with one of the above sequences, the implementation might choose to ignore the remaining characters, or it might use them to select different kinds of a file (some of which might not conform to the properties in 7.19.2).
The term "update" in this context means a file opened for both reading and writing.
The term is used in the specification of the fopen function in section 7.19.5.3 of the C99 standard:
3 The argument mode points to a string.If the string is one of the following, the file is open in the indicated
mode.Otherwise, the behavior is undefined.
r open text file for reading
w truncate to zero length or create text file for writing
a append; open or create text file for writing at end-of-file
rb open binary file for reading
wb truncate to zero length or create binary file for writing
ab append; open or create binary file for writing at end-of-file
r+ open text file for update (reading and writing)
w+ truncate to zero length or create text file for update
a+ append; open or create text file for update, writing at end-of-file
r+b or rb+ open binary file for update (reading and writing)
w+b or wb+ truncate to zero length or create binary file for update
a+b or ab+ append; open or create binary file for update, writing at end-of-file
...
6 When a file is opened with update mode ('+' as the second or third character in the above list of mode argument values), both input and output may be performed on the associated stream. However, output shall not be directly followed by input without an intervening call to the fflush function or to a file positioning function (fseek,fsetpos, or rewind), and input shall not be directly followed by output without an intervening call to a file positioning function, unless the input operation encounters end-of-file. Opening(or creating) a text file with update mode may instead open (or create) a binary stream in some implementations

fprintf() function in C is not working properly

I wrote this code to input a number from a user and output it to a file .But its is not working ,after running the code the output.txt file is still empty.
Please tell me where I have done wrong .
I assure that I have created the output.txt file before running the program so the
file pointer will not be NULL.
#include <stdio.h>
#include <stdlib.h>
int main (void)
{
FILE *ptr;ptr=fopen("output.txt","rw");
if(ptr==NULL){printf("Error in oppening file aborting .......");exit(0);}
char ch[100];
scanf("%s",ch);
fprintf(ptr,"%s",ch);
fclose(ptr);
return 0;
}
From fopen documentation, the supported access modes are:
"r" read: Open file for input operations. The file must exist.
"w" write: Create an empty file for output operations. If a file with
the same name already exists, its contents are discarded and the file
is treated as a new empty file.
"a" append: Open file for output at the end of a file. Output
operations always write data at the end of the file, expanding it.
Repositioning operations (fseek, fsetpos, rewind) are ignored. The
file is created if it does not exist. "r+" read/update: Open a file
for update (both for input and output). The file must exist.
"w+" write/update: Create an empty file and open it for update (both
for input and output). If a file with the same name already exists its
contents are discarded and the file is treated as a new empty file.
"a+" append/update: Open a file for update (both for input and output)
with all output operations writing data at the end of the file.
Repositioning operations (fseek, fsetpos, rewind) affects the next
input operations, but output operations move the position back to the
end of file. The file is created if it does not exist.
In your code you use "rw" which is invalid and that's the reason your program doesn't work.
Change "rw" to "w" and your program will work. Note that you don't need to create output.txt, fopen will create it for you if your current user has write privileges in program's directory.

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.

Confusion about different file modes

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.

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