0

I have an input file 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. 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. fprinft saves something like this:

 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. 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;
}
Brian Brown
  • 3,873
  • 16
  • 48
  • 79
  • 3
    Please read [Why is “while ( !feof (file) )” always wrong?](http://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong). You don't use the exact same constructs but you have the same problem. – Some programmer dude Mar 06 '16 at 16:19
  • 1
    Your `row` function doesn't always return a value. When the `fscanf()` fails, you don't return anything, so you get random gibberish returned. Your code does not protect itself from a file with more lines than it has storage for. This leads to problems. Always make sure you do not access memory out of bounds of an array. – Jonathan Leffler Mar 06 '16 at 16:22

1 Answers1

3

In your bottom loop, you want to index your array by j, not i.

DrC
  • 7,528
  • 1
  • 22
  • 37