69,382
社区成员
发帖
与我相关
我的任务
分享
//d.txt为下面一行内容:( 去掉前面的// )
//01.2,3.45;5.6789 -0.12345678901234 abc 1234567890.123456789123456789 1e300 0
#include <stdio.h>
int n,r;
double d;
FILE *f;
void main() {
f=fopen("d.txt","r");
n=0;
while (1) {
r=fscanf(f,"%lf",&d);
if (1==r) {
n++;
printf("[%d]==%.15lg\n",n,d);//可以试试注释掉这句以后的速度
} else if (0==r) {
fgetc(f);
} else break;
}
fclose(f);
}
//实际运行输出:( 去掉前面的// )
//[1]==1.2
//[2]==3.45
//[3]==5.6789
//[4]==-0.12345678901234
//[5]==1234567890.12346
//[6]==1e+300
//[7]==0
FILE * in;
int c;
int num;
char numstr[10];
char *p;
scanf("%d",&c);
while((fscanf(in, "%s",numstr))!=EOF)//先按字符串读入{
if (p=strrchr(numstr, ',')) *p=0; //删除','
num=atoi(numstr);
if (c==num) //if (c=num)
{ printf("right ");}
else
{ printf("wrong ");}//{ printf("wrong";}
}
#include <cstdio>
#include <cstring>
#include <cstdlib>
int main()
{
FILE * in;
int c;
int num;
char str[128];
char *p;
if( ( in = fopen("file.txt","r") ) == NULL )
return -1;
scanf("%d",&c);
while((fscanf(in, "%s", str)) != EOF)// 先按字符串读入
{
if (p = strrchr(str, ','))
*p=0;
num = atoi(str);
printf("%d ", num);
if (c == num)
{
printf("right ");
break;
}
}
return 0;
}
/*
file.txt内容:
1234, 234, 44, 124, 542, 442, 135
212, 42, 11, 33, 233, 100, 123
输出:
1234 234 44 124 542 442 135 212 42 11 33 233 100 right
*/
#include <stdlib.h>
#include <stdio.h>
int main()
{
FILE *in=NULL;
if( ( in=fopen("d:\\text.txt","r") )==NULL )
{
return -1;
}
int get,find;
scanf("%d",&find);
while(true)
{
if( fscanf(in,"%d",&get)!=EOF )
{
if(get==find)
{
printf("right\n");
}
}
else
{
break;
}
if( fgetc(in)==EOF )
{
break;
}
}
fclose(in);
return 0;
}