Reputation: 109
I wrote a program that creates a text file and reopens the text file with binary mode, here is a code:
#include <stdio.h>
#include <stdlib.h>
void main() {
char ch;
FILE *fp;
fp = fopen("/home/brushmonk/television_table_barchart.txt", "w+");
/* fp is an input stream */
if (fp == NULL) {
fprintf(stderr,
"Can't create television_table_barchart.txt.\n");
exit(EXIT_FAILURE);
}
char *p = "Television is one of the means whereby these feelings are
created and conveyed.\nThe table and bar chart show how journey time
in a city centre changed after improvements were made to the transport
network, and the costs of using different forms of transport in the
city.";
fputs(p, fp);
fp = fopen("/home/brushmonk/television_table_barchart.txt", "rb");
/* fp is an output stream */
while((ch = getc(fp)) != EOF)
putchar(ch);
putchar('\n');
if (fclose(fp) != 0)
fprintf(stderr,
"Error in closing television_table_barchart.txt.\n");
exit(EXIT_SUCCESS);
}
but I run it in linux. output turned out to be nothing! what is going on? because textbook says in linux OS "rb"
and "r"
in fopen()
funtion make no difference.
Upvotes: 1
Views: 192
Reputation: 546073
You forgot to close your file.
Rule of thumb: as for any other resource, after you acquire a file handle (via fopen
), you must release the file handle (via fclose
). It so happens that for file streams the closing of the stream also flushes it, i.e. it logically commits the previous write operations to disk.
Being able to read from a file is only guaranteed to work after the changes have been committed.
That said, you can commit changes to a file without closing it, via fflush
. Thus, to make your code work, it’s sufficient to call fflush(fp)
after the initial fputs
.
However, this does not absolve you of releasing the file handle (via fclose
). The fclose
call you have at the end of your code is insufficient since that closes a different file handle: the one acquired by your second call to fopen
.
Upvotes: 1
Reputation: 145297
You must close the file with fclose(fp)
before reopening it in read mode. Changes are buffered in memory, so if you reopen the file in read mode before you flush the pending data to disk, it is still empty on disk.
You should avoid string literals with embedded unescaped newlines. You can break long literals on multiple lines as shown in the code below.
Note also that ch
must have type int
to reliably detect end of file in the last loop.
Here is a modified version:
#include <stdio.h>
int main() {
int ch;
FILE *fp = fopen("/home/brushmonk/television_table_barchart.txt", "w+");
/* fp is an input stream */
if (fp == NULL) {
fprintf(stderr,
"Can't create television_table_barchart.txt.\n");
return EXIT_FAILURE;
}
char *p = "Television is one of the means whereby these feelings are "
"created and conveyed.\nThe table and bar chart show how journey time "
"in a city centre changed after improvements were made to the transport "
"network, and the costs of using different forms of transport in the "
"city.";
fputs(p, fp);
fclose(fp);
fp = fopen("/home/brushmonk/television_table_barchart.txt", "rb");
/* fp is an output stream */
while ((ch = getc(fp)) != EOF)
putchar(ch);
putchar('\n');
if (fclose(fp) != 0)
fprintf(stderr, "Error in closing television_table_barchart.txt.\n");
return EXIT_SUCCESS;
}
Upvotes: 1
Reputation: 736
You need to close the file to commit and save the changes made to the file, before opening it again in read mode.
fclose(fp);
Upvotes: 2