fopen, freopen, fdopen system calls are used to create file streams.
Definition:
ubuntu@ip-172-31-23-227:~$ less /usr/include/stdio.h
......
/* Open a file and create a new stream for it.
This function is a possible cancellation point and therefore not
marked with __THROW. */
extern FILE *fopen (const char *__restrict __filename,
const char *__restrict __modes) __wur;
/* Open a file, replacing an existing stream with it.
This function is a possible cancellation point and therefore not
marked with __THROW. */
extern FILE *freopen (const char *__restrict __filename,
const char *__restrict __modes,
FILE *__restrict __stream) __wur;
......
/* Create a new stream that refers to an existing system file descriptor. */
extern FILE *fdopen (int __fd, const char *__modes) __THROW __wur;
......
2. File open mode(fopen)
Following mode are allowed at unix:
"r", "rb", "w", "wb", "a", "ab", "r+", "rb+", "w+", "wb+", "a+", "ab+"
fileio.c:
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#define OWN_BUFFSIZE 1024
int main(int argc, char* argv[])
{
FILE* fp;
char buf[OWN_BUFFSIZE];
// Open the test1.txt for reading
// Note: it won't truncate the file or create the file
if((fp = fopen("test1.txt", "r")) == NULL) {
printf("fopen r error!\n");
exit(1);
}
// Open the test1 for reading, the only difference is
// "rb" indicates that test1 is binary file
if((fp = fopen("test1", "rb")) == NULL) {
printf("fopen rb error!\n");
exit(1);
}
// Open the test2.txt for writing only.
// If file exists, truncate it to 0
// If file doesn't exist, create a new one
if((fp = fopen("test2.txt", "w")) == NULL) {
printf("fopen w error!\n");
exit(1);
}
// Open the test2 for writing only, the only difference is
// "wb" indicates that test2 is binary file
if((fp = fopen("test2", "wb")) == NULL) {
printf("fopen wb error!\n");
exit(1);
}
// Open for appending at the end of file.
// If file doesn't exist, create new file.
if((fp = fopen("test3.txt", "a")) == NULL) {
printf("fopen a error!\n");
exit(1);
}
// Open for appending at the end of file
// The only difference is: "ab" indicates that
// test is binary file
if((fp = fopen("test3", "ab")) == NULL) {
printf("fopen ab error!\n");
exit(1);
}
// Open for reading and writing
// file has to exist, otherwise, fail
if((fp = fopen("test4.txt", "r+")) == NULL) {
printf("fopen r+ error!\n");
exit(1);
}
// Open for reading and writing, the only difference is:
// "rb+"(or "r+b") indicates that test4 is binary file
if((fp = fopen("test4", "rb+")) == NULL) {
printf("fopen rb+ error!\n");
exit(1);
}
// Open for reading and writing
// If file doesn't exist, create one
// If file already exists, truncate to 0
if((fp = fopen("test5.txt", "w+")) == NULL) {
printf("fopen w+ error!\n");
exit(1);
}
// Open for reading and writing, the only difference is
// "wb+" indicates that test5 is one binary file
if((fp = fopen("test5", "wb+")) == NULL) {
printf("fopen wb+ error!\n");
exit(1);
}
// Open for reading and appending.
// If file doesn't exist, create one.
if((fp = fopen("test6.txt", "a+")) == NULL) {
printf("fopen a+ error!\n");
exit(1);
}
// Open for reading and appending, the only difference is:
// "ab+" indicates that test6 is one binary file
if((fp = fopen("test6", "ab+")) == NULL) {
printf("fopen ab+ error!\n");
exit(1);
}
exit(0);
}
No comments:
Post a Comment