I have a file with data like this
{0 /Data1/ , 0x00, 0, 0xFF},
{1 /data2/ , 0x00, 0, 0xFF},
{2 /data3/ , 0x00, 0, 0xFF},
{3 /data4/ , 0x00, 0, 0xFF}, ...
I want to print only the second column of each line. Below is the code I worked on.
#include<stdio.h>
#include<string.h>
int main ()
{
char filename[] = "file.txt";
FILE *file = fopen(filename, "r");
if(file!= NULL)
{
char line[128];
char * word1;
char word2;
char word3;
int i=0;
clrscr();
while ( fgets( line, sizeof line, file)!= NULL)
{
i=0;
word1 = strtok(line, " ,");
while(word1!= NULL)
{
i++;
if(i==2 ){
printf("%s\n",word1);
}
word1 = strtok(NULL," ,");
}
}
fclose(file);
}
else
{
perror(filename);
}
getch();
return 0;
}
it works fine. Can I save the value Im printing in each line into an array? I tried something like this
if(i==2){
word2 = * (word1);
}
printf("%s\n",word1);
But it give me a null pointer assignment. How to store the values Im printing into an array?