1

Hi my college class was given a group assignment to read a multi-line text file, drop the lowest score, then average the score from the remaining numbers. I've run into a problem when prompted for the filename of the text file. Although I type it in correctly, it won't load. I have the file in the same folder that the program is located. Please help:

#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);
}
6
  • 3
    Are you running the program inside an IDE? When accessing files, what matters isn't where the file is relative to the executable, but where it is relative to the working directory of the program. IDEs often use a different working directory than the directory the executable is in. Commented Apr 10, 2013 at 0:36
  • I'm running it in Visual Studios Pro 2012. I also compiled it in the command line to see if that made a difference. Received the same results. Commented Apr 10, 2013 at 0:45
  • 1
    Compiling from the command line? Or running your program from the command line? Try using the _getcwd. Does it give the same directory that your file is in? Commented Apr 10, 2013 at 1:12
  • 1
    @jstacy00 try running your program and specifying the absolute path to the file (as in C:\path\to\file). If that works then you know its because the working directory isn't what you expect. Commented Apr 10, 2013 at 1:37
  • Can you show a sample to file content that you need to read. I think the error is in your ReadTxtFile function logic, not the giving path of the file. Commented Apr 10, 2013 at 2:05

1 Answer 1

2

Before C++11, file.open() accepts a char*, which is C-string containing the name of the file to be opened , however, filename is of string type, you need to do the following:

 file.open(filename.c_str());

in order to read from the file.

EDIT: thanks to Benjamin Lindley, you can pass string to open() in C++11. You may need to check the exe file generated from your code can access your file, they may not be in same directory.

Sign up to request clarification or add additional context in comments.

7 Comments

In C++11, you can pass strings directly. And since he seems to be describing a run-time problem, not a compile time problem, it would appear that his compiler's standard library supports that feature. en.cppreference.com/w/cpp/io/basic_ifstream/open
@BenjaminLindley thanks, good to know that. Then probably the exe or object file may not be able to read the file.
Thanks. I just tried that, however I'm still receiving the same issue.
@user2264018 you may try to open and read from ReadTextFile only and not open it in main then read from that functino? You may also try to run exe using command line instead of running the code from IDE?
@jstacy00 one last thing you may try is to make sure your file does not contains space in file name, i.e., assume you type "test.txt" but your real file on the drive is "test .txt".
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.