# Thread: File I/O Issue

1. ## File I/O Issue

I am trying to open() a user specified .dat file. No matter how I enter the path to the file, my attempts always fail(). I've seen this technique work successfully here, but doesn't seem to work in my program.

Here is the entire code.

Here is the offending code:
Code:
void FileReader::file_count()
{
bool match;

string temp;
int back = ignored_words.size();

cout << "\n\n\tExample:  C:\\myfile.txt"
<< "\n\t-------"
<< "\n\n\tEnter the path to your file to count: ";
cin	 >> temp;

infile.open(temp.c_str(),ios::binary);

if(infile.fail())   //Any attempt to open always fails
{
cout << "\n\n\t\aCannot Locate File -OR- File Does Not Exist!"
<< "\n\n\tPress [ENTER] to continue..."; [/color]

cin.ignore();
cin.get();

return;
}

As you can see, I've experimented with the ios bitflags to see if I could get any results but to no avail.

The example I am using is a file called 'text.dat' which is located in my C:\ folder.. so when prompted by the program (menu option #2) I am entering c:\text.dat

Here are the program specification:

A C++ program to read a file (designated by the user) and count the occurrences and frequency of the words in the file. You are to ignore all white spaces and punctuation (still waiting on this part until I can get file I/O working)

The program should ask the user for the file name. It should also read a file- ignore.dat which contains a list of words (one per line) to ignore in the counting process. The output of your program should be each word, the number of occurrences, and the percentage of occurrences, displayed in descending order of frequency. (Although this is a homework-like exercise, I assure you it is not homework.. just working on book exercises on my own for personal fun)

General Strategy:

Load a vector of strings with 'ignore' words. Read user specified file word at a time. Compare to vector of ignores. IF no match, increment word counter. IF match, try again. "Vector of strings" strategy is based on this google search.

Program compiles ok, but with these warnings.. if anyone has any suggestions on how to correct these warnings i'd be much greatful.

--------------------Configuration: filereader - Win32 Debug--------------------
Compiling...
c:\program files\microsoft visual studio\vc98\include\vector(39) : warning C4786: 'std::reverse_iterator<std::basic_string<char,std: :char_traits<char>,std::allocator<char> > const *,std::basic_string<char,std::char_traits<char>,st d::allocator<char>
>,std::basic_string<char,std::char_traits<char>,st d::allocator<char> > const &,std::basic_string<char,std::char_traits<char>,st d::allocator<char> > const *,int>' : identifier was truncated to '255' characters in the debug information
c:\program files\microsoft visual studio\vc98\include\vector(39) : while compiling class-template member function '__thiscall std::vector<std::basic_string<char,std::char_trait s<char>,std::allocator<char> >,std::allocator<std::basic_string<c
har,std::char_traits<char>,std::allocator<char> > > >::std::vector<std::basic_string<char,std::char_tr aits<char>,std::allocator<char> >,std::allocator<std::basic_string<char,std::char_ traits<char>,std::allocator<char> > > >(const std::allocator<std:
:basic_string<char,std::char_traits<char>,std::all ocator<char> > > &)'
c:\program files\microsoft visual studio\vc98\include\vector(39) : warning C4786: 'std::reverse_iterator<std::basic_string<char,std: :char_traits<char>,std::allocator<char> > *,std::basic_string<char,std::char_traits<char>,st d::allocator<char> >,std:
:basic_string<char,std::char_traits<char>,std::all ocator<char> > &,std::basic_string<char,std::char_traits<char>,st d::allocator<char> > *,int>' : identifier was truncated to '255' characters in the debug information
c:\program files\microsoft visual studio\vc98\include\vector(39) : while compiling class-template member function '__thiscall std::vector<std::basic_string<char,std::char_trait s<char>,std::allocator<char> >,std::allocator<std::basic_string<c
har,std::char_traits<char>,std::allocator<char> > > >::std::vector<std::basic_string<char,std::char_tr aits<char>,std::allocator<char> >,std::allocator<std::basic_string<char,std::char_ traits<char>,std::allocator<char> > > >(const std::allocator<std:
:basic_string<char,std::char_traits<char>,std::all ocator<char> > > &)'
c:\program files\microsoft visual studio\vc98\include\vector(39) : warning C4786: 'std::vector<std::basic_string<char,std::char_trai ts<char>,std::allocator<char> >,std::allocator<std::basic_string<char,std::char_ traits<char>,std::allocator<char> > >
>::vector<std::basic_string<char,std::char_traits< char>,std::allocator<char> >,std::allocator<std::basic_string<char,std::char_ traits<char>,std::allocator<char> > > >' : identifier was truncated to '255' characters in the debug information
c:\program files\microsoft visual studio\vc98\include\vector(60) : warning C4786: 'std::vector<std::basic_string<char,std::char_trai ts<char>,std::allocator<char> >,std::allocator<std::basic_string<char,std::char_ traits<char>,std::allocator<char> > >
>::~vector<std::basic_string<char,std::char_traits <char>,std::allocator<char> >,std::allocator<std::basic_string<char,std::char_ traits<char>,std::allocator<char> > > >' : identifier was truncated to '255' characters in the debug information

filereader.exe - 0 error(s), 4 warning(s)

2. hmm.. I can open the file just fine with this little test program..

Code:
#include <string>
#include <iostream>
#include <fstream>

using namespace std;

void test ()
{
string temp;
//int back = ignored_words.size();
cout << "\n\n\tExample:  C:\\myfile.txt"
<< "\n\t-------"
<< "\n\n\tEnter the path to your file to count: ";
cin     >> temp;
ifstream infile( temp.c_str(), ios::binary);
if(infile.fail())
{
cout << "\n\n\t\aCannot Locate File -OR- File Does Not Exist!"
<< "\n\n\tPress [ENTER] to continue...";
}
else
infile.close();
cout << "done" << endl;
}

int main () {
test();
return 0;
}

3. The stream state is tricky when you're using a single file stream object for opening and closing multiple files in sequence. Do this:
Code:
infile.clear();
infile.open(temp.c_str(),ios::binary);

4. If you're going to reuse the infile object, you need to make sure that it is close()ed and clear()ed before calling open again.

Using ios::binary alone is an invalid open mode according to the standard.

gg

5. went through and put all my infile.clear()'s before any calls to .open().. made sure all open file streams were closed by the end of each function.. and it works fine

thanks

6.  Found the answer(s) to my getline( ) question [/edit]

7. >Why was the compiler so picky about the use of getline() in this instance?
Because it was wrong in all but the last case.
Code:
void FileReader::load_ignore_file()
{
string temp;

infile.clear();

infile.open("ignore.dat");

if(infile.fail())
return;

while(!infile.eof())
{
getline(infile, temp);
ignored_words.push_back(temp);
}

infile.close();
}
>getline(infile, temp.c_str());
This doesn't work because getline() expects a string, not a pointer to const char.

>infile.getline(temp,'\n');
This doesn't work because infile.getline() expects a pointer to char, not a string.

>infile.getline(temp.c_str,'\n');
This doesn't work because it's a syntax error, and it wouldn't work even if you added the parens to c_str() because it gives you a pointer to const char, not a pointer to char.

And using <stream>.eof() as a loop condition will bite you eventually. A better solution is to use the return value of your input function as the condition:
Code:
void FileReader::load_ignore_file()
{
string temp;

infile.clear();

infile.open("ignore.dat");

if(infile.fail())
return;

while(getline(infile, temp))
ignored_words.push_back(temp);

infile.close();
}

*smacks Brain for deleting his post*
[/edit]

8. *smacks self*

Popular pages Recent additions