C++ File I/O Issues

Discussion in 'Mac Programming' started by steelphantom, Apr 26, 2006.

  1. macrumors 6502a

    steelphantom

    Joined:
    Oct 15, 2005
    #1
    I'm having more C++ troubles (I know, what else is new? :rolleyes: ). This time it's with simple file I/O. My program will not read the specified file at all, and instead displays the "could not open" error message that I created. What is wrong with this program? And before you ask, yes, the '8.txt' file is in the same directory as the program.

    PHP:
    #include <iostream>
    #include <fstream>
    #include <cstdlib>

    int main()
    {
        
    using namespace std;
        
    ifstream fin;
        
    char ch;
        
    int count 0;
        
        
    fin.open("8.txt");
        if (!
    fin.is_open())
        {
            
    cout << "Could not open the file '8.txt'." << endl;
            
    cout << "Program terminating." << endl;
            exit(
    EXIT_FAILURE);
        }
        
        
    fin.get(ch);
        while (
    fin.good())
        {
            
    count++;
            
    fin.get(ch);
        }
        
        if (
    fin.eof())
            
    cout << "End of file reached." << endl;
        else if (
    fin.fail())
            
    cout << "Input terminated by data mismatch." << endl;
        else
            
    cout << "Input terminated for unknown reason." << endl;
        
    cout << "Total Characters Read: " << count << endl;
        
    fin.close();
        return 
    0;
    }
    I'm anxious to see what mistake I made this time!
     
  2. macrumors 68020

    pilotError

    Joined:
    Apr 12, 2006
    Location:
    Long Island
    #2
    Open Failure

    Go into terminal and do an ls -l on the file to see what the file permissions on 8.txt are. You need at least read access to the file.

    You can do a chmod 777 8.txt just to make sure the file permissions aren't the problem.

    Mike...
     
  3. thread starter macrumors 6502a

    steelphantom

    Joined:
    Oct 15, 2005
    #3
    Well, I just tried it, and I have permissions to read and write the file. It's most likely something wrong with my code.
     
  4. Moderator emeritus

    Joined:
    Jun 25, 2002
    Location:
    Gone but not forgotten.
    #4
    Well, I just tried it with a simple file containing the numerals 1 through 8 on one line each.

    End of file reached.
    Total Characters Read: 16

    8 digits + 8 newlines.
     
  5. thread starter macrumors 6502a

    steelphantom

    Joined:
    Oct 15, 2005
    #5
    I added an absolute file path and it worked now. Is there a way that I can link to my file without including an absolute path to it if the file is in the same directory as my program?
     
  6. macrumors 68030

    slooksterPSV

    Joined:
    Apr 17, 2004
    Location:
    Nowheresville
    #6
    ./

    so fin.open("./8.txt");
    ./ represents current working directory right? or is it going to default to another location? in HTML I do ./ all the time

    EDIT: Yup it works
     
  7. thread starter macrumors 6502a

    steelphantom

    Joined:
    Oct 15, 2005
    #7
    Just tried it with my program and it didn't work. :( I was thinking about trying that, too.
     
  8. macrumors 603

    gekko513

    Joined:
    Oct 16, 2003
    #8
    how about adding a line
    system("pwd > ~/Desktop/pwd.txt");

    And then check the contents of the new file on the desktop to see if you're executing where you think you are?
     
  9. thread starter macrumors 6502a

    steelphantom

    Joined:
    Oct 15, 2005
    #9
    I'm not sure exactly what pwd means (program working directory?), but it looks like it's executing in /Users/nicktiberi instead of the directory in which the program and text file are located. Why is this the case?
     
  10. Moderator emeritus

    Joined:
    Jun 25, 2002
    Location:
    Gone but not forgotten.
    #10
    Sounds like the executable ended up there, unlike the source code. Perhaps an adjustment to your environment.

    pwd = print working directory.
     
  11. macrumors 68030

    slooksterPSV

    Joined:
    Apr 17, 2004
    Location:
    Nowheresville
    #11
    I think I know what may have happened, use finder and navigate to your user folder by going the long route - Macintosh HD->Users->username
    Mine did this a while ago, my Users folder was a file, and there was no way to make it a folder unless I deleted it, but I could still access the folders within that "file" through terminal or using the home button in my dock.
     
  12. thread starter macrumors 6502a

    steelphantom

    Joined:
    Oct 15, 2005
    #12
    The executable is in the same folder as the source code, so I'm still not quite sure why that's the working directory.

    I was able to navigate to my user folder that way with no problems. My user folder seems to be acting normally. I think I'll just ignore this for now until it becomes a real issue.
     
  13. macrumors 603

    janey

    Joined:
    Dec 20, 2002
    Location:
    sunny los angeles
    #13
    Got an idea.

    Are you executing this app from Terminal (i.e. /path/to/program/programname ?)

    Cause, i'm betting its because you executed the program from your User directory and not from wherever 8.txt is, which would explain why you can't get it to work unless you use absolute pathnames.

    The way I do it with mine is simply opening terminal and typing in:
    cd /path/to/directory_with_file
    ./programname

    because when you login or open a new terminal window, you're gonna be in your User directory (~).

    Also, just as a heads up, if this is like, for an assignment for a class, i suggest not using absolute pathnames for files like that, or just in general (once i did that, my prof was on windows...obviously /Users/jane/Documents/svn_working/assignment5/testfile.txt nor C:\Documents and Settings\jane\vs2005\assignment5\testfile.txt work...)
     
  14. thread starter macrumors 6502a

    steelphantom

    Joined:
    Oct 15, 2005
    #14
    That's it! I was just clicking on the executable file to run the program instead of navigating to the directory using Terminal. Thanks a lot! By the way, it's not for a class or anything. I'm learning C++ on my own using a book, and this was one of the excercises in the book. Thanks again for the help! :)
     
  15. macrumors 603

    janey

    Joined:
    Dec 20, 2002
    Location:
    sunny los angeles
    #15
    oh hells yeah! :D

    yeah, it was the only thing i could think of, as i ran into a little snag with my java assignment a few days back about paths and stuff as well.

    good luck :) i'm learning C++ right now too, hate the language, but I figure it's useful to learn. which book ya using? i ended up with core c++: a software engineering approach after going through 3 or 4 books and finding them either way too easy or just boring. not perfect by any means, but it's a decent book :)
     
  16. thread starter macrumors 6502a

    steelphantom

    Joined:
    Oct 15, 2005
    #16
    I'm using SAMS C++ Primer Plus, and so far it's pretty good. I'm pretty new to programming in general, so I wanted something that would cover the basics and get me familiar with programming concepts and whatnot. Next up is PHP and MySQL! :D
     
  17. macrumors 603

    janey

    Joined:
    Dec 20, 2002
    Location:
    sunny los angeles
    #17
    ah fun! :D Ever consider learning Objective-C+cocoa?

    (this is, after all, a mac forum..lol)
     
  18. thread starter macrumors 6502a

    steelphantom

    Joined:
    Oct 15, 2005
    #18
    I'd like to do that as well! So many programming languages, so little time... :D
     
  19. macrumors 603

    janey

    Joined:
    Dec 20, 2002
    Location:
    sunny los angeles
    #19
    well, once you learn a few, and most of the concepts, the rest are pretty easy. :)
     
  20. macrumors 68000

    Soulstorm

    Joined:
    Feb 1, 2005
    #20
    heh. I know C++ pretty well, and I moved on to Obj-C. At first I hated C++, but then I couldn't live without it. Now, with Obj-C, the word "hate" obtained a new meaning in my vocabulary... :D :rolleyes:

    I only hope that at the end I will love this language, too.
     
  21. macrumors 6502a

    Palad1

    Joined:
    Feb 24, 2004
    Location:
    London, UK
    #21
    Hard coding is _bad_

    How about using parameters?

    PHP:
    #include <iostream>
    #include <fstream>
    #include <errno>
    #include <cstdlib>

    int main(int argc,char** argv)
    {
        
    using namespace std;
        
    ifstream fin;
        
    char ch;
        
    int count 0;
        
        
    charfileName="8.txt"
        
    if(arc>0)
        {
          
    //   how nice, the user gave us a filepath
         
    fileName=argv[1];
         
    cout<<"Using file :"<<fileName<<endl;
        }

        
    fin.open(fileName);
        if (!
    fin.is_open())
        {
            
    // the stream is not open, let's diagnose 
            
    cout << "Could not open the file '"<< fileName <<"'"<<endl;
            
    // use the last error code set by this thread
            
    cout << " Error :'"<< strerror(errno)<<"' ("<<errno<<")"<<endl
            cout 
    << "Program terminating." << endl;
            exit(
    EXIT_FAILURE);
        }    
    and invoke your app like this:
    Code:
    user@host$ myApp ~/path/to/file.txt
    
    hint : use tab completion to make sure you get an existing file.

    Cheers,
    Palad1

    ps: ifstream sucks, big time...
     
  22. macrumors 68000

    Soulstorm

    Joined:
    Feb 1, 2005
    #22
    are there alternatives?
     
  23. Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #23
    Once you get to know Objective-C, you'll be in love :)
     
  24. macrumors 6502a

    Palad1

    Joined:
    Feb 24, 2004
    Location:
    London, UK
    #24
  25. macrumors 68020

    pilotError

    Joined:
    Apr 12, 2006
    Location:
    Long Island
    #25
    Probably Asked a million times

    Whats a good book for Obj-C?
     

Share This Page