Header file:
stdio.h
Synopsis:
FILE *fopen(const char *filename, const char *mode);
Description:
Opens the file and returns a stream on success, or NULL on failure.
fopen function C example:
#include<stdio.h>
#include<errno.h>
#include<string.h>
int main() {
FILE *fp;
char ch, *str;
fp = fopen("file.txt", "r");
if (fp == NULL) {
/* returns error message corresponding to errno */
str = strerror(errno);
/* prints error message corresponding to errno */
perror(str);
return;
}
while ((ch = getc(fp)) != EOF)
putchar(ch);
fclose(fp);
return 0;
}
Output:
jp@jp-VirtualBox:~/$ ls
a.out fopen.c file.txt
jp@jp-VirtualBox:~/$ cat file.txt
hello world
hello world
hello world
hello world
hello world
jp@jp-VirtualBox:~/$ ./a.out
hello world
hello world
hello world
hello world
No comments:
Post a Comment