An issue with using fscanf_s to read data like "&1&2" from a file.

This is a discussion on An issue with using fscanf_s to read data like "&1&2" from a file. within the C Programming forums, part of the General Programming Boards category; Dear all, I met a problem about using "fscanf_s" to read string "&1&2" from the file "A.txt". The snippet of ...

  1. #1
    Registered User
    Join Date
    Dec 2006
    Posts
    26

    An issue with using fscanf_s to read data like "&1&2" from a file.

    Dear all,

    I met a problem about using "fscanf_s" to read string "&1&2" from the file "A.txt". The snippet of the code is described as follows,

    char first[5];
    char second[5];
    int number1 = 0;
    int number2 = 0;

    err = fopen_s(&fp_t, "c://A.txt", "r");

    fscanf_s(fp_t, "%s%d%s%d", first, number1, second, number2);

    While the problem is after the above "fscanf_s" is executed, the values of first, number1, second, number2 are not changed, by which means the program fails in reading data. The ideal result should be first[0] = "&", second[0] = "&", number1 = 1, number2 = 2.

    Thereby, can you help me a little bit?

    Thanks in advance,

    Bests,

    Qing
    Last edited by qingxing2005; 05-29-2008 at 04:27 AM.

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,614
    It reads all the "&1&2" as a string.
    You'll want to read a character, to force it to read only one char.
    I'd also like to give you a link: Need help with altering info on a txt file
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    err = fopen_s(&fp_t, "c:\A.txt", "r");
    will most likely fail to open the file in the ROOT of C:. If you use "a.txt" it would fail to open the file at all, since it would translate \a into CTRL-G ('\07'), and that's not a valid filename.

    Use double back-slashes when specifying filenames (or anything else in a C-style string constant).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    %s reads upto whitespace, not upto digit
    something like
    %[^0-9] - should read string till the digit encountered

    also note that scanf needs pointers to int to modify its values...
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  5. #5
    Registered User
    Join Date
    Dec 2006
    Posts
    26
    Dear all,

    thanks for all of you guys' replies.

    For the URL of file, it was my mistake to specify it wrongly. In the real code, the URL is stored in a string, which should follow the right patter, say "//". I changed it already

    For the right solution, as Elysia pointed out, I gotta read the characters one by one.

    thanks,

    Qing.

    Quote Originally Posted by matsp View Post
    Code:
    err = fopen_s(&fp_t, "c:\A.txt", "r");
    will most likely fail to open the file in the ROOT of C:. If you use "a.txt" it would fail to open the file at all, since it would translate \a into CTRL-G ('\07'), and that's not a valid filename.

    Use double back-slashes when specifying filenames (or anything else in a C-style string constant).

    --
    Mats

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,614
    Just pointing out, but a local filename is not an URL.
    "http://blablabla.com" <-- this is an URL
    "C:\test.txt" <-- this is a filename.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    > err = fopen_s(&fp_t, "c://A.txt", "r");

    And this is neither a filename or a URL.

    Well, maybe because fopen accepts /s and \s under windows, but two /s would be redundant.

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,614
    Some functions will accept "C:\\A.txt" (which mostly like "C://A.txt" will be translated to), but others do not, so it isn't safe to rely on it. Only one backslash in Windows filenames.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Elysia View Post
    Some functions will accept "C:\\A.txt" (which mostly like "C://A.txt" will be translated to), but others do not, so it isn't safe to rely on it. Only one backslash in Windows filenames.
    No, you need two backslashes in quoted strings, because one disappears (and depending on the next character(s), it may remove/translate the following character(s) to something you didn't want). If you use forwards slashes, which, as stated, many Windows C libraries will support, then you should use just one.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,614
    Yes, I know, I was mentioning real string and not C-strings.
    Two backslahes is not always a valid filename.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  2. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  3. Please Help Reading Data From A File
    By NickHolmes in forum C Programming
    Replies: 5
    Last Post: 05-29-2005, 11:24 PM
  4. HUGE fps jump
    By DavidP in forum Game Programming
    Replies: 23
    Last Post: 07-01-2004, 10:36 AM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM

Tags for this Thread


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