PDA

View Full Version : C++ File I/O Issues




steelphantom
Apr 26, 2006, 07:36 PM
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.

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



pilotError
Apr 26, 2006, 09:46 PM
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...

steelphantom
Apr 26, 2006, 09:59 PM
Well, I just tried it, and I have permissions to read and write the file. It's most likely something wrong with my code.

bousozoku
Apr 26, 2006, 10:30 PM
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.

steelphantom
Apr 26, 2006, 10:38 PM
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?

slooksterPSV
Apr 26, 2006, 11:27 PM
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?
./

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

steelphantom
Apr 26, 2006, 11:47 PM
./

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

Just tried it with my program and it didn't work. :( I was thinking about trying that, too.

gekko513
Apr 27, 2006, 12:06 AM
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?

steelphantom
Apr 27, 2006, 12:34 AM
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?

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?

bousozoku
Apr 27, 2006, 12:58 AM
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?

Sounds like the executable ended up there, unlike the source code. Perhaps an adjustment to your environment.

pwd = print working directory.

slooksterPSV
Apr 27, 2006, 06:10 PM
Sounds like the executable ended up there, unlike the source code. Perhaps an adjustment to your environment.

pwd = print working directory.
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.

steelphantom
Apr 27, 2006, 07:55 PM
Sounds like the executable ended up there, unlike the source code. Perhaps an adjustment to your environment.

pwd = print working directory.

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

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.

janey
Apr 27, 2006, 08:22 PM
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...)

steelphantom
Apr 27, 2006, 08:38 PM
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...)

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! :)

janey
Apr 27, 2006, 08:46 PM
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! :)
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 :)

steelphantom
Apr 27, 2006, 08:53 PM
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 :)

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

janey
Apr 27, 2006, 09:29 PM
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
ah fun! :D Ever consider learning Objective-C+cocoa?

(this is, after all, a mac forum..lol)

steelphantom
Apr 27, 2006, 09:32 PM
ah fun! :D Ever consider learning Objective-C+cocoa?

(this is, after all, a mac forum..lol)

I'd like to do that as well! So many programming languages, so little time... :D

janey
Apr 27, 2006, 09:42 PM
I'd like to do that as well! So many programming languages, so little time... :D
well, once you learn a few, and most of the concepts, the rest are pretty easy. :)

Soulstorm
Apr 28, 2006, 03:40 AM
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.

Palad1
Apr 28, 2006, 07:13 AM
How about using parameters?


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

int main(int argc,char** argv)
{
using namespace std;
ifstream fin;
char ch;
int count = 0;

char* fileName="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:
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...

Soulstorm
Apr 28, 2006, 07:21 AM
ps: ifstream sucks, big time...are there alternatives?

kainjow
Apr 28, 2006, 07:36 AM
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.
Once you get to know Objective-C, you'll be in love :)

Palad1
Apr 28, 2006, 09:25 AM
the 'old' file management routines still work ;)


As far as OO libs go, I heard the best from colleagues using boost : http://www.boost.org/libs/filesystem/doc/index.htm

here's a nice example : http://www.boost.org/libs/filesystem/example/simple_ls.cpp

pilotError
Apr 28, 2006, 09:32 AM
Whats a good book for Obj-C?

Soulstorm
Apr 28, 2006, 10:28 AM
Popular books for Obj-C are:
Cocoa Programming for Mac OS X (http://www.amazon.com/gp/product/0201726831/ref=pd_sim_books/002-2814893-6864826?n=283155)
Cocoa Programming (http://www.amazon.com/gp/product/0672322307/002-2814893-6864826?n=283155)

kainjow
Apr 28, 2006, 10:35 AM
Popular books for Obj-C are:
Cocoa Programming for Mac OS X (http://www.amazon.com/gp/product/0201726831/ref=pd_sim_books/002-2814893-6864826?n=283155)
Cocoa Programming (http://www.amazon.com/gp/product/0672322307/002-2814893-6864826?n=283155)
That needs to be a sticky, along with the fact that Xcode can do Java and it can do C/C++ :p

FireArse
Apr 28, 2006, 12:46 PM
Popular books for Obj-C are:
Cocoa Programming for Mac OS X (http://www.amazon.com/gp/product/0201726831/ref=pd_sim_books/002-2814893-6864826?n=283155)
Cocoa Programming (http://www.amazon.com/gp/product/0672322307/002-2814893-6864826?n=283155)

Just quickly, can you compile an Obj-C program within the terminal and run it like a C++ file?

I've done C++, but I have exams and disso's before I do Obj-C, maybe this summer eh with a nice GUI within XCode!

Linked lists and arrays are the worst!

F

Palad1
Apr 29, 2006, 01:47 AM
Let's recap things:

1/ A file contains source code and is run through a compiler, it its turned into an object file.
2/ Several files are then linked together and create binaries (or executables)

So, if you get a source file containing c++, compile it with a c++ toolchain, then link it, you get an executable file.

If you take a source file containning obj-c code, compile it with an obj-c toolchain, link it, you get an executable file.

Both executables can be run, without any consideration about the original language.

What makes a big difference are the API (external libraries) used. Most C / C++ libraries can be called from a variety of language (perl/php/obj-C). On the other hand, most Obj-C libraries cannot be called from external languages without running through some serious hoops...

If you want to write portable obj-c code (ie. between Mac OS X and Linux) you should be careful not to use cocoa for example.

Hope that helps ...

ps: good luck with your linked list and your overflowing buffers, we all went though this at some point ;)

FireArse
Apr 29, 2006, 03:35 AM
Palad1: Merci beacoup!

GeeYouEye
Apr 29, 2006, 08:32 AM
Having programmed in both Objective-C and C++ for a 2-3 years each now, I've found that Objective-C is much more consistent, less headachy, and far easier to debug. I used to hate the idea of having no objects on the stack in Objective-C, for example, but when that's the case, everything gets treated the same. Also means you don't need a no-argument constructor if you ever want to use a class in a composition class. And then there's templates... *shudder*