Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

steelphantom

macrumors 6502a
Original poster
Oct 15, 2005
555
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!
 

pilotError

macrumors 68020
Apr 12, 2006
2,237
4
Long Island
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...
 

steelphantom

macrumors 6502a
Original poster
Oct 15, 2005
555
1
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

Moderator emeritus
Jun 25, 2002
15,719
1,894
Lard
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

macrumors 6502a
Original poster
Oct 15, 2005
555
1
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

macrumors 68040
Apr 17, 2004
3,543
305
Nowheresville
steelphantom said:
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

macrumors 6502a
Original poster
Oct 15, 2005
555
1
slooksterPSV said:
./

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

macrumors 603
Oct 16, 2003
6,301
1
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

macrumors 6502a
Original poster
Oct 15, 2005
555
1
gekko513 said:
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

Moderator emeritus
Jun 25, 2002
15,719
1,894
Lard
steelphantom said:
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

macrumors 68040
Apr 17, 2004
3,543
305
Nowheresville
bousozoku said:
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

macrumors 6502a
Original poster
Oct 15, 2005
555
1
bousozoku said:
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.

slooksterPSV said:
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

macrumors 603
Dec 20, 2002
5,316
0
sunny los angeles
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

macrumors 6502a
Original poster
Oct 15, 2005
555
1
janey said:
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

macrumors 603
Dec 20, 2002
5,316
0
sunny los angeles
steelphantom said:
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

macrumors 6502a
Original poster
Oct 15, 2005
555
1
janey said:
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

macrumors 603
Dec 20, 2002
5,316
0
sunny los angeles
steelphantom said:
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

macrumors 6502a
Original poster
Oct 15, 2005
555
1
janey said:
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
 

Soulstorm

macrumors 68000
Feb 1, 2005
1,887
1
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

macrumors 6502a
Feb 24, 2004
647
0
London, UK
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;
    
    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:
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...
 

kainjow

Moderator emeritus
Jun 15, 2000
7,958
7
Soulstorm said:
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 :)
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.