fopen help.

This is a discussion on fopen help. within the C Programming forums, part of the General Programming Boards category; Hi, I am having a problem with fopen. I have searched the forum but with no luck. i am trying ...

  1. #1
    Registered User chriscolden's Avatar
    Join Date
    Jan 2006
    Posts
    32

    fopen help.

    Hi,

    I am having a problem with fopen. I have searched the forum but with no luck.

    i am trying to open a binary file using fopen. I am using Borland C++ 3.1 and it cannot open the file. fopen gives a NULL pointer. When opening the same program in Turbo C++ for Windows it seems to work correctly.

    the file is stored in the follow location

    J:\dsaaIII\ass1\Program 3\DATABASE.BIN

    This is my code

    Code:
    char fname[]="J:\\dsaaIII\\ass1\\Program 3\\DATABASE.BIN";
    
    FILE * ptrFile;
    
    ptrFile=fopen(fname, "rt");
    
    if(ptrFile==NULL){
             printf("\n Error Opening Database File!\n\n Please Press Any Key To Continue >");
             getch();
    
             clrscr();
    
             return 0;
    }
    
    fread( lastRecordID, sizeof(int), 1, ptrFile );
    
    fclose(ptrFile);
    The above is a code snippet. The actual fuction is more complex. however it should still be able to open the file. which it is clearly failing.

    Thanks in advance.

    Chris

  2. #2
    Registered User
    Join Date
    Nov 2004
    Location
    India
    Posts
    515
    You probably want to open the file in binary mode. Use "rb" instead of "rt" as your second parameter to fopen()
    Code:
    >+++++++++[<++++++++>-]<.>+++++++[<++++>-]<+.+++++++..+++.[-]>++++++++[<++++>-] <.>+++++++++++[<++++++++>-]<-.--------.+++.------.--------.[-]>++++++++[<++++>- ]<+.[-]++++++++++.

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >J:\\dsaaIII\\ass1\\Program 3\\DATABASE.BIN
    Borland C++ 3.1 probably doesn't support directories/filenames longer than eight characters. Under DOS, "Program 3" would probably equate to a name like "Progra~1".

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,045
    Turbo C++ might only support 8.3 filenames. Or else you might need quotes:
    Code:
    char fname[]="\"J:\\dsaaIII\\ass1\\Program 3\\DATABASE.BIN\"";
    If you support only 8.3 filenames, find out the DOS name for "Program 3".
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,045
    Under DOS, "Program 3" would probably equate to a name like "Program~1".
    Under Windows-based DOS, you mean, like Windows 98. Windows XP will give it a random 8.3 name. To find out what it is, type this from a command prompt:
    Code:
    C>J:
    
    C>cd \dsaaIII\ass1\
    
    C>dir "Program 3"
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >Windows XP will give it a random 8.3 name.
    Are you sure? Because under the Windows XP version I'm using it doesn't appear to be random.

  7. #7
    Registered User chriscolden's Avatar
    Join Date
    Jan 2006
    Posts
    32
    Hey, thanks for your help so far.

    I got...

    Directory of J:\dsaaIII\ass\Program 3

    .... does this mean its ok??

    Chris

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,045
    Oops, my mistake. Use
    Code:
    C>dir program*
    instead, and it might print something like
    Code:
    qwerty <DIR> 11-11-11 12.00p Program 3
    The "qwerty" part is the part you want. See what it is.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  9. #9
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,045
    >Windows XP will give it a random 8.3 name.
    Are you sure? Because under the Windows XP version I'm using it doesn't appear to be random.
    Windows 2k/2k3/xp Long Filename Truncation
    *edit*DWKS: if you try what he says, you see that the file names don't end up looking like that, copy of text document.txt, turns into co0dfb~1.txt for example.
    I thought it would be like that, too, but it seems random to me.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  10. #10
    Registered User chriscolden's Avatar
    Join Date
    Jan 2006
    Posts
    32
    i have just tried that but cannot seem to make it display anythin random.

    Chris

  11. #11
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,045
    Can you post the output of the dir command?

    Unless the double-quote thing solved the problem, in which case don't bother.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  12. #12
    Registered User chriscolden's Avatar
    Join Date
    Jan 2006
    Posts
    32
    Thanks for all your help.

    I have got it working now.

    It was because i was typing the wrong thing into dos. I have got the shorter name for the folder Program 3 and it seems to work ok now.

    Thanks again

    Chris

  13. #13
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >I thought it would be like that, too, but it seems random to me.
    Well I wasn't using the dir command, but instead was using a 16-bit windows program, and it wasn't coming up random. Tomorrow I'll try the dir command and see what it displays.

  14. #14
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Ok, I tried the dir command on my XP machine, and the 8.3 filenames are not random. Maybe someone else can try it on XP and see what they get.

  15. #15
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,045
    I would expect it to be non-random. But my link seems to suggest otherwise.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

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

Similar Threads

  1. help with stat() and fopen()
    By movl0x1 in forum C Programming
    Replies: 6
    Last Post: 07-25-2007, 05:28 AM
  2. fopen and encoding of "*filename" argument.
    By techi_talk in forum C Programming
    Replies: 4
    Last Post: 05-16-2006, 11:36 PM
  3. problem with fopen command
    By emon in forum C Programming
    Replies: 2
    Last Post: 03-11-2004, 11:11 PM
  4. fopen() and open()
    By Encrypted in forum C Programming
    Replies: 8
    Last Post: 02-09-2003, 03:57 PM
  5. fopen vs open
    By rotis23 in forum Linux Programming
    Replies: 5
    Last Post: 12-10-2002, 01:30 PM

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