C++ 无法读取c++;

C++ 无法读取c++;,c++,text-files,fstream,getline,C++,Text Files,Fstream,Getline,嗨,我的大学班级被分配了一个小组作业,阅读一个多行文本文件,去掉最低分数,然后从剩余的数字中取平均分数。当提示输入文本文件的文件名时,我遇到了一个问题。虽然我输入的正确,但无法加载。我把文件放在程序所在的文件夹中。请帮助: #include<iostream> #include<fstream> #include<string> using namespace std; const int NROWS =10; const int NCOLS =10; vo

嗨,我的大学班级被分配了一个小组作业,阅读一个多行文本文件,去掉最低分数,然后从剩余的数字中取平均分数。当提示输入文本文件的文件名时,我遇到了一个问题。虽然我输入的正确,但无法加载。我把文件放在程序所在的文件夹中。请帮助:

#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);
}
#包括
#包括
#包括
使用名称空间std;
常数int NROWS=10;
常数int NCOLS=10;
使程序无效();
//函数在打开程序时立即加载,并描述程序的用途
//以及它的功能。
int ReadTxtFile(ifstream&file,字符串名[],双重测试[][NCOLS],int ncolUsed=3);
//函数从制表符分隔的文件中读取数据,该文件的第一列是字符串,其他列是字符串
//double或int。该函数返回读取的行数。需要设置三个参数
//传给它。第四个参数的默认值为3。
void outPutData(字符串头,字符串名[],双重测试[][NCOLS],int-ndapts,int-ncolUsed=3);
//该函数打印从文件中读取的数据。该函数需要设置四个参数
//传给它。不返回任何值。
int main(){
字符串名[NROWS];
字符串头,文件名;
双重测试[NROWS][NCOLS];
下一步;
内坦达罗斯;
ifstream file;//将文件声明为输入文件流
theProgram();//调用显示程序信息的函数

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

 file.open(filename.c_str());
以便从文件中读取


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

您是否在IDE中运行程序?访问文件时,重要的不是文件相对于可执行文件的位置,而是相对于程序的工作目录的位置。IDE通常使用与可执行文件所在的目录不同的工作目录。我正在Visual Studio Pro 2012中运行它。我还在命令行中编译了它,以查看是否有差异。收到了相同的结果。从命令行编译?或从命令行运行程序?试着使用。它是否提供了与y相同的目录我们的文件位于?@jstacy00尝试运行您的程序并指定文件的绝对路径(如
C:\path\to\file
)。如果这样做有效,那么您就知道这是因为工作目录不是您所期望的。您可以向需要读取的文件内容显示一个示例吗?我认为错误在
ReadTxtFile
函数逻辑中,而不是文件的给定路径中。在C++11中,您可以直接传递字符串。而且,由于他似乎在描述运行时问题,所以没有“这不是编译时的问题,似乎他的编译器的标准库支持该功能。@BenjaminLindley,谢谢,很高兴知道这一点。那么可能exe或对象文件无法读取该文件。谢谢。我刚刚尝试过,但仍然收到同样的问题。@user2264018您可以尝试打开并从
ReadTextF读取。”ile
仅限,不在main中打开它,然后从该函数中读取?您也可以尝试使用命令行运行exe,而不是从IDE中运行代码?@jstacy00您可以尝试的最后一件事是确保文件名中不包含空格,即,假设您键入“test.txt”,但驱动器上的实际文件是“test.txt”。