Trouble opening text file?
Mar 23, 2012 at 8:59pm UTC
I cannot get my file to open a text file to read the values
Here is the error:
'wordbench.cpp: In member function ‘bool WordBench::ReadText(const fsu::String&)’:
wordbench.cpp:29: error: no matching function for call to ‘std::basic_ifstream<char, std::char_traits<char> >::open(const fsu::String&)’
/usr/local/lib/gcc/x86_64-unknown-linux-gnu/4.3.2/../../../../include/c++/4.3.2/fstream:495: note: candidates are: void std::basic_ifstream<_CharT, _Traits>::open(const char*, std::_Ios_Openmode) [with _CharT = char, _Traits = std::char_traits<char>]
Here is the main.cpp file provided by the instructor:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
#include <wordbench.h>
#include <cstdlib>
#include <cctype>
#include <iostream>
#include <fstream>
#include <xstring.h>
void DisplayMenu ();
int main( int argc, char * argv[] )
{
bool BATCH = 0;
if (argc > 1) BATCH = 1;
std::istream* isptr = &std::cin;
std::ifstream ifs;
if (BATCH)
{
ifs.open(argv[1]);
if (ifs.fail())
{
std::cout << " ** Error: cannot open file " << argv[1] << '\n' ;
return 0;
}
isptr = &ifs;
}
std::cout << "Welcome to WordBench, a Text Analysis Utility.\n" ;
if (!BATCH) DisplayMenu();
WordBench ws;
char selection;
fsu::String filename;
do
{
std::cout << "\nWS command ('m' for menu, 'q' to exit): " ;
*isptr >> selection;
if (BATCH) std::cout << selection << '\n' ;
switch (selection)
{
case 'r' : case 'R' :
std::cout << " Enter file name : " ;
*isptr >> filename;
if (BATCH) std::cout << filename << '\n' ;
while (!ws.ReadText(filename))
{
std::cout << " ** Cannot open file " << filename << '\n'
<< " Try another file name: " ;
*isptr >> filename;
if (BATCH) std::cout << filename << '\n' ;
}
break ;
case 'w' : case 'W' :
std::cout << " Enter file name: " ;
*isptr >> filename;
if (BATCH) std::cout << filename << '\n' ;
while (!ws.WriteReport(filename))
{
std::cout << " ** Cannot open file " << filename << '\n'
<< " Try another file name: " ;
*isptr >> filename;
if (BATCH) std::cout << filename << '\n' ;
}
break ;
case 'e' : case 'E' :
ws.Erase();
std::cout << "\n Current data erased\n" ;
break ;
case 's' : case 'S' :
ws.ShowSummary();
break ;
case 'm' : case 'M' :
DisplayMenu();
break ;
case 'x' : case 'X' :
if (BATCH)
{
std::cout << " switching to interactive mode\n" ;
isptr = &std::cin;
ifs.close();
BATCH = 0;
DisplayMenu();
}
else
{
std::cout << " already in interactive mode\n" ;
}
break ;
case 'q' : case 'Q' :
if (BATCH)
ifs.close();
selection = 'q' ;
break ;
default :
std::cout << "\n ** Command not found -- try again\n" ;
}
}
while (selection != 'q' );
std::cout << "\nWordBench wishes you a nice day." << std::endl;
return EXIT_SUCCESS;
} // end main()
void DisplayMenu()
{
std::cout << '\n'
<< " WS Command key\n"
<< " ---------- ---\n"
<< " Read a file ................ 'r'\n"
<< " show Summary ............... 's'\n"
<< " Write report ............... 'w'\n"
<< " Erase current data ......... 'e'\n"
<< " eXit BATCH mode ............ 'x'\n"
<< " display Menu ............... 'm'\n"
<< " Quit program ............... 'q'\n" ;
}
Wordbench.h:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
#include <xstring.h>
#include <olist.h>
#include <ovector.h>
#include <compare.h>
#include <pair.h>
#include <list.h>
#include <vector.h>
#include <iomanip>
//class WordBench;
class WordBench
{
private :
// the internal class terminology:
typedef fsu::Pair < fsu::String, unsigned long > EntryType;
typedef fsu::LessThan < EntryType > PredicateType;
public :
WordBench();
virtual ~WordBench();
// choose one associative container class for SetType:
//typedef fsu::UOList < EntryType , PredicateType > SetType;
typedef fsu::MOList < EntryType , PredicateType > SetType;
// typedef fsu::UOVector < EntryType , PredicateType > SetType;
// typedef fsu::MOVector < EntryType , PredicateType > SetType;
// typedef fsu::RBLLT < EntryType , PredicateType > SetType;
// declare the two class variables:
SetType wordset_;
fsu::List < fsu::String > infiles_;
bool ReadText (const fsu::String& );
bool WriteReport (const fsu::String& outfile, unsigned short c1 = 15, unsigned short c2 = 15) const ;
void ShowSummary () const ;
void Erase ();
};
#endif
Wordbench.cpp:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
#include <iostream>
#include <wordbench.h>
#include <fstream>
#include <string>
WordBench::WordBench()
{
}
WordBench::~WordBench()
{
}
bool WordBench::ReadText(const fsu::String& infile)
{
std::string line;
std::ifstream dataIn;
dataIn.open(infile);
/* if (infile.is_open())
{
// while (myfile.good())
//{
// getline(myfile, line);
// std::cout << line << std::endl;
//}
//myfile.close();
}*/
std::cout <<"ReadText " << std::endl;
return true ;
}
bool WordBench::WriteReport(const fsu::String& outfile, unsigned short c1, unsigned short c2) const
{
}
void WordBench::ShowSummary() const
{
}
void WordBench::Erase()
{
}
You can kinda ignore most of the stuff under the ReadText file, I tried several different things and changed stuff so the stuff that's there is bits and pieces of other stuff.
Mar 24, 2012 at 1:23am UTC
¿What you don't understand of the message? You need to pass a const char *
as argument to open()
Topic archived. No new replies allowed.