Thread: A question about using "fscanf_s" to read data in the txt file.

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

    A question about using "fscanf_s" to read data in the txt file.

    Dear all,

    In my app, my program has to read a set of data (each per line) in the txt file. I used the following statement in order to do this, i.e. while (fscanf_s(fp_t, "%ld\n", &number)), where the type of "number" is unsigned int.

    While, if there is a number, e.g.9512312222222222222222222, in the txt file, no matter which format I specified in fscanf_s (%u or %ld), the data read from the file is 4087407502.

    However, if my program, such "9512312222222222222222222" in the file which actually exceeds the upper limit of "unsigned int" should be reported as "an error". How can I implement it?

    Can you give me a hand with it?

    Thanks in advance,

    Bests,

    Qing

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You need to use a 64-bit integer. Unsigned long long should do the trick. There's a format specifier for that, as well, but I'm not 100% sure what it is. %ull or something I believe.
    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
    i've lost my mind
    Join Date
    Jun 2008
    Posts
    26
    . you could use a 64bit int <-- best bet for ya

    if you scaled this...

    . without having to use a large number lib, you could check input and see if it is equal to the max value of int/long/etc and throw an error; "value may* exceed ..."

    . you could use a large number lib and just do a comparison, if a > b. throw error based on that.

    . you could count the number of integers for the input by reading it in as const char * ("%s" format), and starting with the most significant integer checking each value to see if it ever 'exceeds' using 'atoi()'. throw error based on that.
    Last edited by gltiich; 06-16-2008 at 11:20 AM.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Ah, also, read the line as a string, then use strtox (64-bit version) [not sure there is one...] to convert it a number.
    The strtox series will set an error flag if overflow occurs.
    This may or may not help...
    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.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Hmm - that number is 25 digits, so I guess than 19 digits of a 64-bit integer would not be sufficient.

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

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Really? I got it to hex:
    7A94 E8FA F3A0 E38E
    Which is less than
    FFFF FFFF FFFF FFFF

    So it should fit.
    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
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Elysia View Post
    Really? I got it to hex:
    7A94 E8FA F3A0 E38E
    Which is less than
    FFFF FFFF FFFF FFFF

    So it should fit.
    7A94E8FAF3A0E38E = 8832940933707260814.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Huh. I guess Windows Calc has a bug.
    Fortunately, the PowerCalc can calculate it and it seems it results to...
    7DE4F 7A94 E8FA F3A0 E38E

    Yep, too big for a 64-bit integer to handle.
    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
    Registered User
    Join Date
    Dec 2006
    Posts
    26
    Quote Originally Posted by Elysia View Post
    Really? I got it to hex:
    7A94 E8FA F3A0 E38E
    Which is less than
    FFFF FFFF FFFF FFFF

    So it should fit.
    Hey guys,

    At first, thanks for all your replies.

    Secondly, I plan to adopt the way of reading the line in terms of string at first, then convert it to long int meanwhile with the checking. The data in txt file is "951231134972364976" chosen randomly.

    However, when I used the following statement i.e. fscanf_s(fp_t, "&#37;s\n", temp), where temp is a string pointer, and initialized to be NULL. The debugger returns an "Access violation" error...

  10. #10
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by Elysia View Post
    Huh. I guess Windows Calc has a bug.
    Love it
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by qingxing2005 View Post
    However, when I used the following statement i.e. fscanf_s(fp_t, "%s\n", temp), where temp is a string pointer, and initialized to be NULL. The debugger returns an "Access violation" error...
    That's because pointers aren't storage units.
    http://cpwiki.sourceforge.net/A_pointer_on_pointers
    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.

  12. #12
    Registered User
    Join Date
    Dec 2006
    Posts
    26
    Quote Originally Posted by Elysia View Post
    That's because pointers aren't storage units.
    http://cpwiki.sourceforge.net/A_pointer_on_pointers
    Hey Elysia,

    I found out this statement works well, i.e. fscanf_s(fp_t, "%s\n", _temp, (size_t) 100), where _temp is a string array.

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You might as well read this about fscanf.
    Also, if _temp is an array of char, then use sizeof(_temp) instead.
    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.

  14. #14
    Registered User
    Join Date
    Dec 2006
    Posts
    26
    Quote Originally Posted by gltiich View Post
    . you could use a 64bit int <-- best bet for ya

    if you scaled this...

    . without having to use a large number lib, you could check input and see if it is equal to the max value of int/long/etc and throw an error; "value may* exceed ..."

    . you could use a large number lib and just do a comparison, if a > b. throw error based on that.

    . you could count the number of integers for the input by reading it in as const char * ("%s" format), and starting with the most significant integer checking each value to see if it ever 'exceeds' using 'atoi()'. throw error based on that. <- lulz
    Hey gltiich,

    I am using "atoi" or "atol" to convert string "9512311349" to integer, while none of them raised any error (actually both of them returned value "1".) and the converted value is "2147483647".

  15. #15
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Do not use atoi/atol; use strtol.
    Quote Originally Posted by MSDN
    strtol returns the value represented in the string nptr, except when the representation would cause an overflow, in which case it returns LONG_MAX or LONG_MIN
    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. read data from file
    By matth in forum C++ Programming
    Replies: 3
    Last Post: 04-21-2005, 09:37 AM
  2. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM
  5. data read problem
    By Supra in forum C Programming
    Replies: 0
    Last Post: 02-03-2002, 07:02 PM