0

Can you help me? I have a string 23;56;36.6;run in a txt file.Then I am reading this string in order to use it for some work: I would like to take this values from a string, then compare them with some values in a code and output my result in console. I think,I should use atoi() function that make my string in numbers, for picking out , I am using strtok(). But how correctly should I record my tokens in loop while and the last token is a type of char. How can I do this work?

CODE:

void printInfo(int note)
{
    int i;
    FILE *out;
    char str[250];
    char sp[10]=";";
    char *istr;


    if ((out =fopen("test.txt","r"))==NULL)
        printf("Error open, file\n");
    else
    {
        for (i=0;i<note;i++)
        {
            fgets(str,250,out);
            istr=strtok(str,sp);
            while (istr != NULL)
            {
                printf("%d\n",atoi(istr));
                istr=strtok(NULL,sp);
                // I think, I need to create a variable for recording my values.
            }
        }
    }
   fclose(out);
}
Nikita Gusev
  • 143
  • 10

2 Answers2

1

I would use sscanf to convert the string to the three floats:

#include <stdio.h>  // sscanf
#include <stdlib.h> // EXIT_SUCCESS
#include <string.h> // memset

int main(void) {
    const char *input = "23;56;36.6;run";
    int i;
    float numbers[3]  = {0, 0, 0};
    char buf[10];
    int nElementsRead;

    // init buf
    memset(buf, 0, sizeof(buf));

    // sscanf returns the number of read elements
    // or EOF on error
    nElementsRead = sscanf(input, "%f;%f;%f;%9s", &numbers[0], &numbers[1], &numbers[2], buf);

    if (nElementsRead == 4) {
        printf("Successfully read %d elements\n", nElementsRead);

        for (i = 0; i < 3; ++i) {
            printf("number[%d]: %f\n", i, numbers[i]);
        }

        printf("Buffer is: %s\n", buf);
    } else {
        printf("Something went wrong!");

        return EXIT_FAILURE;
    }

    return EXIT_SUCCESS;
}
Marco
  • 7,007
  • 2
  • 19
  • 49
0

Another solution, using comparable records:

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

typedef struct MyRecord_s {
   int   f1;
   int   f2;
   float f3;
   char  f4[10];

} MyRecord;

static int MyRecordInit( MyRecord * out, char * line ) {
   if( out == NULL ) {
      return 0;
   }
   char * f1 = strtok( line, ";" );
   char * f2 = strtok( NULL, ";" );
   char * f3 = strtok( NULL, ";" );
   char * f4 = strtok( NULL, ";" );
   if( f1 && f2 && f3 && f4 ) {
      char * err = NULL;
      out->f1 = strtol( f1, &err, 10 );
      if( err && *err ) {
         return 0;
      }
      out->f2 = strtol( f1, &err, 10 );
      if( err && *err ) {
         return 0;
      }
      out->f3 = strtof( f1, &err );
      if( err && *err ) {
         return 0;
      }
      strncpy( out->f4, f4, 10 );
      out->f4[9] = '\0';
      return 1;
   }
   return 0;
}

int MyRecordCmp( const MyRecord * r1, const MyRecord * r2 ) {
   int diff = r1->f1 - r1->f2;
   if( diff ) {
      return diff;
   }
   diff = r1->f2 - r2->f2;
   if( diff ) {
      return diff;
   }
   float d = r1->f3 - r2->f3;
   if( d > 0.000001 ) {
      return +1;
   }
   if( d < -0.000001 ) {
      return -1;
   }
   return strcmp( r1->f4, r2->f4 );
}

int main() {
   char line1[] = "23;56;36.6;run";
   char line2[] = "24;57;37.6;stop";
   MyRecord r1, r2;
   if( MyRecordInit( &r1, line1 ) && MyRecordInit( &r2, line2 )) {
      printf( "cmp: %d\n", MyRecordCmp( &r1, &r2 ));
   }
   return 0;
}
Aubin
  • 14,617
  • 9
  • 61
  • 84