Mail Archives: djgpp/1993/01/17/16:38:25
sngattan AT interceptor DOT ksu DOT ksu DOT EDU (Down and Under) wrotes
> I am working on an IBM-PC and an trying to open a file using
>the "fopen" command. As long as I open the file in the same directory
>where I am running my executable, it works fine. But the moment I
>specify a path..it cannot find that file. Here is the complete
>description of the problem"
>
> I am working in c:\ktran\image directory. I want to open
>a file in c:\ktran\pgm directory...the filename is "crk1.pgm"
>
>****Begin part of the code
>
> char buffer[80];
> buffer = "c:\\ktran\\pgm\\crk1.pgm";
>
> inpfile = fopen (buffer,"rb");
Well, there is inconsistency in using char variable of buffer.
The declaration "char buffer[80]" assigns some memory address to buffer so
that it points to some memory space that can hold 80 chars.
buffer = "c:\\ ..." statement will assign another new address to buffer
that is different from the address already assigned by char buffer[80].
The new memory space (assigned by "c:\\ ...") may not hold a long string.
(Some compilers can allocate a new memory space for this long string, but some
cannot. Short string is possibly okay.) The program is clearly wrongly
written.
So, use strcpy(buffer, "c:\\...\\crk1.pgm"); instead of buffer="c:\\...";.
OR, declare buffer with initialization like char buffer[]="c:\\...";.
(Using / instead of \\ doesn't solve the problem. \\ is fine under msdos.)
- Raw text -