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 and last column of each line. Below is the code I worked on. Its printing the whole line. How to edit to print only the second and last column of each line.
#include<stdio.h>
#include<conio.h>
int main ()
{
char filename[] = "file.txt";
FILE *file = fopen(filename, "r");
if(file!= NULL)
{
char line[128];
while ( fgets( line, sizeof line, file)!= NULL)
{
fputs ( line,stdout);
}
fclose(file);
}
else
{
perror(filename);
}
getch();
return 0;
}
Please Help!
Thanks a ton!
sscanf() should do the trick.
It uses a format string just like printf(), then reads the values into variables.