将信息放入列表中

发布于 2024-11-04 04:18:11 字数 255 浏览 0 评论 0原文

我正在用C语言编程... 我有一个问题,

我有这个输入: LIS MAD 4 TP001 TP002 TP003 TP004

我需要做的是扫描所有信息并将其放入列表中...

问题是,TP 的数量是可变的,它可以从 1 到 1000...

请帮助我...我不知道如何做到这一点..

也许一个循环会有所帮助...我不知道。

我的问题仍然是 TP 的数量可变。其余的,我知道...

谢谢!!

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

I have this input:
LIS 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!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

忱杏 2024-11-11 04:18:11

你需要解释一下你在做什么。
这个输入法在哪里?

  • 这是你程序的参数吗? - 所以
    你只需要循环遍历
    参数
  • 还是输入到控制台? -使用 scanf

尝试阅读:

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:

冷…雨湿花 2024-11-11 04:18:11

根据您对 gskpurs 的回答的评论,看来您有一个变量许多行上以空格分隔的字符串的数量?

您需要首先使用 fgets 获取一行,然后使用 sscanf 将每个单词获取为一一次,直到结束字符串(即行尾)。

在进一步的评论中,您提到 LIS/MAD 之后的标记描述了它后面有多少个单词。好的,您的目标是执行以下操作:

  1. 使用 fgets 读取一行。
  2. 阅读“LIS”和“MAD”单词,然后对它们执行您需要执行的操作(或忽略它们)。 (您可以使用sscanf
  3. 使用sscanf读取一个整数,即要跟随的单词数。 (我们将此称为 n 来进行讨论。)
  4. 使用 malloc 分配一个字符串数组(类型为char **),元素数量为n
  5. 读取一个单词并将其存储到字符串数组中,n 次。

如果您需要澄清,请告诉我。


这是一个快速示例:

#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);
}

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);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文