Results 1 to 9 of 9

Thread: fopen() problem!

  1. #1
    Junior Member
    Join Date
    Aug 2004
    Posts
    25

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

  2. #2
    Custom User
    Join Date
    Oct 2001
    Posts
    503
    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

  3. #3
    Senior Member
    Join Date
    Jul 2002
    Posts
    339

    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


  4. #4
    Junior Member
    Join Date
    Aug 2004
    Posts
    25
    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

  5. #5
    Leftie Linux Lover the_JinX's Avatar
    Join Date
    Nov 2001
    Location
    Beverwijk Netherlands
    Posts
    2,534
    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 !

  6. #6
    well, in C the '\' is used to print certain characters (like " \ ) to deactivate their normal values/
    hope this makes sense

    just like you need a '\n' for a newline, instead of just an 'n', so also works the '\'. normally the '\' is used in combination with the character right after it, and therefore you need to deactivate it using '\\' to have just the one backslash with no value.

    i really hope this makes sense, since at this moment, i wouldn't know how to make it more clear


    **EDIT**well, he posted it at the same time , but i think his explanation is more clearer **/EDIT**

  7. #7
    Custom User
    Join Date
    Oct 2001
    Posts
    503
    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

  8. #8
    Junior Member
    Join Date
    Aug 2004
    Posts
    25
    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

  9. #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
  •