fscanf to read string between " mark

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Matrixinline

    fscanf to read string between " mark


    Hi All,

    File Text.txt
    Contains following text as :
    "C:\program file\applicatio n data\details\ap p" "D:\Program File"


    I tried to read that data as
    fscanf(oFp, "%s %s", sCopyDirectory, sToDirectory);

    it gives me
    sCopyDirectory = "C:\program
    sToDirectory= file\applicatio n

    How can I read a full string between a two double quote mark from a
    file?

    Well we can read "C:\program file\applicatio n data\details\ap p" "D:
    \Program File"
    in two character pointers easily. I need the same solution but reading
    from a file.

    Thanks
    Anup
  • Jens Thoms Toerring

    #2
    Re: fscanf to read string between " mark

    Matrixinline <anup.kataria@g mail.comwrote:
    Hi All,
    File Text.txt
    Contains following text as :
    "C:\program file\applicatio n data\details\ap p" "D:\Program File"
    I tried to read that data as
    fscanf(oFp, "%s %s", sCopyDirectory, sToDirectory);
    it gives me
    sCopyDirectory = "C:\program
    sToDirectory= file\applicatio n
    How can I read a full string between a two double quote mark from a
    file?
    Try

    fscanf( fp, "\"%[^\"]\" \"%[^\"]\"", sCopyDirectory, sToDirectory );

    The '%[^\"]' tells fscanf() to read everything up (but not
    including) the next double quote. You may want to add the
    maximum number of characters that fit into the target string
    between the '%' and the '[', otherwise you may try to write
    more characters to the target string than fits in there (and
    don't forget about the trailing '\0' character that also needs
    to fit in there;-)
    Well we can read "C:\program file\applicatio n data\details\ap p" "D:
    \Program File"
    in two character pointers easily. I need the same solution but reading
    from a file.
    What means "Well we can read"? Reading from standard input and
    a file doens't make any noticable difference here. And be care-
    ful with your terminology, you can't "read into a character
    pointer", you can only write to where a character pointer points
    to, and that, of course, only if it points to enough memory
    you own.
    Regards, Jens
    --
    \ Jens Thoms Toerring ___ jt@toerring.de
    \______________ ____________ http://toerring.de

    Comment

    Working...