Thread: Stuck, can't open file

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    14

    Stuck, can't open file

    Hi, I'm trying to pass filenames to a function that opens the files and reads the data in. At this stage my file pointers are just returning NULL and I can't work out what I'm doing wrong. Any tips would be very much appreciated!
    Thanks in advance...

    Code:
    int loadData(GJCType* menu, char* menuFile, char* submenuFile)
    {
       FILE *fp1 = NULL, *fp2 = NULL;  
       
       printf("menuFile filename is is %s\n", menuFile);
       printf("submenuFile filename is %s\n", submenuFile);
    
       printf("Attempt to load data!\n");
       
       fp1 = fopen(menuFile, "r");
       fp2 = fopen(submenuFile, "r");
       
       if (fp1 == NULL) 
       {
          fprintf(stderr, "Menu File open error\n");
       }
       else {
          printf("Menu File open success, I think!\n");      
       }
       if (fp2 != NULL) 
       {
          printf("Sub Menu File open success, I think!\n");
       }
       else { 
          fprintf(stderr, "Sub Menu File open error\n");
          return EXIT_FAILURE;
       }   
       return 1;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Perhaps the filenames are incorrect.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Apr 2011
    Posts
    14
    Does that mean there is nothing wrong with the code? The filenames are correct, however I am using XCode and am not sure if there is a particular place the files are meant to go... I've added them to the project and they are sitting in the same directory as the main file - is that all I am meant to do?

  4. #4
    Registered User
    Join Date
    Apr 2011
    Posts
    14
    That indeed was the issue. XCode Help gave me no joy, but I found the answer here How can I read files in C using xCode? - MacRumors Forums

    Cheers & thanks Laserlight!

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    If you're running the code from inside the IDE, then the "current directory" could be
    - the project root directory
    - the executable directory

    Most IDEs allow you to configure the working directory for the executable.

    Or you could try specifying an absolute path when prompted for a filename.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 01-04-2007, 03:38 PM
  2. Replies: 6
    Last Post: 10-23-2006, 07:22 PM
  3. Replies: 12
    Last Post: 03-10-2005, 07:48 PM
  4. stuck with file system ....
    By stalker in forum C++ Programming
    Replies: 3
    Last Post: 10-27-2003, 12:28 PM
  5. Newbie - file i/o - I'm stuck
    By djacko in forum C++ Programming
    Replies: 1
    Last Post: 02-22-2002, 09:56 AM