C++怎么读取txt文件并且统计里面的数字:求详解
老师给的作业是打开一个random.txt:100200300400500600700用c++统计这个txt中有几个数字(7个),并且计算总和和平均值我写的程序:#inc...
老师给的作业是打开一个random.txt:
100
200
300
400
500
600
700
用c++统计这个txt中有几个数字(7个),并且计算总和和平均值
我写的程序:#include <fstream>
using namespace std;
int main()
{
ifstream inputFile;
string filename = "random.txt";
int number=0;
//number of numbers
int numbers,sum,average;
// Open the file.
inputFile.open(filename.c_str());
// If the file successfully opened, process it.
if (inputFile)
{
while (inputFile >> numbers)
{
inputFile >> numbers;
number++;
sum=sum+numbers;
}
average = sum / number;
//get average and display all:
cout << "number of numbers: "<<number<<"\n";
cout << "sum of numbers : "<<sum<<"\n";
cout << "average of numbers: "<< average<<"\n";
}
else
{
// Display an error message.
cout << "Error opening the file.\n";
}
system("pause");
return 0;
}
可以调试和生成exe,但是运行出来结果是这样的: 展开
100
200
300
400
500
600
700
用c++统计这个txt中有几个数字(7个),并且计算总和和平均值
我写的程序:#include <fstream>
using namespace std;
int main()
{
ifstream inputFile;
string filename = "random.txt";
int number=0;
//number of numbers
int numbers,sum,average;
// Open the file.
inputFile.open(filename.c_str());
// If the file successfully opened, process it.
if (inputFile)
{
while (inputFile >> numbers)
{
inputFile >> numbers;
number++;
sum=sum+numbers;
}
average = sum / number;
//get average and display all:
cout << "number of numbers: "<<number<<"\n";
cout << "sum of numbers : "<<sum<<"\n";
cout << "average of numbers: "<< average<<"\n";
}
else
{
// Display an error message.
cout << "Error opening the file.\n";
}
system("pause");
return 0;
}
可以调试和生成exe,但是运行出来结果是这样的: 展开
展开全部
1.C语言读取TXT文件的行数并把把TXT文件的内容存到数组中,需要根据每行的内容(字符,数字,字符串)选取fgets、fscanf或者fgets。
这里假设每行是一个字符串,每行不超过127个字符(用数组存字符串,最后一位存'\0'),选取fgets进行读取。
2.示例代码如下:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char* argv) {
FILE* rf = fopen("input.txt","r");
char a[1024][128];
int i=0;
while(fgets(a[i],128,rf))i++;
int n=i;
printf("行数:%d\n",n);
fclose(rf);
i=0;
while(i<n)printf("%s",a[i++]);
system("pause");
return 0;
}
展开全部
sum 没有初始化。
#include <fstream>
using namespace std;
int main()
{
ifstream inputFile;
string filename = "random.txt";
int number=0;
//number of numbers
int numbers,average;
int sum=0; //sum要初始化,否则是个不可预料的值
// Open the file.
inputFile.open(filename.c_str());
// If the file successfully opened, process it.
if (inputFile)
{
while (inputFile >> numbers)
{
inputFile >> numbers;
number++;
sum=sum+numbers;
}
average = sum / number;
//get average and display all:
cout << "number of numbers: "<<number<<"\n";
cout << "sum of numbers : "<<sum<<"\n";
cout << "average of numbers: "<< average<<"\n";
}
else
{
// Display an error message.
cout << "Error opening the file.\n";
}
system("pause");
return 0;
}
#include <fstream>
using namespace std;
int main()
{
ifstream inputFile;
string filename = "random.txt";
int number=0;
//number of numbers
int numbers,average;
int sum=0; //sum要初始化,否则是个不可预料的值
// Open the file.
inputFile.open(filename.c_str());
// If the file successfully opened, process it.
if (inputFile)
{
while (inputFile >> numbers)
{
inputFile >> numbers;
number++;
sum=sum+numbers;
}
average = sum / number;
//get average and display all:
cout << "number of numbers: "<<number<<"\n";
cout << "sum of numbers : "<<sum<<"\n";
cout << "average of numbers: "<< average<<"\n";
}
else
{
// Display an error message.
cout << "Error opening the file.\n";
}
system("pause");
return 0;
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询