Basic question on strings - please help!!

Galligan
Galligan used Ask the Experts™
on
Hi I've a large data file made up as follows
 5.537681    3.993655   -5.651005
 1.835011    6.016797   -5.632307
-2.336837    5.943220   -5.699044
 etc .....

Now I want to read in each line and based on the last number ie -5.651005, filter ( eg if > -5.6 move)  the line to a new file.

I though about writing each line to a string and then trying to obtain -5.651005 within the string, but i wasnt sure how to do this.

Its a big original file, so it makes sence to read the line and filter it at the same time.

all advice gratefully received,
thanks, EG
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
If your file is fixed format you can use

float val1,val2,val3;

fscanf("%f %f %f", &val1, &val2, &val3);

I'm not 100% certain of the other requirement but you can open two new files keep a note of the previous value and write the line to either of the new files depending on your comparison

ie

if ( val1 > last_val1 )
     fprintf( file1, "%f %f %f", val1, val2, val3);
else
     fprintf( file2, "%f %f %f", val1, val2, val3);

hope this helps

Commented:
You haven't stated (a) the underlying operating system and
(b) if the format of each line is well defined.

If you have a system w/ decent shell scripting capabilities, then you might want to forgo the C coding. On the other hand if C is /the/ way for you, then the coding will be greatly simplified if you can be certain that each line consists of the same set of data.

Simple case:
   #include <stdio.h>

   FILE * fpOrig;
   FILE * fpNew;

   double numA;
   double numB;
   double numC;
   int rc;

   fpOrig = fopen("thedatafile", "r");
   fpNew = fopen("thenewfile", "w");

   wroteNew = 0;

   while (3 == (rc = fscanf(fpOrig, "%f %f %f",
                      &numA, &numB, &numC)))
   {
       if (numC > -5.6)
       {
           fprintf(fpNew, "%f %f %f",
                   &numA, &numB, &numC);
           wroteNew++;
       }
   }

   if (0 == wroteNew)
   {
      remove("thenewfile");
   }
   fclose(fpNew);
   fclose(fpOrig);


Alternatively a combination of
    fgets()
    strtod()

can be used to apply better input error handling:

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


     char line[80];
     while (NULL != fgets(line, sizeof(line), fpOrig))
     {
          int i;
          char * ptr;
          char * endptr;
          double dval;

          /*
           *  trim off trailing newline
           */

          /* get last numeric string, scan in reverse */
          ptr = &line[strlen(line) - 1];
          while (!isspace(ptr))
          {
              ptr--;
          }

          dval = strtod(ptr, &endptr);
          if (endptr != ptr && dval > -5.6)
          {
                 fputs(line, fpNew);
          }
     }

Note, this is untested, so I may have missed some closing parens. More importantly, little or no error checking is included.

Top Expert 2006

Commented:
No comment has been added lately and this question is therefore classified abandoned.

If asker wishes to close the question, then refer to
http://www.experts-exchange.com/help/closing.jsp

Otherwise, I will leave a recommendation in the Cleanup topic area that this question is:
PAQed with A grade to garethwebbley

Please leave any comments here within the next seven days. It is assumed that any participant not responding to this request is no longer interested in its final disposition.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Sunny
EE Cleanup Volunteer

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial