C++笔记之C语言中常见的文件操作函数汇总
文章目录 C++笔记之C语言中常见的文件操作函数汇总 1. `fopen()`: 打开文件并返回文件指针 2. `fclose()`: 关闭文件 3. `fread()`: 从文件中读取数据 4. `fwrite()`: 向文件写入数据 5. `fgetc()`: 从文件中读取一个字符 6. `fputc()`: 向文件写入一个字符 7. `fgets()`: 从文件中读取一行文本 8. `fputs()`: 向文件写入一行文本 9. `fprintf()`: 格式化写入数据到文件 10. `fscanf()`: 从文件中格式化读取数据 11. `feof()`: 检查文件结束标志 12. `ftell()`: 获取文件位置指针的当前位置 13. `fseek()`: 设置文件位置指针的位置 14. `rewind()`: 将文件位置指针重置到文件的开头 15. `remove()`: 删除文件 16. `rename()`: 重命名文件 17. `tmpfile()`: 创建临时文件 18. `tmpnam()`: 生成临时文件名 19. `feof()`: 检查文件结束标志 20. `fflush()`: 刷新文件缓冲区 21. `ferror()`: 检查文件错误标志 22. `perror()`: 打印错误消息 23. `clearerr()`: 清除文件错误和结束标志 24. `setvbuf()`: 设置文件缓冲区 25. `fileno()`: 获取文件描述符 26. `flockfile()`: 锁定文件以进行线程安全的访问 27. `funlockfile()`: 解锁文件以允许其他线程访问 28. `getc()`: 从标准输入流中读取一个字符 29. `putc()`: 将字符写入标准输出流 30. `getchar()`: 从标准输入流中读取一个字符 31. `putchar()`: 将字符写入标准输出流 32. `feof()`: 检查文件结束标志 33. `ferror()`: 检查文件错误标志 34. `clearerr()`: 清除文件错误和结束标志 35.一些易混淆的函数以及它们的详细说明 36.每个函数的参数列表参数数量说明
1. fopen()
: 打开文件并返回文件指针
# include <stdio.h> int main ( ) { FILE * file = fopen ( "example.txt" , "w" ) ; if ( file == NULL ) { perror ( "文件打开失败" ) ; return 1 ; } fclose ( file) ; return 0 ;
}
2. fclose()
: 关闭文件
# include <stdio.h> int main ( ) { FILE * file = fopen ( "example.txt" , "w" ) ; if ( file == NULL ) { perror ( "文件打开失败" ) ; return 1 ; } fclose ( file) ; return 0 ;
}
3. fread()
: 从文件中读取数据
# include <stdio.h> int main ( ) { FILE * file = fopen ( "data.bin" , "rb" ) ; if ( file == NULL ) { perror ( "文件打开失败" ) ; return 1 ; } char buffer[ 100 ] ; size_t bytesRead = fread ( buffer, sizeof ( char ) , sizeof ( buffer) , file) ; fclose ( file) ; return 0 ;
}
4. fwrite()
: 向文件写入数据
# include <stdio.h> int main ( ) { FILE * file = fopen ( "output.txt" , "w" ) ; if ( file == NULL ) { perror ( "文件打开失败" ) ; return 1 ; } char data[ ] = "Hello, World!" ; size_t bytesWritten = fwrite ( data, sizeof ( char ) , sizeof ( data) , file) ; fclose ( file) ; return 0 ;
}
5. fgetc()
: 从文件中读取一个字符
# include <stdio.h> int main ( ) { FILE * file = fopen ( "input.txt" , "r" ) ; if ( file == NULL ) { perror ( "文件打开失败" ) ; return 1 ; } int ch = fgetc ( file) ; if ( ch != EOF ) { } fclose ( file) ; return 0 ;
}
6. fputc()
: 向文件写入一个字符
# include <stdio.h> int main ( ) { FILE * file = fopen ( "output.txt" , "w" ) ; if ( file == NULL ) { perror ( "文件打开失败" ) ; return 1 ; } int ch = 'A' ; int result = fputc ( ch, file) ; if ( result == ch) { } fclose ( file) ; return 0 ;
}
7. fgets()
: 从文件中读取一行文本
# include <stdio.h> int main ( ) { FILE * file = fopen ( "input.txt" , "r" ) ; if ( file == NULL ) { perror ( "文件打开失败" ) ; return 1 ; } char buffer[ 100 ] ; if ( fgets ( buffer, sizeof ( buffer) , file) != NULL ) { } fclose ( file) ; return 0 ;
}
8. fputs()
: 向文件写入一行文本
# include <stdio.h> int main ( ) { FILE * file = fopen ( "output.txt" , "w" ) ; if ( file == NULL ) { perror ( "文件打开失败" ) ; return 1 ; } const char * text = "Hello, World!" ; if ( fputs ( text, file) != EOF ) { } fclose ( file) ; return 0 ;
}
9. fprintf()
: 格式化写入数据到文件
# include <stdio.h> int main ( ) { FILE * file = fopen ( "output.txt" , "w" ) ; if ( file == NULL ) { perror ( "文件打开失败" ) ; return 1 ; } int number = 42 ; fprintf ( file, "The answer is: %d\n" , number) ; fclose ( file) ; return 0 ;
}
10. fscanf()
: 从文件中格式化读取数据
# include <stdio.h> int main ( ) { FILE * file = fopen ( "input.txt" , "r" ) ; if ( file == NULL ) { perror ( "文件打开失败" ) ; return 1 ; } int number; if ( fscanf ( file, "%d" , & number) == 1 ) { } fclose ( file) ; return 0 ;
}
11. feof()
: 检查文件结束标志
# include <stdio.h> int main ( ) { FILE * file = fopen ( "input.txt" , "r" ) ; if ( file == NULL ) { perror ( "文件打开失败" ) ; return 1 ; } while ( ! feof ( file) ) { int ch = fgetc ( file) ; if ( ch != EOF ) { } } fclose ( file) ; return 0 ;
}
12. ftell()
: 获取文件位置指针的当前位置
# include <stdio.h> int main ( ) { FILE * file = fopen ( "data.bin" , "rb" ) ; if ( file == NULL ) { perror ( "文件打开失败" ) ; return 1 ; } fseek ( file, 0 , SEEK_END ) ; long fileSize = ftell ( file) ; fclose ( file) ; return 0 ;
}
13. fseek()
: 设置文件位置指针的位置
# include <stdio.h> int main ( ) { FILE * file = fopen ( "data.bin" , "rb" ) ; if ( file == NULL ) { perror ( "文件打开失败" ) ; return 1 ; } fseek ( file, 50 , SEEK_SET ) ; fclose ( file) ; return 0 ;
}
14. rewind()
: 将文件位置指针重置到文件的开头
# include <stdio.h> int main ( ) { FILE * file = fopen ( "data.bin" , "rb" ) ; if ( file == NULL ) { perror ( "文件打开失败" ) ; return 1 ; } rewind ( file) ; fclose ( file) ; return 0 ;
}
15. remove()
: 删除文件
# include <stdio.h> int main ( ) { if ( remove ( "example.txt" ) == 0 ) { } else { perror ( "文件删除失败" ) ; } return 0 ;
}
16. rename()
: 重命名文件
# include <stdio.h> int main ( ) { if ( rename ( "old_name.txt" , "new_name.txt" ) == 0 ) { } else { perror ( "文件重命名失败" ) ; } return 0 ;
}
17. tmpfile()
: 创建临时文件
# include <stdio.h> int main ( ) { FILE * tempFile = tmpfile ( ) ; if ( tempFile != NULL ) { } else { perror ( "创建临时文件失败" ) ; } return 0 ;
}
18. tmpnam()
: 生成临时文件名
# include <stdio.h> int main ( ) { char * tempFileName = tmpnam ( NULL ) ; if ( tempFileName != NULL ) { } else { perror ( "生成临时文件名失败" ) ; } return 0 ;
}
19. feof()
: 检查文件结束标志
# include <stdio.h> int main ( ) { FILE * file = fopen ( "input.txt" , "r" ) ; if ( file == NULL ) { perror ( "文件打开失败" ) ; return 1 ; } while ( ! feof ( file) ) { int ch = fgetc ( file) ; if ( ch != EOF ) { } } fclose ( file) ; return 0 ;
}
20. fflush()
: 刷新文件缓冲区
# include <stdio.h> int main ( ) { FILE * file = fopen ( "output.txt" , "w" ) ; if ( file == NULL ) { perror ( "文件打开失败" ) ; return 1 ; } fputs ( "Hello, World!" , file) ; fflush ( file) ; fclose ( file) ; return 0 ;
}
21. ferror()
: 检查文件错误标志
# include <stdio.h> int main ( ) { FILE * file = fopen ( "example.txt" , "r" ) ; if ( file == NULL ) { perror ( "文件打开失败" ) ; return 1 ; } int ch = fgetc ( file) ; if ( ch == EOF ) { if ( ferror ( file) ) { perror ( "读取文件时发生错误" ) ; } } fclose ( file) ; return 0 ;
}
22. perror()
: 打印错误消息
# include <stdio.h> int main ( ) { FILE * file = fopen ( "example.txt" , "r" ) ; if ( file == NULL ) { perror ( "文件打开失败" ) ; return 1 ; } fclose ( file) ; return 0 ;
}
23. clearerr()
: 清除文件错误和结束标志
# include <stdio.h> int main ( ) { FILE * file = fopen ( "example.txt" , "r" ) ; if ( file == NULL ) { perror ( "文件打开失败" ) ; return 1 ; } clearerr ( file) ; fclose ( file) ; return 0 ;
}
24. setvbuf()
: 设置文件缓冲区
# include <stdio.h> int main ( ) { FILE * file = fopen ( "example.txt" , "w" ) ; if ( file == NULL ) { perror ( "文件打开失败" ) ; return 1 ; } char buffer[ 1024 ] ; setvbuf ( file, buffer, _IOFBF, sizeof ( buffer) ) ; fclose ( file) ; return 0 ;
}
25. fileno()
: 获取文件描述符
# include <stdio.h> int main ( ) { FILE * file = fopen ( "example.txt" , "r" ) ; if ( file == NULL ) { perror ( "文件打开失败" ) ; return 1 ; } int fd = fileno ( file) ; fclose ( file) ; return 0 ;
}
26. flockfile()
: 锁定文件以进行线程安全的访问
# include <stdio.h>
# include <pthread.h> void * thread_function ( void * arg) { FILE * file = ( FILE * ) arg; flockfile ( file) ; funlockfile ( file) ; return NULL ;
} int main ( ) { FILE * file = fopen ( "example.txt" , "w" ) ; if ( file == NULL ) { perror ( "文件打开失败" ) ; return 1 ; } pthread_t thread; pthread_create ( & thread, NULL , thread_function, file) ; pthread_join ( thread, NULL ) ; fclose ( file) ; return 0 ;
}
27. funlockfile()
: 解锁文件以允许其他线程访问
# include <stdio.h>
# include <pthread.h> void * thread_function ( void * arg) { FILE * file = ( FILE * ) arg; flockfile ( file) ; funlockfile ( file) ; return NULL ;
} int main ( ) { FILE * file = fopen ( "example.txt" , "w" ) ; if ( file == NULL ) { perror ( "文件打开失败" ) ; return 1 ; } pthread_t thread; pthread_create ( & thread, NULL , thread_function, file) ; pthread_join ( thread, NULL ) ; fclose ( file) ; return 0 ;
}
28. getc()
: 从标准输入流中读取一个字符
# include <stdio.h> int main ( ) { int ch = getc ( stdin ) ; if ( ch != EOF ) { } return 0 ;
}
29. putc()
: 将字符写入标准输出流
# include <stdio.h> int main ( ) { int ch = 'A' ; int result = putc ( ch, stdout ) ; if ( result == ch) { } return 0 ;
}
30. getchar()
: 从标准输入流中读取一个字符
# include <stdio.h> int main ( ) { int ch = getchar ( ) ; if ( ch != EOF ) { } return 0 ;
}
31. putchar()
: 将字符写入标准输出流
# include <stdio.h> int main ( ) { int ch = 'A' ; int result = putchar ( ch) ; if ( result == ch) { } return 0 ;
}
32. feof()
: 检查文件结束标志
# include <stdio.h> int main ( ) { while ( ! feof ( stdin ) ) { int ch = getc ( stdin ) ; if ( ch != EOF ) { } } return 0 ;
}
33. ferror()
: 检查文件错误标志
# include <stdio.h> int main ( ) { int ch = getc ( stdin ) ; if ( ch == EOF ) { if ( ferror ( stdin ) ) { perror ( "读取标准输入流时发生错误" ) ; } } return 0 ;
}
34. clearerr()
: 清除文件错误和结束标志
# include <stdio.h> int main ( ) { int ch = getc ( stdin ) ; if ( ch == EOF ) { clearerr ( stdin ) ; } return 0 ;
}
35.一些易混淆的函数以及它们的详细说明
fread()
和 fwrite()
: 这两个函数用于二进制文件的读取和写入。fread()
用于从文件中读取二进制数据,而 fwrite()
用于将二进制数据写入文件。它们不适用于文本文件。
fgetc()
和 fputc()
: 这两个函数用于字符级别的读取和写入。fgetc()
从文件中读取一个字符,而 fputc()
将一个字符写入文件。它们适用于文本文件。
fgets()
和 fputs()
: 这两个函数用于文本行的读取和写入。fgets()
从文件中读取一行文本,而 fputs()
将一行文本写入文件。它们适用于文本文件。
getc()
和 putc()
: 这两个函数类似于 fgetc()
和 fputc()
,但它们操作标准输入和标准输出流,而不是文件。getc(stdin)
从标准输入中读取一个字符,而 putc(ch, stdout)
将字符写入标准输出。
getchar()
和 putchar()
: 这两个函数也操作标准输入和标准输出流,类似于 getc()
和 putc()
,但它们不需要传递流参数。getchar()
从标准输入中读取一个字符,而 putchar()
将字符写入标准输出。
feof()
和 ferror()
: 这两个函数都用于检查文件操作的状态,但它们检查的是不同的条件。feof()
检查文件结束标志,如果文件已到达末尾,它返回非零值。ferror()
检查文件错误标志,如果发生了文件相关的错误,它返回非零值。通常,你会在文件操作之后使用这两个函数来检查状态。
36.每个函数的参数列表参数数量说明
# include <stdio.h> int main ( ) { FILE * file = fopen ( "example.txt" , "w" ) ; fclose ( file) ; char buffer[ 100 ] ; size_t bytesRead = fread ( buffer, sizeof ( char ) , sizeof ( buffer) , file) ; char data[ ] = "Hello, World!" ; size_t bytesWritten = fwrite ( data, sizeof ( char ) , sizeof ( data) , file) ; int ch = fgetc ( file) ; int ch = 'A' ; int result = fputc ( ch, file) ; char buffer[ 100 ] ; if ( fgets ( buffer, sizeof ( buffer) , file) != NULL ) { } const char * text = "Hello, World!" ; if ( fputs ( text, file) != EOF ) { } int number = 42 ; fprintf ( file, "The answer is: %d\n" , number) ; int number; if ( fscanf ( file, "%d" , & number) == 1 ) { } while ( ! feof ( file) ) { int ch = fgetc ( file) ; if ( ch != EOF ) { } } long currentPosition = ftell ( file) ; fseek ( file, 50 , SEEK_SET ) ; rewind ( file) ; if ( remove ( "example.txt" ) == 0 ) { } if ( rename ( "old_name.txt" , "new_name.txt" ) == 0 ) { } FILE * tempFile = tmpfile ( ) ; char * tempFileName = tmpnam ( NULL ) ; while ( ! feof ( file) ) { int ch = fgetc ( file) ; if ( ch != EOF ) { } } fflush ( file) ; int ch = fgetc ( file) ; if ( ch == EOF ) { if ( ferror ( file) ) { perror ( "读取文件时发生错误" ) ; } } FILE * file = fopen ( "example.txt" , "r" ) ; if ( file == NULL ) { perror ( "文件打开失败" ) ; return 1 ; } int ch = fgetc ( file) ; if ( ch == EOF ) { clearerr ( file) ; } char buffer[ 1024 ] ; setvbuf ( file, buffer, _IOFBF, sizeof ( buffer) ) ; int fileDescriptor = fileno ( file) ; flockfile ( file) ; funlockfile ( file) ; int ch = getc ( stdin ) ; int ch = 'A' ; int result = putc ( ch, stdout ) ; int ch = getchar ( ) ; int ch = 'A' ; int result = putchar ( ch) ; while ( ! feof ( stdin ) ) { int ch = getc ( stdin ) ; if ( ch != EOF ) { } } int ch = getc ( stdin ) ; if ( ch == EOF ) { if ( ferror ( stdin ) ) { perror ( "读取标准输入流时发生错误" ) ; } } int ch = getc ( stdin ) ; if ( ch == EOF ) { clearerr ( stdin ) ; } return 0 ;
}