my globals.h file
#include <iostream> #include <fstream> #include <string> #ifndef globals_h #define globals_h // PRE: none // POST: displays greeting and describes program std::string greeting(); // PRE: Command line arguments have been specified // POST: returns true if command line arguments are specified bool commandline_check(unsigned); // PRE: Filestream and string reference // POST: Reads data from a text file and puts it in a string bool read_data(std::ifstream&, std::string&); #endif
My globals.cpp file
#include "globals.h"
#include <iostream>
#include <fstream>
#include <string>
std::string greeting()
{
std::cout << " This program will parse through a text file and give the frequencies of how often each letter appears in the text file. Please press enter to continue.\n";
std::cin.ignore();
return 0;
}
bool commandline_check(unsigned x)
{
if( x < 2)
{
std::cerr << "Please specify a text file to be used with this programs.\n";
return (1);
}
else
{
return true;
}
}
bool read_data(std::ifstream& in, std::string& text_string)
{
if(!in)
{
std::cerr << "Please check file and try again.\n";
return false;
}
else
{
getline(in, text_string);
std::cout << text_string << '\n';
in.close();
return true;
}
}
My main driver .cpp file
#include "globals.h"
#include <iostream>
#include <fstream>
#include <string>
int main(int argc, char* argv[])
{
greeting();
commandline_check(argc);
std::ifstream infile(argv[1], std::ios::in);
/*if(!infile)
{
std::cerr << "Please check file and try again." << '\n';
}*/
std::string original_string("intialized");
read_data(infile, original_string);
return 0;
}
The error I get on run time
./a.out testfile This program will parse through a text file and give the frequencies of how often each letter appears in the text file. Please press enter to continue. terminate called after throwing an instance of 'std::logic_error' what(): basic_string::_S_construct NULL not valid Aborted
Other notes: I am using the g++ compiler in Linux. Thanks for your help in advanced!

New Topic/Question
Reply



MultiQuote




|