简体   繁体   English

fprintf将错误的数据保存到文件

[英]fprintf saves wrong data to file

I have an input file a.txt : 我有一个输入文件a.txt

1 abc 3
2 efgh 4.5
3 text 3
4 xyz 2

So basically, it has 3 columns, first one is int, second is text, and third is double. 所以基本上,它有3列,第一列是int,第二列是text,第三列是double。 I need to read this file by rows (which actually works, I guess), but have some problems with writing only second and third column to another ( b.txt ) file. 我需要按行读取此文件(我想这实际上是有效的),但是在仅将第二列和第三列写入另一个( b.txt )文件时存在一些问题。 fprinft saves something like this: fprinft保存以下内容:

 0.000000
 0.000000
 0.000000
 0.000000
xvæ$ 0.000000

instead of 代替

abc 3
efgh 4.5
text 3
xyz 2

I simply need to save only the second and the third column from a.txt file to b.txt file. 我只需要将第二列和第三列从a.txt文件保存到b.txt文件。 Here's my code: 这是我的代码:

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

typedef struct mypair
{
    char string[1024];
    double number;
} mypair;

void zero_string(char *string, int n)
{
    int i;
    for(i=0; i<n; i++)
        string[i] = '\0';
}

int row(FILE* f, struct mypair *p)
{
    int num;

    if(!feof(f))
    {
        if(fscanf(f,"%d %s %lf", &num, p->string, &p->number) == 3)
        {
            return 0;
        }
    }
    else
    {
        return 1;
    }
}

int main(int argc, char **argv)
{
    int n = 5, status = 0, i = 0, j;

    struct mypair array[5];
    char file_in_name[255];
    char file_out_name[255];

    FILE *fin;
    FILE *fout;

    zero_string(file_in_name, 255);
    zero_string(file_out_name, 255);

    printf("Data file:\n> ");
    scanf("%s", file_in_name);
    printf("Out file:\n> ");
    scanf("%s", file_out_name);

    fin = fopen(file_in_name, "r");
    fout = fopen(file_out_name, "w");

    if( fin == NULL )
    {
        exit(-1);
    }
    if( fout == NULL )
    {
        exit(-1);
    }

    while(status != 1)
    {
        status = row(fin, &array[i]);
        i ++;

        fprintf(fout, "%s %lf\n", array[i].string, array[i].number);

        if(i >= n)
            break;
    }

    fclose(fin);
    fclose(fout);

    for(j=0; j<i; j++)
        printf("%s %lf\n", array[i].string, array[i].number);

    return 0;
}

I modified the code, now it works, thanks! 我修改了代码,现在可以了,谢谢!

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

typedef struct mypair
{
    char string[1024];
    double number;
} mypair;

void zero_string(char *string, int n)
{
    int i;
    for(i=0; i<n; i++)
        string[i] = '\0';
}

int row(FILE* f, struct mypair *p)
{
    int num;

    if(!feof(f))
    {
        if(fscanf(f,"%d %s %lf", &num, p->string, &p->number) == 3)
        {
            return 0;
        }
    }
    else
    {
        return 1;
    }
}

int main(int argc, char **argv)
{
    int n = 5, status = 0, i = 0, j;

    struct mypair array[5];
    char file_in_name[255];
    char file_out_name[255];

    FILE *fin;
    FILE *fout;

    zero_string(file_in_name, 255);
    zero_string(file_out_name, 255);

    printf("Data file:\n> ");
    scanf("%s", file_in_name);
    printf("Out file:\n> ");
    scanf("%s", file_out_name);

    fin = fopen(file_in_name, "r");
    fout = fopen(file_out_name, "w");

    if( fin == NULL )
    {
        exit(-1);
    }
    if( fout == NULL )
    {
        exit(-1);
    }

    printf("\n");

    while(status != 1)
    {
        status = row(fin, &array[i]);

        if(i >= n)
            break;
        else
        {
            if(status != -1)
                fprintf(fout, "%s %lf\n", array[i].string, array[i].number);
        }

        i ++;
    }

    fclose(fin);
    fclose(fout);

    for(j=0; j<i; j++)
        printf("%s %lf\n", array[j].string, array[j].number);

    return 0;
}

在底部循环中,您想通过j而不是i来索引数组。

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

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