码迷,mamicode.com
首页 > 其他好文 > 详细

89 fcanf和fprintf

时间:2018-05-22 00:05:45      阅读:193      评论:0      收藏:0      [点我收藏+]

标签:size   fopen   输入格式   har   bsp   print   template   oid   result   

scanf、printf、putchar、getc、putc、fgets、fputs、sscanf、sprintf
fscanffprintf


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

标签:size   fopen   输入格式   har   bsp   print   template   oid   result   

原文地址:https://www.cnblogs.com/xiaodaxiaonao/p/9069973.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!