bug in g++? corrupted fstream?

Discussion in 'Mac Apps and Mac App Store' started by GeeYouEye, Sep 7, 2003.

  1. GeeYouEye macrumors 68000

    GeeYouEye

    Joined:
    Dec 9, 2001
    Location:
    State of Denial
    #1
    I'm writing a program in C++ which reads a dictionary file, puts it into a vector (using the apvector (more specifically, apvector<apstring>)class), and then outputs the vector's elements into another file in a certain order. Here's the problem: almost everything involving fstream is being ignored completely; IOW, the EOF isn't being recognized for the dictionary, nothing is getlined in, when i ask for the just added vector element (cout << vocab[counter];), i get nothing (not even a space), and nothing happens with the file i'm trying to output - it's not created nor written to.

    What's worse is that it runs just fine otherwise - no errors or warnings in compilation, just nothing happens when running it. To add insult to injury, it runs perfectly when compiled on a PC with CodeWarrior 4.

    Any ideas?
     
  2. zimv20 macrumors 601

    zimv20

    Joined:
    Jul 18, 2002
    Location:
    toronto
  3. GeeYouEye thread starter macrumors 68000

    GeeYouEye

    Joined:
    Dec 9, 2001
    Location:
    State of Denial
    #3
    Not that I know of. What is it?
     
  4. zimv20 macrumors 601

    zimv20

    Joined:
    Jul 18, 2002
    Location:
    toronto
    #4
    a C-preprocessor that's way anal about code syntax. e.g. it'll complain if you don't cast function call returns to void (if they don't return anything).

    however, it can useful for figuring out type mismatches and stuff.

    there are versions for C++.
     
  5. bousozoku Moderator emeritus

    Joined:
    Jun 25, 2002
    Location:
    Gone but not forgotten.
    #5
    It doesn't surprise me that Apple's version of gcc isn't working correctly. I continue to run into situations where other versions of gcc and CodeWarrior compile and produce correct code. It has even happened with the gcc version 3.3 update.

    You could set on all warnings in the compiler for the project in order to see if it finds something. Did you notice any warnings from CodeWarrior?
     
  6. GeeYouEye thread starter macrumors 68000

    GeeYouEye

    Joined:
    Dec 9, 2001
    Location:
    State of Denial
    #6
    nope, no warnings in codewarrior. I'll look for lint. Here's the relevant code fragments that are apparently doing nothing:

    Code:
    ifstream dictionary("dictionary.txt");
    	//cout << 2;
            apvector<apstring> vocab(24259);
    	//cout << 3;
            int counter;
    	//cout << 4;
            //apstring temp;
            int wordsLearned = 0;
            cout << "\nLearning vocabulary\n";
    
            /*while (!fin.eof())
    	{	
                    getline(fin, temp);
                    cout << temp;
                    temp+="\b";
    		vocab [counter] = temp;
                    cout << vocab[counter];
                    counter++;
                   // cout << temp;
                    vocab.resize(counter+1);
    	}*/
            for (counter=0; counter < 24259; counter++)
            {
                dictionary >> vocab[counter];
                //getline(dictionary, vocab[counter]);
                wordsLearned++;
                if (wordsLearned % 1000 == 0)
                {
                    cout << endl << endl << wordsLearned << " " << vocab[counter];
                }
                //vocab.resize(vocab.length() + 1);
            }
    	//cout << 9;
            dictionary.close();
    	//cout << 10;
            return vocab;
    
    That's two versions of it. Neither worked (though I have reason to believe that the first one worked at one point, but doesn't anymore. Here's the other part.
    Code:
    ofstream fout("dictvector2.cpp");	
    int i;
    fout << "#include <iostream.h>\n#include \"apvector.h\"\n#include \"apstring.*h\"\n"
            << "\napvector<apstring> myDict(" << vocab.length() << ");\n";
        for (i=0; i < vocab.length(); i++)
        {
            fout << "myDict [" << i << "] = \"" << vocab [i] << "\b" << "\";\n";
        } //try this
        fout.close();
    The intent here being to create file dictvector2.cpp and write code to it. This was actually rendered useless by making the vector fixed instead of resizing, but it's still not working.

    The output for the entire program is:
    Code:
    Learning vocabulary
    1000
    2000
    3000
    4000
    5000
    6000
    7000
    8000
    9000
    
    ...
    Code:
    23000
    24000
    and no file is created.
     
  7. zimv20 macrumors 601

    zimv20

    Joined:
    Jul 18, 2002
    Location:
    toronto
    #7
    have you put the thing up in a debugger and checked the vector values?

    what's your output look like?

    my C++ is rusty, but i did find this:

    if (wordsLearned % 1000 == 0)
    {
    cout << endl << endl << wordsLearned << " " << vocab[counter];
    }


    don't you want another "endl" after "vocab[counter]"?
     
  8. GeeYouEye thread starter macrumors 68000

    GeeYouEye

    Joined:
    Dec 9, 2001
    Location:
    State of Denial
    #8
    Haven't used a debugger at all - just been typing the code in TextEdit, and compiling with "g++ D2VC.cpp (sometimes -ansi) -o /D2VC" in the terminal.
     
  9. GeeYouEye thread starter macrumors 68000

    GeeYouEye

    Joined:
    Dec 9, 2001
    Location:
    State of Denial
    #9
    Okay, I've discovered and fixed the problem. Turns out, contrary to what I'd thought, fstream doesn't care where the binary of the program is in relation to the files it uses, but rather it cares about what the pwd is when launched, irrespective of where the file is. Here was my problem: I was compiling the .cpp, and outputting it to the root folder of my hard drive. Then, in another Terminal window, whose pwd was still my home folder, instead of "cd /; ./D2VC" as I should have done, I was being lazy and just running it as "/D2VC", from "~/". Crazy, but true. could have saved a lot of time if I'd been a little less lazy.

    If you're wondering how I figured it out, I was searching my HD for a better dictionary file than the one I had, so I was searching for "dict" When I saw that dictvector2.cpp was among those listed, I was surprised since I assumed that it would have been created in the same folder as the program. Once I realized where it was, I figured out the pwd sensitivity. Also, it explains why I thought I was having problems with fin.eof(); the file it was looking for the end of didn't exist in the ~/ folder

    crazy stuff.
     
  10. bousozoku Moderator emeritus

    Joined:
    Jun 25, 2002
    Location:
    Gone but not forgotten.
    #10
    If I may ask, why were you compiling something to the root directory? :confused:

    Glad you figured it out.
     
  11. mmmdreg macrumors 65816

    mmmdreg

    Joined:
    Apr 14, 2002
    Location:
    Sydney, Australia
    #11
    I have a small problem that I can't fix. I posted it in another thread too but this thread seemed more relevant.

    This small fragment of code always fails. (Excuse the variable names).
    //
    ifstream inFoo;
    inFoo.open("pathToFoo.txt");
    bool fat;
    fat = inFoo.fail();
    cout << fat << endl;
    //

    I tried using absolute path, changing the placement of the text file, checked permissions etc etc.

    Am I doing something wrong?
     

Share This Page