Link to home
Start Free TrialLog in
Avatar of Galligan
Galligan

asked on

Basic question on strings - please help!!

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
ASKER CERTIFIED SOLUTION
Avatar of garethwebbley
garethwebbley

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ewest
ewest

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.

Avatar of sunnycoder
No comment has been added lately and this question is therefore classified abandoned.

If asker wishes to close the question, then refer to
https://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