当前位置: 代码迷 >> 综合 >> 文件操作命令以及scanf fscanf sscanf printf fprintf sprintf day19
  详细解决方案

文件操作命令以及scanf fscanf sscanf printf fprintf sprintf day19

热度:87   发布时间:2023-11-26 18:08:29.0

scanf: 从标准输入流  读取格式化的数据、

fscanf: 从所有的输入流 读取格式化的数据

sscanf:从字符串中读取一个格式化的数据或者 把一个字符串转成一个格式化的数据

printf:把格式化的数据  输出到标准输出上

fprintf:把格式化的数据  输出到所有输出流上

sprintf:把格式化的数据转化成对应的字符串:

fputc 将字符a的内容放到data.txt文件中

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include<stdlib.h>
#include<string.h>
int main() {FILE* ps = fopen("data.txt", "w");char a = 'A';for (a = 'A'; a <= 'Z'; a++){fputc(a, ps);}fclose(ps);ps = NULL; system("pause");return 0;
}

fgetc 将刚才放入的内容读出来;fgetc 得到的是ascii

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include<stdlib.h>
#include<string.h>int main() {FILE* ps = fopen("data.txt", "r");int ch;ch = fgetc(ps);printf("%c\n", ch);ch = fgetc(ps);printf("%c\n", ch);ch = fgetc(ps);printf("%c\n", ch);ch = fgetc(ps);printf("%c\n", ch);ch = fgetc(ps);printf("%c\n", ch);fclose(ps);ps = NULL; system("pause");return 0;
}

fputs   将写入的字符串放入文件中 将pc的内容 写入到 ps中去

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include<stdlib.h>
#include<string.h>int main() {FILE* ps = fopen("data.txt", "w");char pc[] = "dsfds";fputs(pc, ps);fclose(ps);ps = NULL; system("pause");return 0;
}

fgets  将ps的内容在读出来

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include<stdlib.h>
#include<string.h>int main() {FILE* ps = fopen("data.txt", "r");char pc[20] = {0};//这个地方不能这样charpc[]={0};这样的话会跳出stack corruptfgets(pc,5,ps);  //这个地方如果写入5 那么只能读取四个字符 因为最后他要加入一个'\0';printf("%s\n", pc);fclose(ps);ps = NULL; system("pause");return 0;
}

sprintf   将信息输入到文件中 

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include<stdlib.h>
#include<string.h>struct stu {int a;char b;};
int main() {struct stu s = { 8,'c' };FILE* ps = fopen("data.txt", "w");fprintf(ps, "%d %c", s.a, s.b);  //简单理解为就是之前我们printf都是打印到屏幕 现在打印到了文件fclose(ps);ps = NULL; system("pause");return 0;
}

sscanf  将文件中的内容读出来

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include<stdlib.h>
#include<string.h>struct stu {int a;char b;};
int main() {struct stu s = {0};FILE* ps = fopen("data.txt", "r");fscanf(ps, "%d %c", &(s.a), &(s.b));//都给谁后面就写谁的地址printf("%d %c", s.a, s.b);fclose(ps);ps = NULL; system("pause");return 0;
}

二进制的读写 fwrite fread

fwrite

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include<stdlib.h>
#include<string.h>
#include<errno.h>
struct stu {int a;int b;char name[10];};
int main() {struct stu s = {1,1,"dada"};FILE* ps = fopen("data.txt", "wb");if(ps==NULL)
{   strerror(errno);
}fwrite(&s, sizeof(struct stu), 1, ps);fclose(ps);ps = NULL; system("pause");return 0;
}

fread

  

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include<stdlib.h>
#include<string.h>struct stu {int a;int b;char name[10];};
int main() {struct stu s = {1,1,"dada"};FILE* ps = fopen("data.txt", "rb");fread(&s, sizeof(struct stu), 1, ps);printf("%d %d %s\n", s.a, s.b, s.name);fclose(ps);ps = NULL; system("pause");return 0;
}

  相关解决方案