<< Chapter < Page Chapter >> Page >

Output:

File opened successfully. 111222 33Characters read: 10 111222 333444 5iveCharacters read: 10

The above code: passing a char pointer reads in the entire text file, as demonstrated. Note that the number fread returns in the char pointer case is clearly incorrect. This is because the char pointer (d in the example) must be initialized to point to something first.

An important line is: c[n] = '\0'; Previously, we put 10 instead of n (n is the number of characters read). The problem with this was if the text file contained less than 10 characters, the program would put the null character at a point past the end of the file.

There are several things you could try with this program:

  • After reading the memory allocation section, try allocating memory for d using malloc() and freeing it later with free().
  • Read 25 characters instead of 10: n = fread(c, 1, 25, file);
  • Not bother adding a null character by removing: c[n] = '\0';
  • Not bother closing and reopening the file by removing the fclose and fopen after printing the char array.

Binary files have two features that distinguish them from text files: You can jump instantly to any record in the file, which provides random access as in an array; and you can change the contents of a record anywhere in the file at any time. Binary files also usually have faster read and write times than text files, because a binary image of the record is stored directly from memory to disk (or vice versa). In a text file, everything has to be converted back and forth to text, and this takes time.

Besides reading and writing “blocks” of characters, you can use fread and fwrite to do “binary” I/O. For example, if you have an array of int values:

int array[N];

you could write them all out at once by calling

fwrite(array, sizeof(int), N, fp);

This would write them all out in a byte-for-byte way, i.e. as a block copy of bytes from memory to the output stream, i.e. not as strings of digits as printf %d would. Since some of the bytes within the array of int might have the same value as the \n character, you would want to make sure that you had opened the stream in binary or "wb" mode when calling fopen.

Later, you could try to read the integers in by calling

fread(array, sizeof(int), N, fp);

Similarly, if you had a variable of some structure type:

struct somestruct x;

you could write it out all at once by calling

fwrite(&x, sizeof(struct somestruct), 1, fp);

and read it in by calling

fread(&x, sizeof(struct somestruct), 1, fp);

Close files

The funtion for closing a file :

int fclose(FILE* fp);

This function closes the file associated with the fp and disassociates it. All internal buffers associated with the file are flushed: the content of any unwritten buffer is written and the content of any unread buffer is discarded. Even if the call fails, the fp passed as parameter will no longer be associated with the file.

Return value: If the file is successfully closed, a zero value is returned.

#include<stdio.h>int main () {FILE * fp; fp = fopen ("myfile.txt","wt");fprintf (fp, "fclose example"); fclose (fp);return 0; }
Got questions? Get instant answers now!

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Introduction to computer science. OpenStax CNX. Jul 29, 2009 Download for free at http://cnx.org/content/col10776/1.1
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Introduction to computer science' conversation and receive update notifications?

Ask