C FILE function issues
Greetings all,
I feel odd with posting a lot of 'help request' posts... so, let me know if there is a way I can support the community... rather than ask for help all the time.
Well, on with the issue. I am making my way through my first programming book, which is: Learn C on the Mac by Dave Mark. Well, on chapter 10, the first source example it provides does not work correctly. The following is the source:
I added the second IF statement to see if 'fp' was NULL - and when I ran the program, it printed "File did not open!!!" Could anyone tell me what this file is missing? Thanks!
~Achi
I feel odd with posting a lot of 'help request' posts... so, let me know if there is a way I can support the community... rather than ask for help all the time.

Well, on with the issue. I am making my way through my first programming book, which is: Learn C on the Mac by Dave Mark. Well, on chapter 10, the first source example it provides does not work correctly. The following is the source:
Code:
#include <stdio.h>
int main (int argc, const char * argv[])
{
FILE *fp;
int c;
fp = fopen( "../My Data Filef", "r" );
if ( fp != NULL )
{
while ( (c = fgetc( fp ) ) != EOF )
putchar( c );
fclose( fp );
}
/* Added to see if the program was opening the file */
if ( fp == NULL )
{
printf( "File did not open!!!" );
}
return 0;
}~Achi
Do you have a file called "My Data Filef" in the appropriate location?
I do, well, I have a file name My Data File (without the f). I removed the extra f in the source, not sure how it got there in the first place, though the problem persists. I'm assuming the correct location is where main.c is being ran? I've moved the file around a bit, though it didn't make a difference. Based off your response, I am assuming the syntax is right? Thus, the file must be in the wrong area... let me know if that assumption is completely off. Thanks! 
~Achi

~Achi
Where the right place is depends entirely on how you're running your application. If you're running it from Xcode, the current working directory is the one containing your application (somewhere inside the build folder, perhaps build/Debug). ".." means "go up one directory", so if your application is at build/Debug/MyGreatApp.app, you'd put your file in the build folder.
Totally awesome. I moved my file to the build folder and it works great now.
This was actually the chapter I was most excited for... since every time I start programming with any language (C, C++, Java, Python, Visual Basic, QBasic, Dark Basic... the ones I've played with), I never get as far as actually using files. Aighty, time to finish this book!
Thanks again!
~Achi
This was actually the chapter I was most excited for... since every time I start programming with any language (C, C++, Java, Python, Visual Basic, QBasic, Dark Basic... the ones I've played with), I never get as far as actually using files. Aighty, time to finish this book!
Thanks again!~Achi
Just a silly question, but was there some reason you used "if ( fp == NULL )" instead of else?
Nope, not really. I just popped the extra 'if' statement in there to see what would happen. I didn't really think much about it when I added it. An 'else' would have worked just as well - good point. 
~Achi

~Achi
Here's the general answer to "where's my file":
http://tips.onesadcookie.net/tips/publis...my+File%3F
http://tips.onesadcookie.net/tips/publis...my+File%3F

