Thread: store string data as a short or other data type

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    14

    store string data as a short or other data type

    I need to read variable-value pairs from a file. Some of the values are character strings, some are integers, and some need to be stored as shorts. I know how to use strtok to tokenize the lines read, and I also know how to convert to int using the atoi function. However, I don't know how to convert a character string to a short value. I was told that I can use fscanf somehow, but I haven't been able to figure out how to use it. Any help is greatly appreciated!

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Here's an example using sscanf:
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main() {
    	short x;
    	char a[]="23432";
    	sscanf(a,"%hd", &x);
    
    	return 0;
    }
    fscanf is the same thing but from a file stream.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Mar 2010
    Posts
    14
    Thanks! Do you know where I can find a list of all the "%hd" things. What does %hd mean? Hexidecimal?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    h means short and d means decimal integer. You probably can find more information by searching the Web for fscanf, sscanf, etc, possibly with "man " prepended.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by robin2aj View Post
    Thanks! Do you know where I can find a list of all the "%hd" things. What does %hd mean? Hexidecimal?
    They are part of the standard, you can have a look here at the c99 draft:

    The C99 Draft (N869, 18 January, 1999)

    "%hd" is a conversion specifier.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #6
    Registered User
    Join Date
    Mar 2010
    Posts
    14
    Thanks for the help guys!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  2. Replies: 6
    Last Post: 04-21-2006, 08:49 PM
  3. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  4. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  5. HUGE fps jump
    By DavidP in forum Game Programming
    Replies: 23
    Last Post: 07-01-2004, 10:36 AM