Home>

I have implemented a program that uses the function in the title, but the result is different from what I expected, so I would appreciate it if you could help me.

I want to achieve
Function 1. Get the value line by line from the text file described as follows and store it in the array.

Contents of the text file
test.txt
7
10a
8

Function 2. The value acquired by function 1 is only a number. Store only numbers in an array.

Be in trouble
I coded as follows to satisfy the function, but the result is different from the assumption.

Assumption
The output was assumed as follows.

box [0] = 7
Use only Number: 10a
box [1] = 8

However, in reality
Use only Number: 7
Use only Number: 10a
Use only Number: 8
Use only Number:
Is output.

Probably because \ 0 is not stored in * end.
I can't think of how to fix it as expected.

We apologize for the inconvenience, and thanks for your cooperation.

#include<stdio.h>
int main (void)
{
FILE * fp;
char tmp [10];
long int box [10];
char * end;
int count = 0;

fp = fopen ("test.txt", "r");
while (fgets (tmp, sizeof (tmp), fp)! = NULL) {
        strtol (tmp,&end, 10);
    if ('\ 0'== * end) {
        box [count] = strtol (tmp,&end, 10);
        printf ("box [count] =% d \ n", box [count]);
        count = count + 1;
    }
    else {
        printf ("Use only Number:% s", tmp);
    }

}
    fclose (fp);

return 0;
}
c
  • Answer # 1

    Perhaps there is a \ n at the end of the string

  • Answer # 2

    Use isspace to make sure there are no non-whitespace characters after end.
    Whitespace is'',' \ t','\ n','\ v','\ f','\ r'.

    #include<stdio.h>// fopen, fclose, fgets, printf
    #include<stdlib.h>// strtol
    #include<ctype.h>// isspace
    int main (void)
    {
        char tmp [24], * end;
        long box [10];
        int count = 0;
        FILE * fp = fopen ("test.txt", "r");
        if (fp == NULL) return 1;
        while (fgets (tmp, sizeof (tmp), fp)! = NULL) {
            long num = strtol (tmp,&end, 10);
            if (end == tmp) {
                while (isspace ((unsigned char) * end)) end ++;
                if (* end! ='\ 0')
                    printf ("Use only Number:% s", tmp);
                else else
                    puts ("Empty line");
            }
            else {
                while (isspace ((unsigned char) * end)) end ++;
                if (* end! ='\ 0')
                    printf ("Use only Number:% s", tmp);
                else {
                    printf ("box [count] =% ld \ n", num);
                    box [count] = num;
                    if (++ count == 10) break;
                }
            }
        }
        fclose (fp);
        return 0;
    }


    It's useless to run strtol twice, so I used the variable num.
    In printf, "% d" is an int. long is "% ld".
    If long is 4 bytes, the maximum is 2147483647 and the minimum is -2147483648.
    A minimum of 12 bytes is required, including the'\ 0'at the end of the string.
    char tmp [10];is a bit of a problem.
    Some processing systems have a long length of 8 bytes, which requires 21 bytes.

    Postscript
    Added detection of blank lines.