0

I'm very new to programming and C. I have textfile with some random text and an integer that i want to find and save. The textfile looks something like this (I only want to save 23 in this case, not 56):

# this is a comment
23
this is some random text
56

And this is the code I have:

    int *num = malloc(sizeof(int));
    *num = fgetc(f);

    while(!feof(f)){
        *num = fgetc(f);
        if(isdigit(*num)){
            break;
        }
    }

    if(!isdigit(*num)){
        printf("Error: no number found.\n");
        free(num);
    }
    else{
        printf("%c\n", *num);
    }

I'm kinda stuck right now and my program only prints out the number 2 :/ Very thankful for help.

  • 1
    You need to read more than 1 character at a time to get the whole number and then convert the text represenattion into binary value. Look for `strtol` and friends. – Gerhardh Aug 14 '18 at 09:26
  • What text or who suggested using `feof(f)`? – chux - Reinstate Monica Aug 14 '18 at 10:31
  • 1
    side notes: `feof` tells you whether a previous error was due to eof, it doesn't promise that you can read from the file, and you're ignoring the first character you read. – jthill Aug 14 '18 at 10:36

3 Answers3

2

As @pbn said you're better off using sscanf.

But if you really, really want, you can do it your way, by reading one character at a time, but you'll need to "build" the integer yourself, converting the character to integer, keeping track of what you have, and multiplying by powers of 10 for every digit the number that you already have.

Something like this (not complete code, it's just to get you started):

int c;
int num = 0;

while (c = fgetc(f)) {
    if(!isdigit(c)) {
        break;
    }
    num = (num * 10) + (c - '0');
}

The c- '0' part is to convert the text representation of the integer to the integer itself. 0 is character 48, 1 is 49 and so on.

This is assuming that on the line with numbers, you ONLY have numbers, not a mix of numerical and non-numerical characters.

Also, do not use !feof(file).

ChatterOne
  • 3,381
  • 1
  • 18
  • 24
  • @chux You're right about the `char` and `int`. The `pow` was just a bit of an exaggeration in being explicit as to how it would work, but I guess I'll change it anyway to what you just said. – ChatterOne Aug 14 '18 at 10:33
1

One option could be using getline and sscanf functions. I assumed that text lines do not contain numbers:

#include <stdio.h>

int main() {
    int value, matched = 0;
    char *line = NULL;
    size_t size;
    while(getline(&line, &size, stdin) != -1) {
        if ((matched = sscanf(line, "%d", &value)) != 0) 
            break;
    }
    if (matched) 
        printf("value: %d\n", value);
    return 0;
}

This part:

 while(getline(&line, &size, stdin) != -1) { 

will try to read the entire stream line by line. Next line uses sscanf return value, which is the number of input items successfully matched and assigned, to determine whether the integer value has been found. If so it stops reading the stream.

pbn
  • 2,406
  • 2
  • 26
  • 39
0

One simple way in your program is once you find digit don't just stop continue untill you find next " " , "\n" , "\0" . Till then add Number = Number*10 +(*num);, define Number as global or something.