Question

When I run the following command in the command terminal: gcc practice.c temp.txt

I get the following error:

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

Here is my C code:

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

#define MAX_LEN 1024

int main(int argc, char **argv) {
  FILE *file;
  char line[MAX_LEN];
  float value = 0;

  file = fopen(argv[1], "r");

  while (fgets(line, MAX_LEN, file) != NULL) {
    sscanf(line, "%f", &value);
    printf("%f\n", value);
  }

  fclose(file);

  return 0;
}

Basically I am trying to read numbers in a file and just print them out. Very simple.

For example, temp.txt will just be something like:

10 26 27 52 242

(these numbers should be in a column)

and so forth.

Was it helpful?

Solution 2

use gcc to compile the executable, and then run the executable on the input file afterwards. You get an error b/c gcc is trying to compile your test.txt as C source code.

So:

gcc practice.c -o practice
./practice test.txt

OTHER TIPS

You may need some explanation about what gcc really is, gcc is used to translate your code into a runnable program, it's a sort of translator for code to executable instruction for your computer.

You do not need to compile the text file, you first need to compile your program :

gcc practise.c -o your_binary_name

then launch it with your file in parameter :

./your_binary_name temp.txt

C is a compiled not an interpreted language. GCC does not run the code as say Python or other scripting languages can for example. GCC rather translates the C source code to native machine code that when linked to the target runtime to create an executable is then separately and directly loaded and executed by the operating system without support from GCC at all.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top