C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 01-10-2006, 03:36 PM   #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
chriscolden is offline   Reply With Quote
Old 01-10-2006, 03:59 PM   #2
Registered User
 
PING's Avatar
 
Join Date: Nov 2004
Location: india
Posts: 493
You probably want to open the file in binary mode. Use "rb" instead of "rt" as your second parameter to fopen()
__________________
Go Petr !!
My Blog
PING is offline   Reply With Quote
Old 01-10-2006, 04:00 PM   #3
Registered User
 
Join Date: Oct 2001
Posts: 2,936
>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".
__________________
http://www.freechess.org
swoopy is offline   Reply With Quote
Old 01-10-2006, 04:00 PM   #4
Frequently Quite Prolix
 
dwks's Avatar
 
Join Date: Apr 2005
Location: Canada
Posts: 7,698
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.
dwks is offline   Reply With Quote
Old 01-10-2006, 04:02 PM   #5
Frequently Quite Prolix
 
dwks's Avatar
 
Join Date: Apr 2005
Location: Canada
Posts: 7,698
Quote:
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.
dwks is offline   Reply With Quote
Old 01-10-2006, 04:21 PM   #6
Registered User
 
Join Date: Oct 2001
Posts: 2,936
>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.
__________________
http://www.freechess.org
swoopy is offline   Reply With Quote
Old 01-10-2006, 04:21 PM   #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
chriscolden is offline   Reply With Quote
Old 01-10-2006, 04:24 PM   #8
Frequently Quite Prolix
 
dwks's Avatar
 
Join Date: Apr 2005
Location: Canada
Posts: 7,698
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.
dwks is offline   Reply With Quote
Old 01-10-2006, 04:26 PM   #9
Frequently Quite Prolix
 
dwks's Avatar
 
Join Date: Apr 2005
Location: Canada
Posts: 7,698
Quote:
>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
Quote:
*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.
dwks is offline   Reply With Quote
Old 01-10-2006, 04:29 PM   #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
chriscolden is offline   Reply With Quote
Old 01-10-2006, 04:33 PM   #11
Frequently Quite Prolix
 
dwks's Avatar
 
Join Date: Apr 2005
Location: Canada
Posts: 7,698
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.
dwks is offline   Reply With Quote
Old 01-10-2006, 04:38 PM   #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
chriscolden is offline   Reply With Quote
Old 01-10-2006, 05:17 PM   #13
Registered User
 
Join Date: Oct 2001
Posts: 2,936
>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.
__________________
http://www.freechess.org
swoopy is offline   Reply With Quote
Old 01-11-2006, 07:28 AM   #14
Registered User
 
Join Date: Oct 2001
Posts: 2,936
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.
__________________
http://www.freechess.org
swoopy is offline   Reply With Quote
Old 01-11-2006, 01:20 PM   #15
Frequently Quite Prolix
 
dwks's Avatar
 
Join Date: Apr 2005
Location: Canada
Posts: 7,698
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.
dwks is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 09:00 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

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