File I/O Issue

This is a discussion on File I/O Issue within the C++ Programming forums, part of the General Programming Boards category; I am trying to open() a user specified .dat file. No matter how I enter the path to the file, ...

  1. #1
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903

    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...
    filereader.cpp
    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
    Linking...

    filereader.exe - 0 error(s), 4 warning(s)
    Last edited by The Brain; 06-04-2005 at 06:07 PM.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  2. #2
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    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;
    }
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    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);
    My best code is written with the delete key.

  4. #4
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,660
    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
    Last edited by Codeplug; 06-04-2005 at 01:57 PM. Reason: grammer...

  5. #5
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903

    Thumbs up

    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
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  6. #6
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    [edit] Found the answer(s) to my getline( ) question [/edit]


    google == good;
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >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();
    }
    [edit]
    *smacks Brain for deleting his post*
    [/edit]
    My best code is written with the delete key.

  8. #8
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    *smacks self*
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  2. Need Help Fixing My C Program. Deals with File I/O
    By Matus in forum C Programming
    Replies: 7
    Last Post: 04-29-2008, 07:51 PM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 01:53 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21