89 fcanf和fprintf
Posted 随意就好欧巴
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了89 fcanf和fprintf相关的知识,希望对你有一定的参考价值。
scanf、printf、putchar、getc、putc、fgets、fputs、sscanf、sprintf
fscanf和fprintf
fgets 从屏幕上获取输入(stdin)或者从文件中获取输入
fputs
输入到屏幕上(stdout)或者输出到文件中.
scanf 从屏幕上的输入格式化分离.
printf
输出到屏幕上.
sscanf 从字符串中格式化分离.
sprintf 输出到字符串中.
fscanf
从文件中格式化分离.
fprintf
输出到文件中.
fscanf
%d%c%d可能导致的bug
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int count(int one, int tow, char symbol) {
switch (symbol) {
case ‘+‘:
return one + tow;
case ‘-‘:
return one - tow;
case ‘*‘:
return one * tow;
case ‘/‘:
if (!tow) {
return 0;
}
else { return one / tow; }
}
}
void main() {
FILE*read = fopen("1", "r");
if (!read) {
printf("文件为空");
system("pause");
return;
}
FILE*write = fopen("1", "w");
int kong = NULL;
int xiaomiao = NULL;
while (!feof(read)) {
int one; int tow; char symbol;
fscanf(read, "%d%c%d=\n", &one, &symbol, &tow);
int shaomiao = ftell(read);
if (shaomiao != kong) {
int result = count(one, tow, symbol);
fprintf(write, "%d%c%d=%d\n", one, symbol, tow, result);
}
}
fclose(read);
fclose(write);
system("pause");
}
#include<stdio.h>
#include<stdlib.h>
void main() {
FILE*read = fopen("1.txt", "r");
if (!read) {
printf("文件为空");
system("pause");
return;
}
int kong = NULL;
int xiaomiao = NULL;
while (!feof(read)) {
char name[20] = { NULL };
int age = NULL;
fscanf(read, "%[^,],%d\n",&name,&age );
int shaomiao = ftell(read);
if (shaomiao != kong) {
printf( "名字%s,年龄%d\n",name,age);
}
}
fclose(read);
system("pause");
}
以上是关于89 fcanf和fprintf的主要内容,如果未能解决你的问题,请参考以下文章
AT89C51,AT89C52等单片机在Altium的啥库中?