Thread: Breaking down a char array into a struct

  1. #1
    Registered User
    Join Date
    Dec 2018
    Posts
    5

    Breaking down a char array into a struct

    Hi,

    I have input such as the following:

    Code:
    DC12345 54I91L9
    
    This input is fed into a char array using fgets. I then want to be able to split it into a struct.

    I would therefore define a struct as:

    Code:
    Struct input
    char firstCode
    int secondCode
    char fourthCode 
    char fifthCode
    
    If I had the codes:

    Code:
    DC12345 54I91L9
    AB67891 23LN5TY
    
    they would be split into the following sturcts:

    Code:
    input[0]
    firstCode - DC12345
    secondCode-5 
    fourthCode-4I9
    fifthCode-1L9
    
    input[1]
    firstCode- AB67891
    secondCode-2
    fourthCode-3LN
    fifthCode -5TY
    
    
    Could someone please help as to how I would go about feeding this input into the correct elements of the struct?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    It looks like the codes are of fixed length, so parsing should be a matter of copying the characters at the fixed positions, and then converting to int if necessary.
    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

  3. #3
    Registered User
    Join Date
    Dec 2018
    Posts
    5
    Quote Originally Posted by laserlight View Post
    It looks like the codes are of fixed length, so parsing should be a matter of copying the characters at the fixed positions, and then converting to int if necessary.
    Thank you, I thought along similar lines. Could you give me a idea of how I would go about doing this as I am new to C.

    Thank you

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I would begin with a struct like this:
    Code:
    struct CodeSet
    {
        char firstCode[8];
        int secondCode;
        char fourthCode[4];
        char fifthCode[4];
    };
    I'm curious as to why the jump from second to fourth, but actually you should have descriptive names for the members, not just name the members by numbers.
    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
    Registered User
    Join Date
    Dec 2018
    Posts
    5
    Thank you! How would you reccomend copying the characters?

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by DcoltGaming
    How would you reccomend copying the characters?
    For the array of char members, use strncpy from <string.h>, remembering to null terminate the strings. For the int member, you can directly convert the single digit char to a digit integer, e.g., by subtracting '0'.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-30-2013, 05:45 PM
  2. Struct is Breaking Down
    By TheDenominater in forum C Programming
    Replies: 3
    Last Post: 11-11-2009, 01:43 AM
  3. Change char array in struct
    By Rob4226 in forum C Programming
    Replies: 1
    Last Post: 10-20-2009, 03:52 PM
  4. Struct Char Array Problems
    By RMDan in forum C Programming
    Replies: 5
    Last Post: 06-18-2009, 07:16 PM
  5. initializing char array inside a struct
    By panos in forum C Programming
    Replies: 6
    Last Post: 06-01-2007, 06:43 PM

Tags for this Thread