1

I am getting weird output while running code written below on Xcode. The program simply reads integers from a file given like command line argument, and outputs array of those integers.

That is the result that I get:

"0 Program ended with exit code: 0"

#include <stdio.h>
#include <stdlib.h>
int main(int argc, const char * argv[]) {

FILE* file;
file = fopen(argv[1], "r");
if (!file) {
    printf("error occured!! make sure right file is provided");
}
else{
    int i = 0;
//        int value;
    int array[atoi(argv[2])];
    while (!feof(file)) {
        fscanf(file, "%d", &array[i]);
        printf("%d ", array[i]);
        i++;
    }
}

return 0;
}

However, if I run same code from terminal it outputs expected result:

run from terminal

I guess the problem might be the arguments that I provided in Xcode, but I can't figure out what precisely.

arguments, Xcode

Update: My input.txt file contains following: "1 2 3 4 5"

Update: SOLVED Seems there is a bug in Xcode after last update. When Files names are added into the arguments section..it does not create them in debug folder. I manually created file "input.txt" in the following folder:/Users/name/Library/Developer/Xcode/DerivedData/percentile-ecpffkfxacqgpggopnguhvlicvtt/Build/Products/Debug (it could be accessed by selecting 'Show In Finder' from executable name in the 'Products' group in the Xcode Project Navigator) and it worked.

fiz
  • 389
  • 5
  • 26
  • 1
    All of the `argv` entries that you use might not be valid, depending on `argc`. `fscanf` returns a value that tells you whether the scan was successful; use it. The file might contain more numbers than your array size, which will lead to a buffer overflow. – M Oehm Nov 03 '14 at 19:39
  • Incorrect usage of `printf("%d ", array[i]);`. The return value from `fscanf()` was ignored and upon EOF, which could be the first time through the loop, `array[i]` has an undefined value. – chux - Reinstate Monica Nov 03 '14 at 23:59
  • No, that is not a bug. An IDE should not be creating a file just because you mention it in the arguments setting for a test run. – Chris Stratton Nov 07 '14 at 03:50
  • I agree that IDE should not create file, but it should be able to access file if it is mentioned in arguments list. As far as I know, that is the was all the other IDE's behave. In addition to that, Xcode was like that before the update as well. – fiz Nov 07 '14 at 12:35

1 Answers1

1

Take a look to “while( !feof( file ) )” is always wrong

Consider using the result of fscanf to exit from loop:

int temp;
while (fscanf(file, "%d", &temp) == 1) {
    array[i] = temp;
    printf("%d ", array[i]);
    i++;
}
Community
  • 1
  • 1
David Ranieri
  • 39,972
  • 7
  • 52
  • 94
  • if it was always wrong, it would not output anything. However, in this case it does outputs "0". In addition to that, I don't think that there is a bug in code, there must be something wrong with my way of passing arguments. Because if I run it from terminal, it gives expected output – fiz Nov 03 '14 at 19:49
  • 1
    Have you read the link? your loop condition is "while we haven't tried to read past end of file", so you are writting out of the bounds of the array on the last iteration (undefined behavior). – David Ranieri Nov 03 '14 at 20:07
  • I know that it goes out of bounds, but even in that case value at that address must be printed, is not it? And as I mentioned above program does output result when it is run from terminal. Just I could not get why Xcode behaves like this. Just solved issue, will update question in a minute – fiz Nov 03 '14 at 20:22