1

I am currently trying to read in an input file of 15,000 integers and pass these values into an array. I'm really rusty when it comes to passing command line arguments into the program, so perhaps I am not doing this the correct way. Here is what I have coded thus far:

#include <stdio.h>

int main(int argc, char *argv[]) {
int i;
FILE *fp;
int c;
int values[15000];
char line[32];
int index = 0;

for (i = 1; i < argc; i++) {
    fp = fopen(argv[i], "r");

    if (fp == NULL) {
        printf(stderr, "cat: can't open %s\n", argv[i]);
        continue;
    }

    while (fgets(line, sizeof(line), fp) != NULL) {
        scanf(line, "%d", values[index];
        index++;
    }

    fclose(fp);
}

return 0;
}

I am invoking gcc -o prob_5 input.txt from the command line and am receiving this error message:

/usr/bin/ld:input.txt: file format not recognized; treating as linker script
/usr/bin/ld:input.txt: syntax error
collect2: ld returned 1 exit status

Is there an error with my code or the command line arguments, or both?

2
  • possible duplicate of Command Line Arguments and File Input Commented Sep 15, 2011 at 23:03
  • @sehe Yes, I wasn't sure what the protocol was for asking questions on a continual exercise. I thought that I should edit the post whenever I hit a new problem, but someone suggested that I submit a new question for each stopping point in able to get answers quicker. Commented Sep 15, 2011 at 23:13

3 Answers 3

3

Try

gcc -o prob5 prob5.c
./prob5 input.txt

Assuming that the source file (shown...) is named prob5.c - you don't mention that :)

#include <stdio.h>

int main(int argc, char *argv[])
{
    int i;
    FILE *fp;
    int values[15000];
    char line[32];
    int index = 0;

    for (i = 1; i < argc; i++)
    {
        fp = fopen(argv[i], "r");

        if (fp == NULL)
        {
            fprintf(stderr, "cat: can't open %s\n", argv[i]);
            continue;
        }

        while (fgets(line, sizeof(line), fp) != NULL && (index < 15000))
        {
            sscanf(line, "%d", &values[index]);
            index++;
        }

        fclose(fp);
    }

    return 0;
}
Sign up to request clarification or add additional context in comments.

11 Comments

Whoops! My source file is actually named prob_5.c. And thanks! I just caught the missing parenthesis as well.
regarding the scanf fix, wel... I fixed the fix too. :)
Am I reading the values into the array appropriately? I have a printf("%d", values[0]); statement to test that there is a value at the first index but the program does not return anything.
Yes, I caught the index < 15000 error check and added it to my code. I can't see where else it may be acc.... doh! &value[index] seems to have fixed it.
Cheers. Next time please mention when the challenge was to find/fix the errors :) make it a lot clearer to us what you're doing and what you're supposed to learn. (For your conscience: did you understand all of the fixes? Hint: you need to really read the manpage for sscanf/scanf on %d and sscanf at least; Also did you the spot the one thing that was harmless but also not useful? I deleted it :)) Good luck
|
1

You need to compile the source code

gcc prob_5.c -o prob_5

and then run the binary with command-line parameters

./prob_5 input.txt

What happens with what you're doing is the compiler trying to interpret a bunch of numbers as source code.

Comments

1

The problem is your execution. gcc is a compiler/linker and you shouldn't pass in your input files:

gcc -o prob_5 prob_5.c
./prob_5 input.txt

1 Comment

Thanks! I should have mentioned that I have tried this as well and got a bunch of errors. Just noticed my missing parenthesis on line 20.

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.