【问题标题】:Issue with reading a file and saving values on structure读取文件并在结构上保存值的问题
【发布时间】:2017-12-26 21:51:11
【问题描述】:

这是我正在做的一个项目的一部分。基本上我想读取一个名为“Circuit”的文本文件,里面有这个:

电路标题示例

V1 1 0 24

V2 3 0 15

R1 1 2 10000

R2 2 3 8100

R3 2 0 4700

为了给你一些背景信息,这些值代表这样的电路: Circuit example 我编写了一个代码来将所有这些值保存在一个结构中并将它们打印出来以查看它们是否被正确保存。

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

    typedef struct
    {
char type, name, noMaior, noMenor;
int value;
    }line;

    int main(void)
    {
line ramo[10];
FILE *ficheiro;
int i = 0, j;
char titulo[200];

if ((ficheiro = fopen("circuit.txt", "r")) == NULL)
    printf("Error opening file!");

fgets(titulo, 199, ficheiro);
    printf("%s", titulo);

while ((fscanf(ficheiro, "%c%c %c %c %d\n", &ramo[i].type, &ramo[i].name, &ramo[i].noMaior, &ramo[i].noMenor, &ramo[i].value)) != EOF) 
    {
        i++;
        //if (fgetc(ficheiro)=='.')
        //  break;  
    }
    fclose(ficheiro);

for (j = 0; j < i; j++)
    printf("%c%c %c %c %d\n", ramo[j].type, ramo[j].name, ramo[j].noMaior, ramo[j].noMenor, ramo[j].value);

return 0;

}

它输出与文件中相同的文本,这正是我想要的。现在到了棘手的部分,我们必须将“.end”或“.END”放在文件末尾,所以我用这两行注释来扫描文件中的一个点并在遇到一个点时停止读取它但是在将值保存到结构时,它会给我带来一些问题。这是我得到的输出:

电路标题示例

V1 1 0 24

2 3 0 15

1 1 2 10000

2 2 3 8100

3 2 0 4700

“中断”按预期工作,因为如果我在文件中间放一个点,它将停止读取后面的任何内容,但遗憾的是它忽略了第一个字母,并且根据调试工具它正在保存一个 ' ' (空格)代替字母(ramo[].type)。我试图尽可能多地了解 fscanf 和 fgetc 的行为,但我无法得出任何关于为什么会发生这种情况的结论。

PS:尝试翻译一些变量以使其更易于阅读,但有些仍然是葡萄牙语,例如“ficheiro”=file。伙计们,也请放轻松,我刚开始学习编码!

【问题讨论】:

    标签: c structure scanf fgetc


    【解决方案1】:

    使用fgets 从文件中读取每一行。然后您可以检查一个点或使用strncmpstrcmp 来测试特定值。如果未检测到该值,则使用sscanf 解析该行。

    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    typedef struct
    {
        char type, name, noMaior, noMenor;
        int value;
    }line;
    
    int main(void)
    {
        line ramo[10];
        FILE *ficheiro;
        int i = 0, j;
        char titulo[200];
        char input[200];
    
        if ((ficheiro = fopen("circuit.txt", "r")) == NULL)
            printf("Error opening file!");
    
        fgets(titulo, sizeof titulo, ficheiro);
        printf("%s", titulo);
    
        while ( fgets ( input, sizeof input, ficheiro)) 
        {
            if ( '.' == input[0]) {//test for a dot at start of line
                break;
            }
            else {
                if (( 5 == sscanf(input, "%c%c %c %c %d"
                , &ramo[i].type, &ramo[i].name, &ramo[i].noMaior, &ramo[i].noMenor, &ramo[i].value))) {
                    i++;
                }
            }
        }
        fclose(ficheiro);
    
        for (j = 0; j < i; j++)
            printf("%c%c %c %c %d\n", ramo[j].type, ramo[j].name, ramo[j].noMaior, ramo[j].noMenor, ramo[j].value);
    
        return 0;
    }
    

    【讨论】:

    • 哇,干杯,它创造了奇迹!我完全忘记了 sscanf,因为我们在课程中还没有大量使用它。非常感谢您花时间回答我的问题!新年快乐!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-10
    • 1970-01-01
    • 2020-02-17
    • 1970-01-01
    • 1970-01-01
    • 2020-10-18
    相关资源
    最近更新 更多