Thread: File Path Setting

  1. #1
    Registered User
    Join Date
    Dec 2003
    Posts
    92

    Question File Path Setting

    how to give the absolute path in VC++

    suppose fp = fopen( "c:\\fold\\vol\\ file.txt ","r");


    is it correct? c:\\fold\\vol\\ file.txt

    many times i confuse whether to use single slash or double slash. or mixed ..... and thats why my file does not open.


    what is the correct syntax to give path ????
    blue_gene

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >many times i confuse whether to use single slash or double slash.
    In your example, \f and \v are both standard escape characters for a form feed and a vertical tab, so you need to double the backslashes as you did. Your example is correct except for the embedded whitespace possibly causing a problem.

    >and thats why my file does not open
    When fopen fails, be sure to call perror to see why it failed. This can give you a hint as to what the problem may be so that you can fix it more easily:
    Code:
    fp = fopen ( "c:\\fold\\vol\\file.txt", "r" );
    if ( fp == NULL )
      perror ( NULL );
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Dec 2003
    Posts
    92
    i was usuing \\ blindly......i donot know why it is usued??

    All i know in DOS if i have to specify path then i have to use single slash

    e.g c:\fold\vol ----->something like this.....

    Then why in VC++ it is exceptional !!!!!! why do i have to use \\ (instead of single slash).

    thanks
    blue_gene

  4. #4
    King of the Internet Fahrenheit's Avatar
    Join Date
    Oct 2001
    Posts
    128
    Because \ is the escape character. For instance, if you tried to fopen

    "dir\no.txt"

    it would try to open

    "dir
    o.txt"

    To get a literal \ you have to escape twice.

    To sum it up, \\ = \

  5. #5
    Registered User
    Join Date
    Dec 2003
    Posts
    92
    ok.....from the standard escape table i see....

    \n -----new line charater

    dir\no.txt" ----so creating trouble....

    and also in my above post c:\\fold\\vol\\ file.txt there was

    \f-----> form feed

    \v-----> vertical tab

    these are also in the escape sequence table. so effectively if i really need a sigle slash( which is like in DOS) i have to use one more slash(to override?)

    from table i see also \\-----> backslash......

    but suppose i have c:\pd\kl ..... i believe i donot have to use \\ here
    bcoz \p and \k are not in the escape sequence table.. so no need to go for \\. Only single slash must work.

    so i should write c:\pd\kl ...... am i right?
    blue_gene

  6. #6
    King of the Internet Fahrenheit's Avatar
    Join Date
    Oct 2001
    Posts
    128
    Originally posted by blue_gene
    ok.....from the standard escape table i see....

    \n -----new line charater

    dir\no.txt" ----so creating trouble....

    and also in my above post c:\\fold\\vol\\ file.txt there was

    \f-----> form feed

    \v-----> vertical tab

    these are also in the escape sequence table. so effectively if i really need a sigle slash( which is like in DOS) i have to use one more slash(to override?)

    from table i see also \\-----> backslash......

    but suppose i have c:\pd\kl ..... i believe i donot have to use \\ here
    bcoz \p and \k are not in the escape sequence table.. so no need to go for \\. Only single slash must work.

    so i should write c:\pd\kl ...... am i right?
    No, you'll probably get a warning if you try to escape something not in the table. You'd write "c:\\pd\\kl"

    Also, "c:\\fold\\vol\\ file.txt" wont contain a form feed and vertical tab because the \ is escaping another \, not f or v.

    Otherwise, yeah. To get a single slash, you have to "override" it with another slash.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > bcoz \p and \k are not in the escape sequence table.. so no need to go for \\. Only single slash must work.
    No, backslash followed by any lower-case letter is reserved, even if it doesn't actually do anything at the moment.

    The only safe thing to do is write a pair of \\ every time you want a single \
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Registered User kinghajj's Avatar
    Join Date
    Jun 2003
    Posts
    218

    Also...

    If I remember correctly, fopen doesn't take pointers...
    so:
    Code:
    fopen("C:\\DIR\\FILE","r");
    is incorrect.

    Instead, you should try something like this:
    Code:
    char filename[] = "C:\\DIR\\FILE";
    fopen(filename,"r");

  9. #9
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Your two examples are equivalent, kinghajj. First, fopen does indeed take pointers, here is the prototype:
    Code:
    FILE *fopen ( const char *filename, const char *mode );
    And since an array name in an expression (such as an argument to a function) is converted to a pointer to the first element, you are effectively passing fopen a char *.
    My best code is written with the delete key.

  10. #10
    Registered User kinghajj's Avatar
    Join Date
    Jun 2003
    Posts
    218

    really?

    Whenever I do the first method, my program either has compiler errors or crashes when I try to open....

  11. #11
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >my program either has compiler errors or crashes when I try to open....
    Can you post a compilable program that does this and a list of the errors that you get?
    My best code is written with the delete key.

  12. #12
    Registered User kinghajj's Avatar
    Join Date
    Jun 2003
    Posts
    218

    porbably not.

    I don't remember where that was from... but it was from a while ago. But it doesn't matter now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  2. To find the memory leaks without using any tools
    By asadullah in forum C Programming
    Replies: 2
    Last Post: 05-12-2008, 07:54 AM
  3. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  4. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM