Thread: Opening a data file....or not

  1. #1
    Registered User
    Join Date
    Jul 2013
    Posts
    4

    Opening a data file....or not

    I have attempted a couple of different times to open a .txt file in a program, however it always goes to the "file was not opened." Is there someone who could let me know where I am going wrong? Thank you for taking the time to help!

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
        FILE *inFile;
        
        inFile = fopen("caps.txt","r");
        
        if (inFile == NULL)
        {
            printf("\nThe file was not successfully opened.");
            printf("\nPlease check that the file currently exists.\n");
            exit(1);
        }
        printf("\nThe file has been successfully opened for reading.\n");
        
        return 0;
    }

  2. #2
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Does the file exists in the current working directory in the first place. Check that first. Afterall your opening the file to read not write and this would ofcourse will return NULL if the file doesn't exists.

    ssharish2005
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,907
    Your code is correct, there is nothing wrong with it. One suggestion is to use the perror function, or strerror and errno to print a more useful error message. There are many reasons the file might not be opened, besides that it doesn't exist. So something like:
    Code:
    if (inFile == NULL)
    {
        perror("fopen failed");
        // or
        sprintf("Couldn't open caps.txt: %s\n", strerror(errno));
    }
    Also, I know this isn't the full program, but make sure you remember to close the file in your real code.

    You didn't specify anything about your system. Are you sure caps.txt exists and can be opened/read? If you are using an IDE, it's possible that the "current directory" when you are running your program is not the same as where caps.txt is stored. Often times, an IDE will have some options that control what directory your program is run from, which may be separate from the directory the source files are in. Check into that, if that doesn't help, we'll need more info.

  4. #4
    Registered User
    Join Date
    Jul 2013
    Posts
    4
    Caps.txt does exist, I set it on the desktop so I could find it easily. I'm not sure that I understand what and IDE is, I am very new to all of this. I did check in the compiling program that I am using and I was able to open through the directory window.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    The caps.txt file will only be opened, if your program has the correct path to the file, as well as the file name. By default, files in the same directory as your program, need no path to be included - they program will be able to find them, OK.

    If you put the file anyplace else, you must include the correct path, along with the file's name, in the fopen() statement.

  6. #6
    Registered User
    Join Date
    Jul 2013
    Posts
    4
    I'm sorry I don't understand what you mean. How do I check to see if the path is the same? I understand the program needs to find it, I'm just not understanding how to make that happen.

    [QUOTE=Adak;1170665]The caps.txt file will only be opened, if your program has the correct path to the file, as well as the file name. By default, files in the same directory as your program, need no path to be included - they program will be able to find them, OK.

  7. #7
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    I would suggest that if you dont know how to check the IDE setting I would change the path to fopen to absolute path. Thats is change the call to fopen to something as follow

    Code:
    iFile = fopen( "c:/<where the file is>/caps.txt", "r" );
    And check that works?

    ssharish2005
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  8. #8
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,907
    An IDE is an Integrated Development Environment. It's a program that combines an editor, compiler and debugger all in one place. Some people like that, some (like me) don't. Programs like Code::Blocks, Eclipse and Visual Studio are IDEs.

    As for your file, think about this: If you had a file caps.txt on your desktoip and one in your documents folder, which should the program open? Even if there is only one, how long do you think it might take the program to search the entire hard drive looking for it. As Adak said, it only searches the current working directory, unless you give a complete path to the file. Your options are move caps.txt to the current working directory, or to specify the full path, something like
    Code:
    fopen("C:\\Users\\YourUserName\\Desktop\\caps.txt", "r");
    Note, that's probably not exactly the right path, but you should be able to figure the real path out. And why the \\ you might ask, instead of a single \ ? Well, \ signifies an escape sequence, which allows you to represent characters that are otherwise hard/impossible to type -- link). You need a \\ inside string and char literals to get a regular backslash.

  9. #9
    Registered User
    Join Date
    Jul 2013
    Posts
    4
    Thank you!!! Thank you!! I appreciate the help so much! It worked

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File opening
    By Benji Wiebe in forum Linux Programming
    Replies: 11
    Last Post: 01-02-2012, 09:23 PM
  2. Replies: 6
    Last Post: 11-30-2011, 12:49 AM
  3. Opening file and loading data into Vector
    By soopah256 in forum C++ Programming
    Replies: 4
    Last Post: 08-04-2009, 10:18 PM
  4. NOOB: Need a little help opening file and manipulating data
    By liquidcourage1 in forum C++ Programming
    Replies: 7
    Last Post: 02-26-2006, 10:27 PM
  5. File Opening
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 04-24-2002, 03:50 PM