Home > Software engineering >  Compare the number of each line in a file
Compare the number of each line in a file

Time:01-08

I started learning C, the variables, functions, memory and structures were ok but now I'm stuck with very simple exercises about files

Example

I have a file with a name and a grade in each line. Something like

asdfgh 1.5
asdfg h 2
a 0.5

I've learned how to read each line and I can write a program that reads every line and outputs the number of lines, etc but what I want to learn now is how to isolate the numbers to compare them later. For example to get the maximum or the average, it doesn't matter. Something like

The max number is 2

I'm using fgets and sscanf but at this point I'm very confused with all the tutorials I already watched.

Any simple example or tutorial anyone can share?

I'm reading about strtok, it seems to be useful for what I want but I'm not understanding the theory to apply it for what I want and the videos I watched made me even more confused

I can open the file, read the file and read each line until the end of the file but storing the chars that have numbers in it to later compare them is the part that I don't get

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

char name[1000];
float number = 0;
int lines = 0;
char line[1000];

do {
    fgets(line, 1000, stdin);
    lines  
} while (line[0] != '\n');

return 0;

CodePudding user response:

You can use strcspn to find the first numeric digit.

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

int main(void) {
    char s[] = "hello world 123";
    size_t n = strcspn(s, "1234567890");

    printf("%s\n", &s[n]);

    return 0;
}

Prints:

123

If we have a pointer to this substring, we can then use strtod to convert it to a double.

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

int main(void) {
    char s[] = "hello world 123";
    size_t n = strcspn(s, "1234567890");

    printf("%lf\n", strtod(&s[n], NULL));

    return 0;
}

Prints:

123

Of course, you should probably also error check to determine if a numeric digit was found.

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

int main(void) {
    char s[] = "hello world";
    size_t n = strcspn(s, "1234567890");

    if (!s[n]) {
        printf("Numeric digit not found.\n");
        return 1;
    }

    printf("%lf\n", strtod(&s[n], NULL));

    return 0;
}

CodePudding user response:

Numbers may begin with '0'-'9', ' ', '-', '.','I', 'i' (Infinity), 'N', 'n' (NAN), white-spaces and other implementation specific values like ','. Perhaps even ` in the future. *1

Rather than look for just the first numeric character, use strtod() repeatedly and let that function validate the text as a number.

const char *s = line;
char *endptr;  // Location to store end of conversion.
bool found = false;
double d;
for (; *s; s  ) {
  errno = 0;
  d = strtod(s, &endptr);
  if (s == endptr) continue;  // No conversion.
  // If following is not a space nor a null character ...
  if (!isspace((unsigned char) *endptr) && *endptr) continue;
  // You may want to test for range or skip this.
  if (errno == ERANGE) continue;
  found = true;
  break;
}
if (found) {
  printf("Found %g at <%.*s>\n", d, (int)(endptr - s), s);
}

*1 The next version of C may allow new things like a ' digit separator, binary input, etc. By using standard functions to validate, your code is ready for the future - this reduces maintenance and keeps code up to date..

  •  Tags:  
  • c
  • Related