从文本文件里读取数字,c语言怎么实现

发布于2021-12-26 18:15:25
8个回答
admin
网友回答2021-12-26
设数据文件名为123.txt且在当前目录下。利用fscanf函数的选择功能,只读取文件中的int型数据,“跳过”非数字字符即能完成题面要求。举例代码如下:
//#include stdafx.h//If the vc++6.0, with this line.#include stdio.h#include stdlib.hint main(void){ FILE *fp; int ln=0,*p,x; if((fp=fopen(123.txt,r))==NULL){ printf(Open the file failure...\n); exit(0); } while(fscanf(fp,%d%*[^0123456789],&x)==1 && x!=100);//找到100位置 if(feof(fp)){//未找到100则退出 printf(Starting point number not found...\n); exit(0); } while(fscanf(fp,%d%*[^0123456789],&x)==1)//检测100后有多少个数,计入ln ln++; if((p=(int *)malloc((sizeof(int)+1)*ln))==NULL){//创建动态数组 printf(Application memory failure...\n); exit(0); } rewind(fp);//文件指针移到开头,开始正式读文件 while(fscanf(fp,%d%*[^0123456789],&x)==1 && x!=100);//跳过100前的数据 p[0]=100,x=1; while(fscanf(fp,%d%*[^0123456789],p+x++)==1);//数据读入p[1]及以后单元 fclose(fp); for(x=0;x=ln;printf(%d ,p[x++]));//打出来看看... printf(\n); free(p); return 0;}
admin
网友回答2021-12-26
有个格式读取函数
admin
网友回答2021-12-26
从文本文件读取的字符串:
1:100,200,101,200,102,200
------------------------------------
截取字符串转为int,用数组存放。
100,200,101,200,102,200
int数组内容为:
num[0] = 100 num[1] = 200 num[2] = 101 num[3] = 200 num[4] = 102
Press any key to continue
data.txt文件内容为:1:100,200,101,200,102,200

#include stdio.h
#include conio.h
#include stdlib.h
int main()
{
FILE *fp;
char *pchBuf,tmp[5];
int nLen,cnt=0;
int i,j=0,k=0,num[8];
fp = fopen (data.txt,r);

fseek(fp, 0, SEEK_END); //文件指针移到文件尾
nLen = ftell(fp); //得到当前指针位置, 即是文件的长度
//fseek(fp, 2, SEEK_SET); //文件指针移到文件尾
rewind(fp); //文件指针恢复到文件头位置

//动态申请空间, 为保存字符串结尾标志\0, 多申请一个字符的空间
pchBuf = (char*) malloc(sizeof(char)*nLen+1);
if(!pchBuf)
{
perror(内存不够!\n);
exit(0);
}
//读取文件内容//读取的长度和源文件长度有可能有出入,这里自动调整 nLen
nLen = fread(pchBuf, sizeof(char), nLen, fp);
fclose(fp);
pchBuf[nLen] = '\0'; //添加字符串结尾标志
printf(从文本文件读取的字符串:\n);
printf(%s\n, pchBuf); //把读取的内容输出到屏幕看看
printf(------------------------------------\n);
printf(截取字符串转为int,用数组存放。\n);
for (i=2;inLen;i++)
{
printf(%c,*(pchBuf+i));
tmp[j++]=*(pchBuf+i);
if (*(pchBuf+i)==',' ||*(pchBuf+i)=='\0' )
{
tmp[j] = '\0';
cnt++;
//printf(%s,tmp);
num[k++]=atoi(tmp);
j=0;
}
}
free(pchBuf);
printf(\nint数组内容为:\n);
for (k=0;kcnt;k++)
{
printf(num[%d] = %d ,k,num[k]);
}
printf(\n);
return 0;
}
admin
网友回答2021-12-26
void main()
{
FILE *fin;
int a,b,c,d;
char s[20];
float f1,f2,f3,f4;
fin = fopen(abc.txt,r); // 打开
fscanf(fin,%1d%1d%1d%1d,&a,&b,&c,&d); // 按1位整型读前4个数
printf(a=%d b=%d c=%d d=%d\n,a,b,c,d);
rewind(fin); // 文件回绕到开始处
fscanf(fin,%s,s); // 按字符串读一串
printf(s=%s\n,s);
rewind(fin); // 文件回绕到开始处
fscanf(fin,%1f%1f%1f%1f,&f1,&f2,&f3,&f4); // 按1位float型读前4个数
printf(%f %f %f %f\n,f1,f2,f3,f4);
flcose(fin);
}
abc.txt 内容:
123456789

读到数组:
int y[20];
int i;
fin = fopen(abc.txt,r);
for (i=0;i9;i++) fscanf(fin,%1d,&y[i]);
admin
网友回答2021-12-26
string str_line;ofstream in(文本文件的路径); while( getline(in, str_line) );循环结束后str_line就是最后一行字符串了,你可以再用其他类型截取你要的数据数据
admin
网友回答2021-12-26
void main()
{
FILE *fin;
int a,b,c,d;
char s[20];
float f1,f2,f3,f4;
fin = fopen(abc.txt,r); // 打开
fscanf(fin,%1d%1d%1d%1d,&a,&b,&c,&d); // 按1位整型读前4个数
printf(a=%d b=%d c=%d d=%d\n,a,b,c,d);
rewind(fin); // 文件回绕到开始处
fscanf(fin,%s,s); // 按字符串读一串
printf(s=%s\n,s);
rewind(fin); // 文件回绕到开始处
fscanf(fin,%1f%1f%1f%1f,&f1,&f2,&f3,&f4); // 按1位float型读前4个数
printf(%f %f %f %f\n,f1,f2,f3,f4);
flcose(fin);
}
abc.txt 内容:
123456789

读到数组:
int y[20];
int i;
fin = fopen(abc.txt,r);
for (i=0;i9;i++) fscanf(fin,%1d,&y[i]);
另外,站长团上有产品团购,便宜有保证
admin
网友回答2021-12-26
打开文件
顺序读取
以逗号分隔,将ASCII转换为整数,保存到数组
admin
网友回答2021-12-26
StreamReader sr=new StreamReader (TXT的路径。);while (true)
{
string str = sr.ReadLine();
if (String.IsNullOrEmpty(str))
{
break;
}
Console.WriteLine(str);
}

回到
顶部