-4

I have this code:

#include <stdio.h>

int main ( int argc, char *argv[] )
{    
     FILE *file;
     int x;

     if ( argc != 2 )
           printf( "Use: %s file name", argv[0] );
     else {
            if ((file=fopen( argv[1], "r" ))== 0 ) 
            printf( “Couldn't open the file.\n" );
             else {
                          while (( x = fgetc( file ) ) != EOF)  printf( "%c", x );
                          fclose( file );
              }
      }
      return 0;
}

Now, having this code, how do I execute a file from terminal (that is on the pic as well as my configuration of NetBeans). https://i.stack.imgur.com/6WRh3.png

Tunaki
  • 132,869
  • 46
  • 340
  • 423
Beth
  • 1
  • 2

1 Answers1

4

First, better replace your statement (in your program above)

printf( “Couldn't open the file.\n" );

with

perror(argv[1]);

then, simply compile your program in your terminal, e.g. type there a shell command similar to

gcc -Wall -g mysource.c -o myprog

(Read more about invoking GCC: the -Wall option asks for nearly all warnings and is very useful -so never miss it-; you could even add -Wextra to get even more warnings; the -g is asking for DWARF debugging information in the ELF executable and enables you to use later gdb)

assuming your source code is in a file named mysource.c in the current working directory (use pwd command to query that current directory, ls -al to list it, and cd builtin command to change it)

at last, run your program as

./myprog sometestfile.txt

You might want to use the debugger. Read first about GDB, and try perhaps

gdb ./myprog

(I am guessing you are on Linux or some other POSIX compliant operating system, and using GCC compiler)

Read more about perror(3), command line interface, shells, globbing, glob(7), PATH variable

Later, you'll want to have some bigger program of yours made in several translation units, having some common header file (to be #included in all of them). Then you'll use a builder like GNU make. You could use a good editor like emacs, some version control like git, etc...

(you might realize that NetBeans is not very useful, because you can have even better developing comfort with your own collection of tools; having the freedom to choose your tools is worthwhile!)

PS. Perhaps replace printf( "%c", x ); with the shorter and more efficient putchar(x); ...

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
  • In `gcc -Wall -g mysource.c -o myprog` what is `-Wall`? – Beth Nov 29 '15 at 14:48
  • `-Wall` ("warnings all") indicates a high warning level. (It doesn't include all possible warnings, though.) Please make it a habit to compile with `-Wall`. It will help you to catch many errors, for example a mismatch of `printf` formats and arguments. – M Oehm Nov 29 '15 at 14:51
  • @MOehm I'm writting `gcc -Wall -g main.c -o CppApplication_1` and says `gcc: error: main.c: No such file or directory` – Beth Nov 29 '15 at 14:57
  • Because you are not in the directory containing `main.c`; you should use some `cd` command to go there – Basile Starynkevitch Nov 29 '15 at 14:58
  • @BasileStarynkevitch @MOehm If I had the `CppApplication_1` on `C:\` how I'd put the commands? – Beth Nov 29 '15 at 14:59
  • I don't understand what you are asking. A drive like `C:` is Windows specific. Linux don't have that. – Basile Starynkevitch Nov 29 '15 at 15:00
  • @BasileStarynkevitch @MOehm If I place the `CppApplication_1` on `C:` I get the same error. `No such file or directory` I don't know how to use `pwd` or `cd` to get there. (I'm currently working at Windows, I said I placed it on `C:` to give an example). – Beth Nov 29 '15 at 15:04
  • I never used Windows. In the old 1985 MSDOS days you might have typed `C:` as a single command, but I don't understand that question (you should ask another one, and mention which operating system you are using. I hope it is Linux or some POSIX compliant system ....) – Basile Starynkevitch Nov 29 '15 at 15:05
  • @BasileStarynkevitch If I have `CppApplication_1` on `Desktop` How I write the command to execute properly. – Beth Nov 29 '15 at 15:07
  • I cannot help about Windows, and I strongly recommend installing Linux if you are studying computer science or information technology (use Windows for games; have some dual boot system if you need to play) – Basile Starynkevitch Nov 29 '15 at 15:07
  • If you have a Linux or POSIX system, learn to use the shell (using `cd`, `pwd`, `ls -l` commands....). But that is another question. If you are forced to use Windows, you have my pity (but I cannot help, I'm using Unix since 1987, Linux since 1993, and never coded on Windows) – Basile Starynkevitch Nov 29 '15 at 15:08
  • Maybe you want to `cd $HOME/Desktop`, but I am only guessing (and that works on Linux, not on Windows). I cannot help about Windows, and I recommend avoiding it (except perhaps for games). – Basile Starynkevitch Nov 29 '15 at 15:15