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

  1. #16
    Registered User
    Join Date
    Dec 2006
    Posts
    26
    Quote Originally Posted by Elysia View Post
    Do not use atoi/atol; use strtol.
    Hey Elysia,

    I used your proposed statement in the following way, i.e.
    Code:
    if (strtol(_temp, NULL, 10)) 		
    {
       number = strtol(_temp, NULL, 10);
    }
    There is no error raised when the value actually exceeds the limitation, instead after the conversion, the value of "number" is "2147483647".

    In above example, _temp is "8729348702983457023894" read from the txt data file.

    Bests,

    Qing

  2. #17
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    As the manual says, it returns LONG_MAX if an overflow occurs. C functions do not throw errors.
    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. #18
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Elysia View Post
    As the manual says, it returns LONG_MAX if an overflow occurs. C functions do not throw errors.
    What do you mean by "throw errors?" I see no exception handling in the example.

    At any rate, strtol() most certainly does inform you if the value under- or overflowed, by setting errno to ERANGE.

    And many standard functions return special values to indicate "error."

  4. #19
    Registered User
    Join Date
    Dec 2006
    Posts
    26
    Quote Originally Posted by Elysia View Post
    As the manual says, it returns LONG_MAX if an overflow occurs. C functions do not throw errors.
    Hey Elysia,

    Thanks for it.

    I fix the problem, i.e. by using LONG_MAX and LONG_MIN.

    Best,

    Qing

  5. #20
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by brewbuck View Post
    What do you mean by "throw errors?" I see no exception handling in the example.
    It was in response to
    There is no error raised...
    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.

  6. #21
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    I don't know if that was meant to refer literally to the raising/throwing of an exception, but just an indication of error state. The OP can certainly check whether overflow occurred by seeing if errno == ERANGE.

  7. #22
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I was referring more to the point that the OP was expecting some "magic" to happen when overflow occurred. Which it obviously doesn't.
    Checking for the error in errno == ERANGE is the error checking, not some "magic".
    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