C FILE function issues

Member
Posts: 21
Joined: 2009.05
Post: #1
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. Smile

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;
}
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
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #2
Do you have a file called "My Data Filef" in the appropriate location?
Quote this message in a reply
Member
Posts: 21
Joined: 2009.05
Post: #3
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! Smile

~Achi
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #4
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.
Quote this message in a reply
Member
Posts: 21
Joined: 2009.05
Post: #5
Totally awesome. I moved my file to the build folder and it works great now. Smile 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! Grin Thanks again!

~Achi
Quote this message in a reply
Member
Posts: 254
Joined: 2005.10
Post: #6
Just a silly question, but was there some reason you used "if ( fp == NULL )" instead of else?
Quote this message in a reply
Member
Posts: 21
Joined: 2009.05
Post: #7
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. Smile

~Achi
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #8
Here's the general answer to "where's my file":

http://tips.onesadcookie.net/tips/publis...my+File%3F
Quote this message in a reply
Post Reply