0

I am trying to loop through a text file that contains random content. It's current contents are:

"13 -35 57 - 23723724 
12taste-34the+56rain-bow845"

My program should only get the numbers from the file (-35 as a negative number, but not - 23723724 due to the space in between) and no letters or other characters unrelated to the integer.

Currently my code has a while loop that runs through the file and fetches all the decimal values. For some unknown reason however, it stops after 57 (total result is: "13-3557" and then it stops).

I have attempted to iterate over every character seperately but that brought along it's own set of problems and this method at least returns whole numbers.

Here is my code:

int *getIntegers(char *filename, int *pn) {
    // Create a dynamic array
    int len = 100;
    int *numbers = malloc(sizeof(int) * len);

    // Source file
    FILE *file;
    file = fopen(filename, "r");

    int i = 0, number = 0;
    while(fscanf(file, "%d", &number) > 0) {
        numbers[i++] = number;
        printf("%d", number);
    }

    return numbers;
}

EDIT: I have changed my code and it now retrieves all the numbers, but no spaces.

// Create a dynamic array
int len = 100;
int *numbers = malloc(sizeof(int) * len);

// Source file
FILE *file;
file = fopen(filename, "r");

int i = 0, number = 0;
while(!feof(file)) {
    if(fscanf(file, "%d ", &number) > 0) {
        numbers[i++] = number;
    } else {
        clearerr(file);
        fgetc(file);
    }
}

fclose(file);
return numbers;

3 Answers3

1

When the input stream encounters - and it expects to see an integer, it does not read anything. It stops there.

If you want to continue reading the rest of the numbers, you'll need some code that reads the next characters, discards it, and continues on.

while(!foeof(file) )
{
    if ( fscanf(file, "%d", &number) > 0) {
       numbers[i++] = number;
       printf("%d", number);
    else {
       clearerr(file); // Clear the error state.
       fgetc(file);    // Read the next character and discard it.
    }
}

Update

To add a space between the numbers in the output, use:

       printf("%d ", number);
R Sahu
  • 204,454
  • 14
  • 159
  • 270
  • Your solution works but the numbers are not separated by a space. I tried simply adding `numbers[i++] = ' '` but that only changed the numbers and didn't add spaces. – Aydin Biber May 18 '15 at 18:32
  • You can print a space between numbers. – R Sahu May 18 '15 at 18:33
  • Amazingly, I thought I wouldn't be able to do this since my main function (which prases the data) didn't include a space after printing the values. But upon checking it again it actually DOES have a space so I won't have the add spaces before returning the data. Thanks and I will mark your answer as accepted ASAP. – Aydin Biber May 18 '15 at 18:35
0

fscanf doesn't keep looking at its input until it finds something matching its patters. In this case, it encounters the lone -, and unable to parse it into an integer, returns zero. This breaks your loop. You will need to use EOF to break your loop instead.

Politank-Z
  • 3,653
  • 3
  • 24
  • 28
  • Ok I have changed it. Now it returns me a lot more numbers, most of which are 12 (a total of 551 characters) which is a lot more than I have in my file. – Aydin Biber May 18 '15 at 18:15
  • You are outputting the contents of `number` for each non-decimal which `fscanf` encounters. Store the return value of `fscanf` and only output `number` if the return value isn't zero. – Politank-Z May 18 '15 at 18:19
0

It's because fscanf sees the lonely '-' and as that's not a valid number it cant parse it and returns 0 which causes your loop to end.

I suggest you use fgets to read the whole line, and then use strtok to separate on space, and strtol to convert the tokenized strings to numbers.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621