c语言读取文件的问题。新手求帮助!

C语言 码拜 8年前 (2016-02-03) 740次浏览
新手一枚,求高手告知这段代码的具体意思。
int Read_Num(FILE *fp)
{
char temp[8];
int i=0;
int stop=0;
char ch;
while(!stop)
{
ch=fgetc(fp);
if(ch==EOF)
break;
while(ch>=””0″”&&ch<=””9″”)
{
stop=1;
temp[i++]=ch;
ch=fgetc(fp);
}
}
temp[i]=0;
return atol(temp);
}
解决方案:40分
注释写在代码里了,相当于从一个文件里提取出碰到的第一组的连续的数字
例如一个文件:
asd2358395bb
12312asd
运行结果:提取出的是2358395

#include <iostream>
using namespace std;
int Read_Num(FILE *fp) 
{
	char temp[8]; //用来存储读到的数字字符
	int i=0; //用来记录当前字符数组的位置
	int stop=0; //标志能否读到了数字(仅读一组数字)
	char ch; //记录当前读到的字符
	while(!stop)
	{
		ch=fgetc(fp); //读取一个字符
		if(ch==EOF)    //判断能否读到了文件尾
			break;
		while(ch>=""0""&&ch<=""9"")//读到的能否是一个数字字符
		{
			stop=1;//标志位置1
			temp[i++]=ch;//当前数字字符存到数组里,然后i+1
			ch=fgetc(fp);//继续读取字符
		}
	}
	temp[i]=0;//在当前数组最后填0
	return atol(temp);//将字符串转为长整数并返回
}
int main()
{
	FILE *fp;
	fp = fopen("E:\out_1.txt", "r");
	cout << Read_Num(fp) << endl;
	system("pause");
	return 0;
}

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明c语言读取文件的问题。新手求帮助!
喜欢 (0)
[1034331897@qq.com]
分享 (0)