gpt4 book ai didi

c++ - 无法读取 C++ 中的文本文件

转载 作者:行者123 更新时间:2023-11-30 04:19:19 25 4
gpt4 key购买 nike

嗨,我的大学类(class)布置了一项小组作业,要求阅读多行文本文件,舍弃最低分,然后对剩余数字取平均分。当提示输入文本文件的文件名时,我遇到了问题。虽然我输入正确,但它不会加载。我将该文件放在程序所在的同一文件夹中。请帮助:

#include<iostream>
#include<fstream>
#include<string>
using namespace std;
const int NROWS =10;
const int NCOLS =10;

void theProgram();
//Function loads immediately upon opening the program and describes the purpose of the program
//and its functionality.

int ReadTxtFile(ifstream &file, string name[], double test[][NCOLS], int ncolUsed=3);
//Function reads data from a tab delimited file, whose first column is a string and the others are
//double or int. The function returns the number of rows read. The requires three parameters to be
//passed to it. The fourth parameter is has a default value of 3.

void outPutData(string header, string name[], double test[][NCOLS], int nDataPts, int ncolUsed=3);
//The function prints out the data read from the file. The function requires four parameters to be
//passed to it. Does not return any value.

int main(){

string name[NROWS];
string header, filename;
double test[NROWS][NCOLS];
char next;
int nDataRows;
ifstream file; //Declares file as an input file stream

theProgram(); //Invokes the function that displays the program information

cout<<"Please give me the filenames containing the test scores\n";
getline(cin,filename);

//Opens the file and checks if the file has opened correctly
file.open(filename);
if(file.fail()){
cout<<"Failed to open input file "<<filename<<endl;
exit(1);
}

getline(file,header);// Reads the column headers as a string
nDataRows=ReadTxtFile(file, name, test); // Calls the function to read the file
file.close();

cout<< "Number of records in the file is "<<nDataRows<<endl;
outPutData(header,name, test, nDataRows); //Calls the function to output the data read from the file


cin>>next;
} //End of main


void theProgram(){
cout<<"************************************************************************\n";
cout<<"* ******************************************************************** *\n";
cout<<"* *This program will help the faculty to analyze the test scores and * *\n";
cout<<"* *assign grades. It reads a tab delimited file that contains the * *\n";
cout<<"* *raw score for different tests and drops the lowest score. It then * *\n";
cout<<"* *calcluates the average percentage of the remaining test scores. * *\n";
cout<<"* *Based on the average, the program then assigns a letter grade. * *\n";
cout<<"* ******************************************************************** *\n";
cout<<"************************************************************************\n";
}

void outPutData( string header, string name[], double test[][NCOLS], int nDataRows, int ncolUsed)
{
cout<<"\t"<<header<<endl;
for(int i=0; i<nDataRows; i++){
cout<<i<<"\t"<<name[i]<<"\t";
for(int j=0; j<ncolUsed; j++){
cout<<test[i][j]<<"\t";
}
cout<<endl;
}
char next;
cin>>next;
}


int ReadTxtFile(ifstream &file, string name[], double test[][NCOLS], int ncolUsed)
{
int i=0;
char next;
while (!file.eof()) //repeat until end of file
{
file>>name[i];
for(int j=0; j<ncolUsed; j++){
file>>test[i][j];
}
file.get(next);
if(!file.eof()){
file.putback(next);
}
i++;
}
return(i-1);
}

最佳答案

在 C++11 之前,file.open() 接受一个 char*,它是包含要打开的文件名的 C 字符串,但是, filenamestring类型,需要做如下操作:

 file.open(filename.c_str());

为了从文件中读取。

编辑:感谢 Benjamin Lindley,您可以在 C++11 中将字符串传递给 open()。您可能需要检查从您的代码生成的 exe 文件是否可以访问您的文件,它们可能不在同一目录中。

关于c++ - 无法读取 C++ 中的文本文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15914839/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com