extract data using c programming

March 13, 2011 at 06:19:13
Specs: Windows XP
If i got a list of data in the data file let say 1-10 and I want to extract only even number, how can it be done?

See More: extract data using c programming

Report •

#1
March 13, 2011 at 12:29:41
I don't see the problem:

Open file.
Read first line - do nothing with it.
Read second line - do whatever you want with it; write it to another file, whatever.
and so on.

But that's trivial, so what's the problem?


Report •

#2
March 13, 2011 at 13:29:25
while(fscanf(file_handle, "%d", &variable) == 1) {
   if(variable % 2 == 0) // do something
}

and then hope for the best...



Report •

#3
March 13, 2011 at 18:44:02
That is assuming the file is a flat ascii file If a database or spreadsheet something else is needed.

Report •

Related Solutions

#4
March 13, 2011 at 20:30:48
when questions are so ambiguous, assumptions are all that can be made. :)

Report •

#5
March 13, 2011 at 22:19:10
This is the list copy from the file:-

t v
0.00 1.08
0.20 0.83
0.40 0.60
0.60 0.43
0.80 0.36
1.00 0.43
1.20 0.58
1.40 0.74
1.60 0.89
1.80 1.03
2.00 1.15
2.20 1.24
2.40 1.32
2.60 1.36
2.80 1.38
3.00 1.38
3.20 1.34
3.40 1.28
3.60 1.20
3.80 1.08
4.00 0.96
4.20 0.81
4.40 0.65
4.60 0.49
4.80 0.38
5.00 0.39

I only need to extract at t=0.0, 1.0, 2.0, 3.0, 4.0 and 5.0

try to use command as below:-
FILE *fp1;
fp1=fopen ("motion.dat","r");
fscanf(fp1," %s %s", text1,text2);
printf(" %s %s \n", text1, text2);

for(k=1; k<=26; k=k+5) {
fscanf(fp1,"%f %f",&masa[k], &halaju[k]);
printf("%4.2f %4.2f \n", masa[k], halaju[k]);
} fclose(fp1);
}

but it doesn't works


Report •

#6
March 13, 2011 at 22:20:31
by the way, for your info, I'm still new with c programming

Report •

#7
March 14, 2011 at 00:06:03
Maybe something like below will help; include string.h

char buf[BUFSIZ];
float data1, data2;
... bits of code

if(fp1) {
   while(fgets(buf, sizeof buf, fp1) != NULL) {
      if((sscanf(buf, "%f %f", &data1, &data2)) == 2) {
         // do something with data1 (left number) and data2 (right number)
      } else {
        // sscanf didn't convert and store two floats into two variables
      }
   }
    ... bits of code
}

hope that helps


Report •

Ask Question