small io problem

This is a discussion on small io problem within the C Programming forums, part of the General Programming Boards category; For some reason i am having trouble opening a file with fopen. Code: #include <stdio.h> main() { char quit; FILE* ...

  1. #1
    Call me AirBronto
    Join Date
    Sep 2004
    Location
    Indianapolis, Indiana
    Posts
    195

    small io problem

    For some reason i am having trouble opening a file with fopen.

    Code:
    #include <stdio.h>
    
    main()
    {
          char quit;
          FILE* file = 0;
          
          file = fopen("Test", "r+");
          
          if(file == 0)
          {
                  printf("file dose not exist\n");
          }
          
          while((quit = getchar()) != 'q')
          {
                      
          }
    }

    For some reason this will end up printing out "file dose not exist" for all of the following:
    file = fopen("C:\Binary\Test", "r+");
    file = fopen("\Binary\Test", "r+");
    file = fopen("\Test", "r+");
    file = fopen("Test", "r+");

    Yes Test is a file that is in the folder Binary of which is in the C drive.

    What is going on?

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Escape the back-slashes, ie,

    Code:
    file = fopen("C:\\Binary\\Test", "r+");
    Also you should be checking against NULL (to see if you couldn't open the file), and don't forget to call fclose() when you're done!

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    if(file == 0)
    Is a test agains NULL [ish], isn't it?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    ish.

    Yes, but it avoids confusion -- no harm in being explicit (or more explicit).

  5. #5
    Call me AirBronto
    Join Date
    Sep 2004
    Location
    Indianapolis, Indiana
    Posts
    195
    Code:
    #include <stdio.h>
    
    main()
    {
          char quit;
          FILE* file = 0;
    
          file = fopen("C:\\Binary\\Test", "r+");
    
          
          if(file == NULL)
          {
                  printf("file dose not exist\n");
          }
          
          while((quit = getchar()) != 'q')
          {
                      
          }
          
          if(file != NULL)
            fclose(file);
    
    }
    This still dose not work. God i hate doing file io after not doing it for so long kills my mojo every time.

  6. #6
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    fclose(NULL); does nothing, checking against NULL is redundant (since it'll do it again).

    Make sure C:\Binary\Test exists, (with the same upper/lower case) and you have permissions to read & write to it.

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Try a
    Code:
    char *filename = ""C:\\Binary\\Test";
    ...
    // inside if (fp == NULL) 
    perror("unable to open file");
    printf("filename = %s\n", filename);
    That will:
    a) give you an textual form of the error message.
    b) give you an idea of what the compiler thinks the file is called,

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,627
    Make sure you have permission to write and read to the file.
    Besides that, fopen doesn't set an error flag when it fails? I don't have access to doc right now, so I can't confirm.

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Elysia View Post
    Make sure you have permission to write and read to the file.
    Besides that, fopen doesn't set an error flag when it fails? I don't have access to doc right now, so I can't confirm.
    google "man fopen", and yes, fopen sets errno on failure. Which is why I suggested perror().

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    My money is still on permissions or it doesn't exist (hence fopen would fail with r+). Not like it could be on anything else

    > char *filename = ""C:\\Binary\\Test";
    Tisk

  11. #11
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,627
    Maybe not, but it's better to be safe.

  12. #12
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    I would go and check the file extension and included them within the path. Fopen will fail even if file extensions donst match. So make sure the Test.*** ext is and include them.

    ssharish

  13. #13
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Which could also be a very good point if file extensions are hidden on Windows.

  14. #14
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,627
    I just hope no one who is programming is silly enough not to realize that Windows hides them by default and thinks that most files do not have an extension.

  15. #15
    Call me AirBronto
    Join Date
    Sep 2004
    Location
    Indianapolis, Indiana
    Posts
    195
    Boom, it was the extention. I am calling bull $!*% on windows. I right clicked to get the path and it did not say .text.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Small problem with this array...
    By Merholtz in forum C Programming
    Replies: 7
    Last Post: 11-03-2008, 03:16 PM
  2. Help with a small problem (beginner)
    By piffo in forum C Programming
    Replies: 13
    Last Post: 09-29-2008, 04:37 PM
  3. a very small problem, i'm new... please help?
    By Uberverse in forum C++ Programming
    Replies: 9
    Last Post: 11-10-2007, 09:44 AM
  4. File io problem - need help
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 01-16-2002, 01:36 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21