简体   繁体   English

使用fscanf读取字符串,然后将可变数量的整数转换为EOF

[英]Using fscanf to read a string and then a variable number of integers to EOF

I'm having a little trouble starting a project I have been assigned. 我在开始分配一个项目时遇到了一些麻烦。 I can't use string operators, fgets , etc. I need to scan a file to EOF. 我不能使用字符串运算符, fgets等。我需要将文件扫描到EOF。

Here is an example of the file: 这是文件的示例:

AND 3 2 1
OR 4 5 6
SPECIAL 4 5 6 7

What I tried to do is set up a while loop: 我试图做的是设置一个while循环:

while (fscanf(circuit, "%s", cur_gate) != EOF){

then tried to check what the cur_gate string is: 然后尝试检查cur_gate字符串是什么:

if (cur_gate[0] == 'A'){

The problem is, I don't know how I would scan 3 integers after reading the string. 问题是,我不知道读取字符串后如何扫描3个整数。 Then eventually, once it reads SPECIAL, I need to scan 4 integers. 最终,一旦读取到SPECIAL,我就需要扫描4个整数。

I want to store the first integer in a array called Output, and the rest in an array called INPUT. 我想将第一个整数存储在名为Output的数组中,并将其余的整数存储在称为INPUT的数组中。

So to summarize, how would I fscanf a string and then fscanf a variable amount of integers depending on the string I have read? 因此,总而言之,我将如何fscanf一个字符串,然后fscanf根据读取的字符串可变数量的整数?

Based on what I understood from your question, below code would help you. 根据我对您的问题的理解,以下代码将为您提供帮助。

I assumed, that you just wanted to store all the outputs in just one array, and similarly all the inputs in one array. 我假设,您只是想将所有输出存储在一个数组中,类似地,将所有输入存储在一个数组中。 So, as per your example you want to store 3,4,4 (first integer just after word) in output array. 因此,根据您的示例,您想在输出数组中存储3,4,4(单词后的第一个整数)。 Rest of the integers need to go to another input array. 其余的整数需要转到另一个输入数组。

#include<stdio.h>
#include<string.h>
#include<stdlib.h>

#define MAX_CHAR  256
#define MAX_IN    100
#define MAX_OUT   100

enum
{
    FALSE = 0,
    TRUE
};

int main()
{
    char word[MAX_CHAR];
    FILE *fp;

    int i = 0, in[MAX_IN];
    int o = 0, out[MAX_OUT];

    int flag = FALSE;

    fp = fopen("dict.txt","r");
    if(NULL != fp)
    {
        while(fscanf(fp, "%s", word) != EOF)
        {
            printf("%s\n",word);
            if(0 != isalpha(word[0]))
            {
                flag = TRUE;
            }
            else
            {
                if(TRUE == flag)
                {
                    out[o] = atoi(word);
                    o++;
                    flag = FALSE;
                }
                else
                {
                    in[i] = atoi(word);
                    i++;
                }
            }
        }
        fclose(fp);
    }
    int j;
    printf("INPUT: ");
    for(j=0; j<i; j++)
    {
        printf("%d\t",in[j]);
    }
    printf("\n");
    printf("OUTPUT: ");
    for(j=0; j<o; j++)
    {
        printf("%d\t",out[j]);
    }
    printf("\n");
    return(0);
}

OUTPUT for the example of file you have given 输出为您提供的文件示例

INPUT: 2        1       5       6       5       6       7
OUTPUT: 3       4       4

DISCLAIMER: The code has been compiled and tested in my environment. 免责声明:该代码已在我的环境中编译和测试。 I encourage you to take care of error handling scenarios yourself. 我鼓励您自己处理错误处理方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM