-
September 9th, 2004, 11:13 PM
#1
Junior Member
fopen() problem!
Hi there,
I have been trying to develop a program in the C language, although have struck a problem. I have written the following line of code, although it does not work in the way in which I had intended.
f=fopen ("C:\Binary","w");
I wanted this code to create a file on the C drive of my computer and open it, ready to write to. Although the code creates the file in which every directory I execute the file from, not at the file path which I have specified.
Any help or thoughts will be much appreciated.
Thank You, in advance.
-
September 10th, 2004, 12:16 AM
#2
I've written up an example quickly to see if what you were doing was correct (hadn't used fopen before)...worked fine for me. Here it is, so have a look at yours and see if there's anything obviously different about the io stuff:
Code:
#include <stdio.h>
int main(int argc, char *argv[])
{
FILE *f = fopen("../test.io", "w");
fprintf(f, "BLAH");
if(fflush(f)==EOF)
{
printf("ERROR: could not flush buffer");
return(0);
}
if(fclose(f)==EOF)
{
printf("ERROR: could not close file");
return(0);
}
return(0);
}
Hope that helps you somewhat.
ac
-
September 10th, 2004, 05:06 AM
#3
Re: fopen() problem!
Originally posted here by TTAYO
f=fopen ("C:\Binary","w");
TTAYO, try this:
f=fopen ("C:\\Binary","w");
(note the double slashes).
What's your compiler anyway? I tried your code on gcc and f returned NULL (it didn't even create the file in current directory).
Peace always,
<jdenny>
Always listen to experts. They\'ll tell you what can\'t be done and why. Then go and do it. -- Robert Heinlein
I\'m basically a very lazy person who likes to get credit for things other people actually do. -- Linus Torvalds
-
September 10th, 2004, 07:09 AM
#4
Junior Member
Thanks jdenny, your \\ worked. Although I don't understand why, could you explain.
btw, I am using visual c++ to compile this c code.
L8R, TTAYO
-
September 10th, 2004, 07:34 AM
#5
The single \ escapes the next character..
The most common escape-sequences are:
Code:
\a Bell (alert)
\b Backspace
\n Newline
\t Horizontal tab
\\ Backslash
\? Question mark
\\' Single quotation
\" Double quotation
Microsoft has unfortunately chosen the backslash as their path devider, while the other osses use the slash..
ASCII stupid question, get a stupid ANSI.
When in Russia, pet a PETSCII.
Get your ass over to SLAYRadio the best station for C64 Remixes !
-
September 10th, 2004, 07:36 AM
#6
-
September 10th, 2004, 10:06 AM
#7
Basically, the single slash is an escape character (like the others said). LOL...I didn't even think about that. (serves me right for writing it on linux :P
ac
-
September 10th, 2004, 02:03 PM
#8
Junior Member
Hi there, its me again.
jdenny said that when using gcc, to compile the program, the compiled program didn't execute as it did when I compiled the program using visual c++. Although I thought that the code would be compiled into the same executable, whether one uses visual c++ or gcc to compile the code.
Could anyone tell me if I am correct or not?
Thanx,
L8R, TTAYO
-
September 10th, 2004, 04:04 PM
#9
jdenny said that when using gcc, to compile the program, the compiled program didn't execute as it did when I compiled the program using visual c++. Although I thought that the code would be compiled into the same executable, whether one uses visual c++ or gcc to compile the code.
Could anyone tell me if I am correct or not?
(assuming gcc for linux) no it would not, otherwise you could run every program written in linux on a windows computer and visa versa (without the use of wine or cigwin).
i can't tell you exactly whats the difference, but even between different kernel versions you might need to recompile the program.
as for different compilers and same OS, they all have their own rules and ways to compile, one compiler might be compiling for performance, while the other might compile for lesser size. this is just one of the differences..
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
|