PDA

View Full Version : read a file




FuuFuu
Sep 23, 2010, 07:29 PM
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.


#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;
}



MorphingDragon
Sep 23, 2010, 07:42 PM
IS the filed located in the same folder as the executable?

FuuFuu
Sep 23, 2010, 07:47 PM
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

kainjow
Sep 23, 2010, 11:07 PM
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 ;)

#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.

chown33
Sep 24, 2010, 12:30 AM
ifstream myReadFile ("const.rtfd");


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.

MorphingDragon
Sep 24, 2010, 11:42 PM
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.

Or you would get garbage as the reader will read code like its a text file.

lloyddean
Sep 25, 2010, 12:53 AM
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:


#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;
}

raaj2112
Oct 5, 2010, 03:16 AM
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 (http://www.nextdaycatering.co.uk) | Restaurant Equipment (http://www.nextdaycatering.co.uk)

GorillaPaws
Oct 5, 2010, 06:56 AM
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 (http://www.nextdaycatering.co.uk) | Restaurant Equipment (http://www.nextdaycatering.co.uk)

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.