0

I am trying to read a tab delimited file containing 4 columns and store each element of the column as an 1-dimensional array. Though the program prints the strings correctly as in the commented printf but it doesn't give me first_col[0] value and so on. My code is as follows:

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


int main()
{
    FILE *int_file1;
    int BUF = 1024;

    char first_col[BUF],sec_col[BUF],third_col[BUF], fourth_col[BUF];

    int index=0,i=0;

    char *array1[BUF];

    int_file1=fopen("test.txt","r");

    if(int_file1 == NULL)
    {
        perror("Error while opening the file.\n");
    }
    else
    {
        while(!feof(int_file1))
        {
            fscanf(int_file1,"%[^\t]\t%[^\t]\t%[^\t]\t     [^\n]\n",first_col,sec_col,third_col,fourth_col);
//            printf("%s\n",first_col);
            strcpy(first_col,array1[index]);
            index++;
        }
     }

    fclose(int_file1);    

    for(i=0;i<index;i++)
    {
        printf("%s",array1[i]);
    }

    return 0;
}

The inputfile test.txt has the following elements:

34932 13854 13854 2012-01-07

172098 49418 53269 2012-01-07

-

Please help!!

user3258515
  • 75
  • 1
  • 6
  • 1
    You might check whether fscanf returns 4; putting \n in fscanf is useless because blank characters in the format do match for 0,1,+ blanks in the file. If you want to check a \n, try %[\n] and count +1 for fscanf result. – Edouard Thiel Feb 11 '14 at 20:42
  • 1
    Reverse your `strcpy` parameters; the first argument is the destination, not the source. – John Bode Feb 11 '14 at 20:42
  • 1
    And note on top of everything else, [`while(!feof(int_file1))`](http://stackoverflow.com/questions/5431941/while-feof-file-is-always-wrong) is almost certainly *wrong*. You never check whether the invoke of `fscanf()` actually *worked*. – WhozCraig Feb 11 '14 at 20:44
  • Reversing strcpy parameters didn't work. Still segmentation fault. – user3258515 Feb 11 '14 at 20:47
  • please suggest how to check or invoke fscanf – user3258515 Feb 11 '14 at 20:50
  • tried adding this snippet but could get only first value of first_col and then a segmentation fault [CODE] while(fscanf(int_file1,"%[^\t]\t%[^\t]\t%[^\t]\t%[^\n]",first_col,sec_col,third_col,fourth_col) == 4) { printf("%s\t",first_col); strcpy(array1[index],first_col); index++; } [\CODE] – user3258515 Feb 11 '14 at 21:02

1 Answers1

0

Two errors:

  1. In strcpy(first_col,array1[index]), you have swapped between the source string and the destination string. So you should change it to strcpy(array1[index],first_col).

  2. You have not initialized any of the entries in array1, so an expression such as array1[index] is essentially junk. Hence, before commencing strcpy(array1[index],first_col):

    Set array1[index] = malloc(strlen(first_col)+1);

Oh, and don't forget to free(array1[i]) after printing it...

barak manos
  • 29,648
  • 10
  • 62
  • 114
  • Or `malloc(strlen(first_col) + 1)` rather, since this seems to be C and not C++. Otherwise, good advice. – M Oehm Feb 11 '14 at 20:46
  • Thanks a lot!! It did work after doing the corrections. However, I am getting a new line character added to the second row variable of first_col. Please suggest how to get rid of this new line character?? – user3258515 Feb 11 '14 at 21:28
  • Hey no problem man, then how about some credit up there? I don't know the structure of your input file, so I can't help you with it, but I'm guessing that you need to add `\n` somewhere after the second `%`... – barak manos Feb 11 '14 at 21:35
  • No idea how to give credits in this forum.. Im very new to d forum sorry.. but yes my tons n tons of thanks!! I wanted to get rid of the \n not bring it.. But hope I can figure it out by little googling.. Thanks once again!! – user3258515 Feb 11 '14 at 22:18
  • Click on the V next to the answer :) – barak manos Feb 11 '14 at 22:19