fopen() c:\...\

This is a discussion on fopen() c:\...\ within the C Programming forums, part of the General Programming Boards category; hi all! I'm curious about fopen() function. For exmaple: Code: fopen("c:\windows\test","r"); won't work. But if i change \ for / ...

  1. #1
    ipe
    ipe is offline
    Registered User
    Join Date
    Jan 2003
    Posts
    52

    Question fopen() c:\...\

    hi all!
    I'm curious about fopen() function.
    For exmaple:
    Code:
    fopen("c:\windows\test","r");
    won't work.
    But if i change \ for /
    then
    Code:
    fopen("c:/windows/test","r");
    will work fine.

    But if ask for the user enter the path and the user write c:\windows\test will work.
    How do it work?

  2. #2
    .........
    Join Date
    Nov 2002
    Posts
    303
    Try using "c:\\windows\\test"

  3. #3
    ipe
    ipe is offline
    Registered User
    Join Date
    Jan 2003
    Posts
    52
    I still not understanding why it accepts when the user write it but not when i write it on code.
    Code:
    scanf("%s",&path);
    if (file = fopen(path,"r")) ...;
    in this case, if i set path to "c:\windows\..." inside the code it won't work. but if the user type in the same it will work

  4. #4
    ipe
    ipe is offline
    Registered User
    Join Date
    Jan 2003
    Posts
    52
    yes, i know it!!!!
    but why the user needn't to write c:\\...\\ ?

  5. #5
    .........
    Join Date
    Nov 2002
    Posts
    303
    It's because the backslash character \ is used to format text with printf, so to display the actual \ character you need to use \\. There is \n , \t, \f, \r, and \b. I think there is more but I dunno about them. Hope that clears up the why part for you. Salems example shows how it works so im sure you got that part working.

  6. #6
    ipe
    ipe is offline
    Registered User
    Join Date
    Jan 2003
    Posts
    52
    owwwhh!!
    i got it now!
    in c: "\" is "\\"
    and the scanf converts \ to "\\"
    thanks!

  7. #7
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    >i got it now! in c: "\" is "\\" and the scanf converts \ to "\\"

    You've got it backwards. In C source the \\ is represents \. The scanf passes \ as \. This is why you write "c:\\windows\\test" in source code, but enter "c:\windows\test" at the for scanf.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

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, 06:28 AM
  2. fopen help.
    By chriscolden in forum C Programming
    Replies: 17
    Last Post: 01-13-2006, 06:27 AM
  3. problem with fopen command
    By emon in forum C Programming
    Replies: 2
    Last Post: 03-12-2004, 12:11 AM
  4. fopen() and open()
    By Encrypted in forum C Programming
    Replies: 8
    Last Post: 02-09-2003, 04:57 PM
  5. fopen vs open
    By rotis23 in forum Linux Programming
    Replies: 5
    Last Post: 12-10-2002, 02:30 PM

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