struct help

This is a discussion on struct help within the C Programming forums, part of the General Programming Boards category; hi guys i have so far read a file into buffer used strtok() to identify each part of the struct ...

  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    17

    struct help

    hi guys

    i have so far read a file into buffer

    used strtok() to identify each part of the struct

    added these into a struct, printed this struct.

    read the next values into the struct and so on...

    i need to be able to store these structs, in an array maybe or something

    that allows me to sort through them


    anyideas?

    pm or email me if you need the source code to even understand what i just said, i know it doesnt make to much sense, kind of hard to explain.

    your thankfully

    Techevo

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by techevo View Post
    i need to be able to store these structs, in an array maybe or something
    You mean like perhaps a struct array?

    Code:
    struct example {
          int a;
          char *b;
    };
    struct example myarray[10];
    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
    Dec 2009
    Posts
    17
    my struct looks like this might be wrong way to do it mind

    <code>
    typedef struct {
    char name[12];
    int quantum;
    int priority;
    } processDetails;

    processDetails process;

    </code>

    and the array would need to possibly be smaller or larger each time, depending on the size of the file, but im not too worried about that.

    thanks for your quick reply

    techevo

  4. #4
    Registered User
    Join Date
    Dec 2009
    Posts
    17

    the source code

    this is the course code in complete

    gcc sched.c -o sched

    #./sched <filename>
    Last edited by techevo; 12-23-2009 at 09:47 AM.

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Well, there is a problem on line 103, where you intend to read the data in:
    Code:
    token = strtok(NULL, " ");
    process.quantum[i] = (int)token;
    The 2nd line implies that the data was serialized in the file -- in other words, written as an int, not a string representation of a number. Eg, if you wrote the data like this "someword 1234", 1234 is not an int, it is a string.

    In any case, strtok does not place the data into an int, it places it into a string. You cannot have actual int data in a string, because an int can contain a zero byte '\0' which terminates the string. It would be impossible to use strtok here anyway, since it reads to some terminating character, which there are no single byte values which are not a part of a legal int. What you could use is something like:
    Code:
    fread(myint, sizeof(int), 1, fp);
    But I am guessing the numbers are just written into the file as text, right? In that case, you want to use strtol() to place the number into process.quantum. You can't simply cast a string as int to convert the value.

    I think maybe you need to explain more specifically the problems you are having, since clearly this was a problem, rather than saying "I am having trouble trying to figure out how to do this" and not mentioning what has gone wrong.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help me please.
    By yann in forum C Programming
    Replies: 15
    Last Post: 09-29-2009, 09:04 PM
  2. help with structs and malloc!
    By coni in forum C Programming
    Replies: 20
    Last Post: 09-14-2009, 05:38 PM
  3. Replies: 1
    Last Post: 12-03-2008, 02:10 AM
  4. Replies: 10
    Last Post: 05-18-2006, 11:23 PM
  5. What's wrong with my search program?
    By sherwi in forum C Programming
    Replies: 5
    Last Post: 04-28-2006, 09:57 AM

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