C++ File Reading Help

Discussion in 'Mac Programming' started by Furgster, Nov 26, 2008.

  1. Furgster macrumors 6502

    Furgster

    Joined:
    Jun 16, 2007
    #1
    Ok, I have this homework assignment, (and yes it sis a homework assignment so im not asking anyone to write the code for me but i really need the help to figure out why this is thing isnt working!!! :confused:) ok, so anyway, i am basically reading in all the names of files in a directory and then reading in each one of the files (word by word) into a vector. so i basically have a nested vector (i know that there are better ways of doing this, but i have to do it this way) so, i have it all working up to the part where i have to read in the actual file. I am using the ifstream to get the input, as you can see in the code, i have an object called infile and then use the open function to open the desired file (the interesting thing there is that the name of the file has to be a c-string) and then i go into a while loop to read in all the data. i hope that this is some simple syntax error that im just not seeing, and help would be very very appreciated thanks!

    oh and the filter function just makes everything lower case and strips any punctuation (it was written by my professor and works, so i didnt include it)

    Code:
    ifstream infile;
    	cout << "DEBUG: ifstream object created" << endl;
    	
    	infile.open(charFilename, ifstream::in);
    	cout << "DEBUG: read in the file using the 'open' command" << endl;
    	
    
    	cout << "Debug: Prepare to enter the while loop, using the eof() command here" << endl;
        while( !infile.eof()){
          cout << "DEBUG: Enter the while loop" << endl;
    	  string word;
    	  infile >> word;
    	  filter(word);
    	  
          setSet[i].push_back(word);
    	  cout << "DEBUG: Exit the while loop" << endl;
        }
    
     
  2. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #2
    What is happening? What are you expecting to happen?

    Can you include compilable code so we can test it?

    -Lee
     
  3. Sander macrumors 6502

    Joined:
    Apr 24, 2008
    #3
    So what exactly is your question..?

    Also, you're right to notice that it's strange that the file name needs to be a C-style string and not a std::string. One of the "historical quirks" in C++.

    EDIT: Lee beat me to it with this question. Weird, I swear his post wasn't there when I replied although it's half an hour older :confused:
     
  4. toddburch macrumors 6502a

    Joined:
    Dec 4, 2006
    Location:
    Katy, Texas
    #4
    Without you stating what is wrong, or any error messages, the most we can do is review your code. So, on to that:

    Your WHILE loop's condition is testing for EOF. This concept is fine, but not the way you've done it. When you do the read inside your loop, THAT's when you need to test for EOF. However, you use the read value assuming there is one, and there might not be. Not only might you have hit EOF, but there might even be some errors. So, checking infile for fail() and/or bad() would certainly be in order.

    Your filter function appears to be return void, so I'm not sure the purpose of it, as whatever function it does perform is lost, unless it exits your program if it finds something it does not like.

    Finally, inside your WHILE loop, you are using "i" as an index into your vector, but you are never incrementing it. I can't tell if that is by design and perhaps the current index to your outer file vector, or not, based on the limited context of your post.

    Todd
     
  5. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #5
    Until the OP responds we can just reply to one another.

    toddburch: it seems that you are better versed in C++ and streams than I, but I think in the OP's code they are reading in the loop:
    Code:
    infile >> word;
    Also, I'll guess purposes for filter:
    It changes state of an object both of these methods are in, like a boolean hasObscenity
    It prints word to stdout, unless word is considered obscene, then it prints word.length() *s instead.

    I'm sure you are right, and it should return a boolean, that controls whether or not it gets placed in the vector, but I thought it would be fun to guess what it does as-is.

    -Lee
     
  6. Furgster thread starter macrumors 6502

    Furgster

    Joined:
    Jun 16, 2007
    #6
    ok, the filter function is a void returnign fucntion, but it passes in the word by reference so it can modify it. the problem that i am having and sorry for not stating it before is that the program runs forever! it never seems to get to the end of the file.

    as for incrementing i in the while loop: the while loop is actually nested in a for loop (sorry i forgot to mention that) and i know that nesting thinsg like that is a bad idea but thats the way i have to do it.
     
  7. Furgster thread starter macrumors 6502

    Furgster

    Joined:
    Jun 16, 2007
    #7
    so do you think i should replace the eof() with fail() or bad() or put that inside the while loop somewhere. I understand that it is not testing for eof() but im not sure how to fix it
     
  8. toddburch macrumors 6502a

    Joined:
    Dec 4, 2006
    Location:
    Katy, Texas
    #8
    I usually keep things like this pretty simple, and coding this way, you can use a debugger and not miss anything...

    Code:
    while( 1 ){
        cout << "DEBUG: Enter the while loop" << endl;
        string word;
        infile >> word;
        if ( infile.eof() ) break ; 
        else if ( infile.bad() | infile.fail() ) { ...do something to alert the user... } 
        filter(word);
        setSet[i].push_back(word);
        cout << "DEBUG: Exit the while loop" << endl;
        }
    
    Since the code was failing by looping, my guess is that either fail() or bad() was being set, and therefore, eof() was never set.
     

Share This Page