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!!