I'm having a very frustrating time getting what should be a simple program to work. For now, I simply want .cpp and .h files to be recognised by being read in from another text file. My code is below.
#include <iostream>
#include <cstdlib>
#include <fstream>
#include <string>
using namespace std;
void newInput(const char* entry)
{
cout << entry << endl;
fstream currentFile (entry);
if(!currentFile)
{
cout << "No file to be read. Please check file location. Program shall now exit.\n" << endl;
exit(1);
}
else
{
cout << "File Input" << endl;
}
currentFile.close();
}
int main(int argc, char *argv[])
{
if (argc != 2)
{
cout << "Incorrect execution - please enter a text file (only one) next time you run the program. Program shall now exit.\n" << endl;
return 0;
}
fstream myFile (argv[1]);
string line;
bool eofFound = false;
if(!myFile)
{
cout << "No file to be read. Please check file location. Program shall now exit.\n" << endl;
return 0;
}
while (!myFile.eof())
{
//The file is read in line by line, until the end of file
getline (myFile,line);
//If it's not the EOF line, the file is input
if (line != "EOF")
{
newInput(line.data());
}
//If the line is the EOF line, we know that it is the end of the file
else if (line == "EOF")
{
eofFound = true;
}
}
//If no 'EOF' is found, then an error is displayed and the program exits
if (!eofFound)
{
cout << "No EOF line in file - please check your input, and try again. Program shall now exit.\n";
return 0;
}
//closes the file
myFile.close();
cout << "\nProgram finished." << endl;
return EXIT_SUCCESS;
}
I provide a text file that simply has
Test.cpp
EOF
as the input file the text file is provided as the second argument when the program is executed. I've attatched it with the post.
My problem is that where I want the file to be read, i.e. in the newInput method, the file isn't recognised, although it should be. Please try the program out for yourself to see what I mean. Is there something I'm doing wrong? Thank you very much in advance.
files.txt (13bytes)
Number of downloads: 41

New Topic/Question
Reply




MultiQuote





|