【问题标题】:sscanf statement stops the programsscanf 语句停止程序
【发布时间】:2015-05-07 03:49:34
【问题描述】:
#include <stdio.h>
#include <stdlib.h>


struct urunler {
    int kod;
    char Ad[16];
    int stok;
    float fiyat;
};

void urunTara(struct urunler* inputs,int *amount);

int main()
{
    struct urunler Urun[50];
    int amount = 0;



    urunTara(Urun,&amount);
}

void urunTara(struct urunler *inputs,int *amount){
    char Temp[150];
    FILE *fPtr;
    fPtr = fopen("urunler.txt","r");
    if(fPtr == NULL){
        printf("File not found!");
    } else {
        while(!feof(fPtr)){
            fgets(Temp,100,fPtr);
            sscanf(Temp,"%d %s %d %f",&(inputs[*amount].kod),inputs[*amount].Ad,&(inputs[*amount].stok),&(inputs[*amount].fiyat));
            *amount++;
        }
    }

};

我对 C 语言比较陌生,刚开始学习结构。文本文件包含以下内容:

25 televizyon 1000 150.25
40 video 500 25.45
50 plazma 75 2300.50
76 dvd 20000 90.00
85 supurge 700 110.75
90 buzluk 250 10.00
95 teyp 1250 8.99 

我在这里遇到的问题是 sscanf。当我在 main 函数中执行所有这些操作时,效果很好。但是,当我尝试在函数 urunTara 中执行此操作时,sscanf 语句出现问题并且程序停止工作。我使用 scanf 成功地将值传递给 &(inputs[*amount].kod) 和其他地址。但是无法理解这个 sscanf 语句有什么问题。

【问题讨论】:

    标签: c file struct fgets scanf


    【解决方案1】:
    *amount++;
    

    类似于

    *(amount++);
    

    这意味着取消引用amount,然后将amount 增加到之后的元素。
    这是不正确的。

    *amount++; 更改为 (*amount)++;,这将增加取消引用的值。

    请参阅thisthis 了解更多详情

    【讨论】:

    • 我认为问题出在 sscanf 上。非常感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-18
    • 2018-02-07
    • 1970-01-01
    • 1970-01-01
    • 2014-07-01
    相关资源
    最近更新 更多