举个例子: 从文件e.txt中读取第一行内容并统计第一行有多少个数字
#include
#include
int main()
{
int count=0; //统计数字个数
char StrLine[1024]; //每行最大读取的字符数
FILE * fp = fopen("e.txt", "r");
if(!fp){
printf("The file is not exist!");
return -1;
}
fgets(StrLine,1024,fp); //读取一行
printf("第1行内容为: %s", StrLine); //输出
for(int i=0; i{
if(int(StrLine[i])>=48 && int(StrLine[i])<=57)
count++;
}
printf("第一行共有%d个数字!\n", count);
fclose(fp);
return 0;
}
文件e.txt中的内容如下:
程序运行结果如下:
#include
#include
using namespace std;
int main()
{
ifstream in("1.txt");//打开文件1.txt
char s[100];
in.getline(s,sizeof(s));//读取文件第一行内容,并存入s中
int b = strlen(s); //得到字符串s的长度
int i = 1;
cout<<"文本中第一行的内容是:\n";
for(int j = 0; j < b; j++) //输出s的内容
{
if(s[j]=='\40') //遇到一个空格i++,这里以空格数判断数字个数
i++;
cout<}
cout<cout<<"第一行共有"< system("pause");//暂停
return 0;
}
double number;
int count = 0;
if(number>=0&&number<=100)
{
count ++;
}
printf(count);