0

I am trying to take all the integers from the file file.txt and place them in a dynamically allocated array. However the file could also contain other characters, and these should not be put in the array.

The file contains the following:

2 -34 56 - 23423424 12example-34en+56ge-tal345

int* getIntegers(char* filename, int* pn)
{
    FILE* fileInput = fopen(filename, "r");
    int* temp = (int*)malloc( 100*sizeof(int));
    int counter = 0;

    int c= fgetc(fileInput);
    while(c != EOF){
    counter ++;
    printf("%d;\t%d\n", counter, c);fflush(stdout);
    temp[counter++] = c;
    }
    *pn = counter;
    return (temp);
}

int main(void)
{
    int n;
    int* a = getIntegers("file.txt", &n);
    if (a != NULL){
    puts("numbers found:");
    for (int i = 0;i < n; i++){
        printf("%d ",a[i]);
    }
    free(a);
    }
    putchar('\n');
    while(getchar()== '\n');
    return (EXIT_SUCCESS);
}

when I run this the following output is returned:

Output:

1;    49
3;    49
5;    49
7;    49
9;    49
11;    49
13;    49
15;    49
17;    49
19;    49
21;    49

While the correct output should have been

found numbers:

12 -34 56 23423424 12 -34 56 345
2
  • why c= fgetc(fileInput); for one time? Commented Jun 28, 2014 at 10:14
  • and you shouldn't increment counter twice inside the loop. Commented Jun 28, 2014 at 17:31

3 Answers 3

2

There are many things wrong with that program.

  • You leak your file object. You should close it with fclose() when done.
  • If there are more than 100 numbers in your input, you will overflow your buffer, trash your stack, and do Bad Things to your program.
  • You increment your counter twice at every loop iteration, so you'll skip every second entry in your output array.
  • You never read another byte from the input, so you're just going to keep processing the same byte over and over in an infinite loop until your buffer overflow causes your program to crash.
  • You never convert the digits that you read from your input file into an integer; instead, you just take the character code. 49 is the ASCII/UTF-8 code for '1', which appears to be the first character in your input.
Sign up to request clarification or add additional context in comments.

Comments

1

try this

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


int* getIntegers(char* filename, int* pn)
{
    int* temp = (int*)malloc( 100*sizeof(int));
    int counter = 0;
    int result;
    int c;

    FILE* fileInput = fopen(filename, "r");
    if ( fileInput == NULL) {
        return temp;  // return if file open fails
    }
    while( 1) {
        result = fscanf (fileInput, "%d", &c);//try to read an int
        if ( result == 1) { // read successful
            temp[counter] = c; //save int to array
            counter++;
            printf("%d;\t%d\n", counter, c);
        }
        else { // read not successful
            fscanf ( fileInput, "%*[^-+0-9]"); //scan for anything not a -, + or digit
        }
        if ( counter > 98) { // dont exceed array 
            break;
        }
        if ( feof( fileInput)) { // check if at end of file
            break;
        }
    }
    fclose ( fileInput); // close the file
    *pn = counter;
    return (temp);
}

int main(void)
{
    int n;
    int i;
    int* a = getIntegers("file.txt", &n);
    if (a != NULL){
    printf("numbers found:");
    for (i = 0;i < n; i++){
        printf("%d ",a[i]);
    }
    free(a);
    }
    putchar('\n');

    return (EXIT_SUCCESS);
}

Comments

1

Many loop holes in your code.You need to first fix it.

int c= fgetc(fileInput);
while(c != EOF)
{
    counter ++;
    printf("%d;\t%d\n", counter, c);fflush(stdout);
    temp[counter++] = c;
}

This code make me crazy. What you gain by reading only one character from file and run while loop? Give chance to read another byte.

Must check the return value fopen function. what if files not present OR error in opening? Your program gone wild. So ensure

 FILE* fileInput = fopen(filename, "r");
 if(fileInput==NULL ){
  printf("Error to open file\n");
  return
  }

Also you incremented counter two times in loop first one is counter ++; and second one is temp[counter++] = c; this is wrong to manage array index.

Also most important thing is every open file must be close.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.