Another fopen() problem

This is a discussion on Another fopen() problem within the C Programming forums, part of the General Programming Boards category; I get the following error: FILE *fpsource; /* source data file : LDR file to be flashed at given offset ...

  1. #1
    Registered User
    Join Date
    Aug 2008
    Posts
    1

    Another fopen() problem

    I get the following error:

    FILE *fpsource; /* source data file : LDR file to be flashed at given offset */

    if ( (fpsource = fopen ("ldr1_6Aug.ldr", "r")) != NULL )
    printf("Cannot open an LDR file.\n");
    else printf("LDR file opened for writing to Flash.\n"); /* Remove */

    if ( fseek ( fpsource, 0L, SEEK_END) != 0 )
    fprintf(stderr, " Seek to end failure : %s\n", strerror ( errno ));// comes here: WHY?
    size = ftell (fpsource); /* size in bytes of the opened file */
    printf("size first: %d\n", size); /* remove */
    if ( fseek ( fpsource, 0L, SEEK_SET) != 0)
    fprintf(stderr, "Seek to start error : %s\n", strerror ( errno ));
    rewind (fpsource); /* the same as line above which appears to be ineffective here ? */

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    How about using code-tags instead of removing the
    Code:
     {}
    in your code?

    --
    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.

  3. #3
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Yeah, how are we supposed to tell what statements are encompassed by those if statements?

    Plus, what error do you actually get?

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,793
    Code:
    if ( (fpsource = fopen ("ldr1_6Aug.ldr", "r")) != NULL )
    printf("Cannot open an LDR file.\n");
    else printf("LDR file opened for writing to Flash.\n"); /* Remove */
    First off, the file is opened successfully if fopen does not return NULL, not the other way around that the above seems to be suggesting. You may be getting a "file opened" message when in reality it should be saying "cannot open" because your logic is switched around the wrong way. Second, you are opening the file in read mode, not write mode so the statement in the 2nd printf above is misleading. Third, it's difficult to determine if you are perhaps calling functions that you should not (brackets would help here) - for instance if the file wasn't opened correctly are you still trying to call those fseek functions later on?

    I second the request for you to use code tags [code][/code].
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  2. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  3. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  4. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 05:24 PM
  5. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM

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