c 读写文件
c 读文件
#include <stdio.h>
#include <string.h>
int main()
{
FILE *fp = NULL;
char buff[255];
memset(buff, 0, sizeof(buff));
fp = fopen("in.txt", "r");
fscanf(fp, "%s", buff);
printf("1: %s\n", buff);
memset(buff, 0, sizeof(buff));
fgets(buff, 255, fp);
printf("2: %s\n", buff );
memset(buff, 0, sizeof(buff));
fgets(buff, 255, fp);
printf("3: %s\n", buff );
size_t size_of_elements=1;
size_t number_of_elements = 20;
memset(buff, 0, sizeof(buff));
size_t cnt = fread(buff, size_of_elements, number_of_elements, fp);
printf("%d\n", cnt);
printf("4: %s\n", buff);
fclose(fp);
return 0;
}
c 写文件
#include <stdio.h>
int main()
{
FILE *fp = NULL;
fp = fopen("/tmp/test.txt", "w+");
fprintf(fp, "This is testing for fprintf...\n");
fputs("This is testing for fputs...\n", fp);
fclose(fp);
}
二进制 I/O 函数 下面两个函数用于二进制输入和输出:
size_t fread(void *ptr, size_t size_of_elements, size_t number_of_elements, FILE *a_file);
size_t fwrite(const void *ptr, size_t size_of_elements, size_t number_of_elements, FILE *a_file);