The I/O functionality of C is fairly low-level by modern standards; C abstracts all file operations into operations on streams of bytes, which may be "input streams" or "output streams". Unlike some earlier programming languages, C has no direct support for random-access data files; to read from a record in the middle of a file, the programmer must create a stream, seek to the middle of the file, and then read bytes in sequence from the stream.
The stream model of file I/O was popularized by the Unix operating system, which was developed concurrently with the C programming language itself. The vast majority of modern operating systems have inherited streams from Unix, and many languages in the C programming language family have inherited C's file I/O interface with few if any changes (for example, PHP). The C++ standard library reflects the "stream" concept in its syntax; see iostream.
Opening a file using fopen
A file is opened using fopen, which returns an I/O stream attached to the specified file or other device from which reading and writing can be done. If the function fails, it returns a null pointer.
The related C library function freopen performs the same operation after first closing any open stream associated with its parameters.
They are defined as
FILE *fopen(const char *path, const char *mode);
FILE *freopen(const char *path, const char *mode, FILE *fp);
The fopen function is essentially a slightly higher-level wrapper for the open system call of Unix operating systems. In the same way, fclose is often a thin wrapper for the Unix system call close, and the C FILE structure itself often corresponds to a Unix file descriptor. In POSIX environments, the fdopen function can be used to initialize a FILE structure from a file descriptor; however, file descriptors are a purely Unix concept not present in standard C.
The mode parameter to fopen and freopen must be a string that begins with one of the following sequences:
mode description starts..
r rb open for reading beginning
for further modes refer the man pages
Closing a stream using fclose
The fclose function takes one argument: a pointer to the FILE structure of the stream to close.
int fclose(FILE *fp);
Reading from a stream using fgetc
The fgetc function is used to read a character from a stream.
int fgetc(FILE *fp);
Writing a file using fwrite
fwrite is defined as
size_t fwrite (const void *array, size_t size, size_t count, FILE *stream);
fwrite function writes a block of data to the stream. It will write an array of count elements to the current position in the stream. For each element, it will write size bytes. The position indicator of the stream will be advanced by the number of bytes written successfully.
Writing a file using fwrite
fwrite is defined as
size_t fwrite (const void *array, size_t size, size_t count, FILE *stream);
fwrite function writes a block of data to the stream. It will write an array of count elements to the current position in the stream. For each element, it will write size bytes. The position indicator of the stream will be advanced by the number of bytes written successfully.
The function will return the number of elements written successfully. The return value will be equal to count if the write completes successfully. In case of a write error, the return value will be less than count.
The following program opens a file named new1.c, writes a string of characters to the file, then closes it.
#include
#include
#include
int main(void)
{
FILE *fp;
size_t count;
const char *str = "hello\n";
fp = fopen("sample.txt", "w");
if(fp == NULL) {
perror("failed to open sample.txt");
return EXIT_FAILURE;
}
count = fwrite(str, 1, strlen(str), fp);
printf("Wrote %zu bytes. fclose(fp) %s.\n", count, fclose(fp) == 0 ? "succeeded" : "failed");
return EXIT_SUCCESS;
}
Writing to a stream using fputc
The fputc
function is used to write a character to a stream.
int fputc(int c, FILE *fp);
The parameter c
is silently converted to an unsigned char
before being output. If successful, fputc
returns the character written. If unsuccessful, fputc returns EOF
.
The standard macro putc
, also defined in
, behaves in almost the same way as fputc
, except that—being a macro—it may evaluate its arguments more than once.
The standard function putchar
, also defined in
, takes only the first argument, and is equivalent to putc(c, stdout)
where c
is that argument.
#include
#include
int main(void)
{
char buffer[5] = {0}; /* initialized to zeroes */
int i, rc;
FILE *fp = fopen("myfile", "rb");
if (fp == NULL) {
perror("Failed to open file \"myfile\"");
return EXIT_FAILURE;
}
for (i = 0; (rc = getc(fp)) != EOF && i < 5; buffer[i++] = rc)
;
fclose(fp);
if (i == 5) {
puts("The bytes read were...");
printf("%x %x %x %x %x\n", buffer[0], buffer[1], buffer[2], buffer[3], buffer[4]);
} else
fputs("There was an error reading the file.\n", stderr);
return EXIT_SUCCESS;
}
No comments:
Post a Comment