使用 fprintf 和 fscanf 时出错 [英] Error using fprintf and fscanf

查看:56
本文介绍了使用 fprintf 和 fscanf 时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个存档 results.csv,我需要阅读该存档的第一行并将其打印在 output.txt 上.不知何故,它在一切之后打印随机字符,我无法弄清楚出了什么问题.

I have an archive results.csv and I need to read the first line of this archive and print it out on output.txt. Somehow it's printing random characters after everything and I couldn't figure out what is wrong.

命令:a.c results.csv

第一行:date,home_team,away_team,home_score,away_score,Tournament,city,country,neutral

output.txt: date,home_team,away_team,home_score,away_score,tournament,city,country,neutral,(!£,(!£,(!£,(!£,(!£,@,£,(!£,(!£

output.txt: date,home_team,away_team,home_score,away_score,tournament,city,country,neutral,(!£,(!£,(!£,(!£,(!£,@,£,(!£,(!£

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


typedef struct
{
    char *line1;
    char *line1a;
    char *line1b;
    char *team1;
    char *team2;
    char *reason;
    char *city;
    char *country;
    char *neutral_field;

}data;


void open_input(char *argv[], FILE **input)
{       

        if((*input=fopen(argv[1], "r")) == NULL)
        {
            printf("%s not found\n", argv[1]);
                exit(1);
        }

}
void open_output(char *string, FILE **output)
{       

        if((*output=fopen(string, "w")) == NULL)
        {
            printf("%s not found\n", string);
                exit(1);
        }

}

void alloc_data(data *d, int size)
{
d->line1 = (char*)malloc(4*sizeof(char)); 
d->team1 = (char*)malloc(9*sizeof(char)); 
d->team2 = (char*)malloc(9*sizeof(char)); 
d->line1a = (char*)malloc(10*sizeof(char)); 
d->line1b = (char*)malloc(10*sizeof(char)); 
d->reason = (char*)malloc(10*sizeof(char)); 
d->city = (char*)malloc(4*sizeof(char)); 
d->country = (char*)malloc(7*sizeof(char)); 
d->neutral_field = (char*)malloc(7*sizeof(char)); 
}

void store(data *d, FILE *input, FILE **output)
{

    fscanf(input,  "%s,%s,%s,%s,%s,%s,%s,%s,%s", d[0].line1, d[0].team1, d[0].team2, d[0].line1a, d[0].line1b, d[0].reason, d[0].city, d[0].country, d[0].neutral_field );
    fprintf(*output,  "%s,%s,%s,%s,%s,%s,%s,%s,%s\n", d[0].line1, d[0].team1, d[0].team2, d[0].line1a, d[0].line1b, d[0].reason, d[0].city, d[0].country, d[0].neutral_field );


}

int main(int argc, char *argv[])
{
    FILE *input;
    FILE *output;
    char *string = "output.txt";
    int size = 1000;

    open_input(argv, &input);   
    open_output(string, &output);   

    data *d;
    d = (data*)malloc(size*sizeof(data)); 
    alloc_data(d, size);

    store(d, input, &output);

    free(d);

    return 0;
}

推荐答案

fscanf(input, "%s,%s,%s,%s,%s,%s,%s,%s,%s", d[0].line1, d[0].team1,...

上面的代码试图将整行读入d[0].line1,这会导致缓冲区溢出.team1 其余部分将包含未初始化的数据.

The above code tries to read the whole line in to d[0].line1 which causes buffer overflow. team1 and the rest will contain uninitialized data.

您必须将 fscanf 更改如下:

You have to change fscanf as follows:

fscanf(input, "%3[^ ,\n\t],%9[^ ,\n\t],...

其中3是4 - 1,4是d[0].line1

Where 3 is 4 - 1, and 4 is the size of d[0].line1

或者你可以使用 strtok

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

void store(FILE *input, FILE *output)
{
    char buf[500];
    while(fgets(buf, sizeof(buf), input))
    {
        //strip end-of-line from `buf`
        if(strlen(buf))
            if(buf[strlen(buf) - 1] == '\n')
                buf[strlen(buf) - 1] = 0;

        //tokenize with strtok
        char *token = strtok(buf, ",");
        while(token)
        { 
            fprintf(output, "%s", token);
            token = strtok(NULL, ",");
        }
        fprintf(output, "\n");
    }
}

int main(int argc, char *argv[])
{
    FILE *input = fopen("input.txt", "r");
    FILE *output = fopen("output.txt", "w");
    store(input, output);
    return 0;
}

使用上面的代码,您不需要额外的结构.<小时>如果您确实使用数据结构,则必须更加小心.看来您正在尝试创建一个包含 1000 个 data 的数组,但以下仅创建一个超大指针,而不是一个 data

With above code you don't need an additional structure.


If you do use a structure for data, you have to be more careful. It seems you are trying to create an array of 1000 data, but the following only creates one oversized pointer, not an array of data

int size = 1000;
data *d;
d = (data*)malloc(size*sizeof(data)); 
alloc_data(d, size);

另外,对于每个malloc,应该有一个对应的free.

Additionally, for each malloc there should be a corresponding free.

这篇关于使用 fprintf 和 fscanf 时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆