read a file

Discussion in 'Mac Programming' started by FuuFuu, Sep 23, 2010.

  1. macrumors regular

    FuuFuu

    Joined:
    Jul 8, 2008
    Location:
    Roselle
    #1
    trying to read in a file. not working out lol. please help here is the code. i'm using xcode and it is in c++. the getline line is giving me an error. i've tried const.txt but its also not working.


    Code:
    #include <iostream>
    #include <fstream>
    #include <strstream>
    #include <string>
    
    using namespace std;
    
    int main (int argc, char * const argv[]) {
    
    	string line;
    	ifstream myReadFile ("const.rtfd");
    	while (!myReadFile.eof())
    	{
    		myReadFile.getline(myReadFile,line);
    		std::cout << line << endl;
    	}
    	
        return 0;
    }
     
  2. macrumors 603

    MorphingDragon

    Joined:
    Mar 27, 2009
    Location:
    The World Inbetween
    #2
    IS the filed located in the same folder as the executable?
     
  3. thread starter macrumors regular

    FuuFuu

    Joined:
    Jul 8, 2008
    Location:
    Roselle
    #3
    i've saved it under the project, still nothing. D:

    here is the directory where i saved it. the project is named createTree1
    /Users/takayukigoto/Documents/createTree1
     
  4. Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #4
    I imagine you are getting several warnings when you compile that code, because I do.

    Here's a version that works. Notice I removed an #include, and changed the getline() function to work slightly differently. That function can either be by itself and take a string object, or it works on the ifstream object directly as a method and takes a C-style string. Don't ask me why ;)

    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    
    using namespace std;
    
    int main (int argc, char * const argv[])
    {
        string line;
        ifstream myReadFile ("const.txt");
    
        while (getline(myReadFile, line))
        {
            cout << line << endl;
        }
    
        return 0;
    }
    And yes, your text file needs to be in the same folder as your executable.
     
  5. macrumors 603

    Joined:
    Aug 9, 2009
    #5
    The format of a .rtfd file isn't actually a file at all. It's directory containing one or more files. The most important consequence of this is that there is no way that ifstream would ever work to open it.
    http://en.wikipedia.org/wiki/Rich_Text_Format_Directory

    So if you saved something from TextEdit.app (or any other app) as an actual .rtfd, then the program won't work. However, if you simply did something like rename a .txt file to .rtfd, then it might work, but only if the file was in the same directory as the executable itself.

    You didn't explain how you produced the file you attempted to read, so I would guess you saved it as .rtfd from some app. In that case, it won't work even if it's in the same directory.
     
  6. macrumors 603

    MorphingDragon

    Joined:
    Mar 27, 2009
    Location:
    The World Inbetween
    #6
    Or you would get garbage as the reader will read code like its a text file.
     
  7. macrumors 6502a

    Joined:
    May 10, 2009
    Location:
    Des Moines, WA
    #7
    With your project open select "Edit Active Executable" from Xcode's "Project" menu.

    In the resultant window make sure the "General" tab view control is selected at the top of the window.

    Near the bottom of the window is a radio button group titled "Set the working directory to:" of which you should select "Project directory".

    Close the window.

    It should now be able to find your document bundle "const.rtfd" if you placed it into the same folder as your project.

    As to your code I'd suggest something along the line of:

    Code:
    #include <fstream>
    #include <iostream>
    #include <string>
    
    int main(int argc, char* const argv[])
    {
        using std::cout;
        using std::endl;
        using std::ifstream;
        using std::string;
        
        string      str;
        ifstream    ifs("const.rtfd/TXT.rtf");
        if ( ifs.is_open() )
        {
            while ( getline(ifs, str) )
            {
                cout << str << endl;
            }
    
            return EXIT_SUCCESS;
        }
        
        return EXIT_FAILURE;
    }
    
     
  8. macrumors newbie

    Joined:
    Oct 5, 2010
    #8
    Hi, i have to write a program to read in only the numbers from a txt file and then sort them. The sorting part is easy but how do i read only the numbers of a txt file like "oawda 232 kdd 44 oiro23 9000 4004" Note that oiro23 must not be read. Do i use fgets somehow...

    __________

    Industrial Kitchen Equipment | Restaurant Equipment
     
  9. macrumors 6502a

    GorillaPaws

    Joined:
    Oct 26, 2003
    Location:
    Richmond, VA
    #9
    The ASCII for the chars 0-9 begin at a hex value 30 and end at a hex value of 39.

    You could parse each segment of text after a space character and check to see that the ASCII values of all chars in that text segment fall into this range. If this is true, you can add that number to your array to later be sorted (or insert it into its place in the final array right then); if false, continue on to the next text segment.
     

Share This Page