开发者

Put informations into a list

I'm programming in C language... I'm having a problem

I have this input: L开发者_开发百科IS MAD 4 TP001 TP002 TP003 TP004

and what I need to do is to scan all of the information and put it in a list...

The thing is, the number of TP is variable, it can go from 1 to 1000...

Please help me... I have no ideia how to do this..

Maybe a cycle for will help... I don't know.

My problem remains with the variable number of TP's. The rest, I know...

Thanks!!


You need to explain what you are doing. Where is this input?

  • Is it arguments to you program? - So You simply need to loop through the arguments
  • Or input into the console? -Use scanf

Trying reading:

  • http://publications.gbdirect.co.uk/c_book/chapter10/arguments_to_main.html
  • http://en.wikipedia.org/wiki/Scanf


Per your comment for gskspurs' answer, it seems you have a variable number of space-delimited strings on many lines?

You need to start by using fgets to get one line, and then use either sscanf to get each word one at a time, until the end of the string (i.e., the end of the line).

In a further comment, you mentioned that the token after LIS/MAD describes how many words follow it. Okay, so your goal is to do the following:

  1. Read a line using fgets.
  2. Read the "LIS" and "MAD" words, and do what you need to do with them (or ignore them). (You can use sscanf)
  3. Use sscanf to read into an integer, the number of words to follow. (Let's call this n for this discussion.)
  4. Use malloc to allocate an array of strings (type char **), the number of elements being n.
  5. Read a word and store it into the array of strings, n times.

Let me know if you need clarification.


Here's a quick sample:

#define LINE_SIZE 1024

char line[LINE_SIZE];     /* Unfortunately you do need to specify a maximum line size. */

while (fgets(line, LINE_SIZE, stdin) != NULL)
{
    /* If we're here, a line was read into the "line" variable. We assume the entire line fits. */

    char lis_string[4];
    char mad_string[4];
    int num_words;
    int offset;

    char **word_array;

    sscanf(line, "%s %s %d %n", lis_string, mad_string, &num_words, &offset);

    /* Allocate memory for num_words words */
    word_array = malloc(sizeof(*word_array) * num_words);

    int i;
    for (i = 0; i < num_words; ++i)
    {
        int chars_read;

        /* Allocate space for each word. Assume maximum word length */
        word_array[i] = malloc(sizeof(*word_array[i]) * 16);

        sscanf(line + offset, "%s %n", num_words[i], &chars_read);
        offset += temp;
    }

    /* Do something with the words we just got, maybe print them or add them to a file */
    do_something_with_words(lis_string, mad_string, num_words, word_array);

    /* At the end of this loop, lis_string/mad_string/num_words are out of scope, and
       will be overwritten in next loop iteration. We need to free up the word_array
       to make sure no memory is leaked. */

    for (i = 0; i < num_words; ++i)
    {
        free(word_array[i]);
    }
    free(word_array);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜