-7

First, I should tell you that I use DEV C++ to write my program.

Ok, to the point now... I wrote a program that gets input from a file named "candidates1.txt". So, the first lines are:

main() {
    FILE *fp;

    fp = fopen("candidates1.txt", "r");
    fscanf(fp, "%d %d", &N, &length);

   // ...

and the rest I believe don't matter. If I run the program like this, I get the desirable output. But, if I changed it to this:

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

    fp = fopen(argv[1], "r");
    fscanf(fp, "%d %d", &N, &length);

    // ...

and try to run it, nothing appears as an output and the command line closes. Could somebody tell me why is this happening?

CristiFati
  • 38,250
  • 9
  • 50
  • 87
Grabenfly
  • 13
  • 5
  • 1
    How you are executing code ?? – Jeyaram Jun 16 '14 at 04:15
  • 1
    Did you pass `candidates1.txt` as parameter to your program? – Rohan Jun 16 '14 at 04:15
  • can you elaborate more? how could you run your program? also make sure you pass proper command line arguments. – Jayesh Bhoi Jun 16 '14 at 04:16
  • Yeah that's why I wrote that i run the program using DEV C++. If main has the argc and argv parameters, the only way to run it is via the command line? Not with Dev C++? – Grabenfly Jun 16 '14 at 04:19
  • 3
    If your development environment cannot pass arguments to the program it runs, then expecting it to receive arguments won't go over very well… –  Jun 16 '14 at 04:21
  • 2
    It seems unlikely that even the most brain-dead IDE could not support command line arguments to programs. Surely Dev C++ is not that bad, is it? – Jonathan Leffler Jun 16 '14 at 04:24
  • 5
    You should check that you got a value in `argv[1]` before you use it. You should check that you successfully opened the file before you use the file pointer. You should check that you get 2 values from `fscanf()` as you expect. And you should code as though C99 or C11 was the standard supported by the compiler, so you should have an explicit return type `int main(void)` or `int main(int argc, char **argv)`. – Jonathan Leffler Jun 16 '14 at 04:25
  • I've not down-voted, because I prefer to give you time to redeem the question. There are a lot of FGITW shooters who prefer to down-vote instantly, which doesn't welcome people. I'm sorry; I can't stop them. You could help yourself by including checks in your code to ensure that what you think is happening actually is happening, and you could also explain how you run the command with an argument from Dev C++. If you can't provide a command line argument from Dev C++, then (a) it is a bit pointless trying to look for one, and (b) maybe you need to get an IDE that can do what you need. – Jonathan Leffler Jun 16 '14 at 04:32
  • First, thanks a lot for your answer to my problem. Second, I'm a newbie as hell "programmer". Those who down-voted could cut me some slack because some things I don't mention them, because I DO NOT KNOW THEM. THAT'S WHY I ASK. @JonathanLeffler – Grabenfly Jun 16 '14 at 04:36

1 Answers1

1

Change your program to this:

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

  if (argc < 2)
  {
     printf ("Please supply an argument\n") ;
     return 1 ;
  }

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

and look what the output is. Probably you don't provide a command line argument to your program.

Jabberwocky
  • 48,281
  • 17
  • 65
  • 115