-1

hi I'm trying to write a program like this in visual C++

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

int main(int argc, char *argv[])
{
    FILE *in;
    char ch;
    int openbracket=0,closebracket=0;

    if(argc!=2)
    {
        printf("the number of arguments is incorrect");
        getch();
        exit(1);
    }

    if((in=fopen(arg[1],"r"))==NULL)
    {
        fputs("Error",stderr);  exit(1);
    }

    ch=getc(in);

    while(!feof(in))
    {

        if(ch=='{')
            openbracket++;

        else if(ch=='}')
            closebracket++;

        ch=getc(in);
    }

    printf("Open bracket==%d,close bracket=%d",openbracket,closebracket);

    getch();

}

i am trying to open a file with argv[1] in this program in visual c++ can you please show me how i can put a file in argv[1] 0f main function?

Thank you

R.A
  • 49
  • 4
  • 1
    What problems or difficulties are you encountering? – abelenky Mar 01 '17 at 17:32
  • 1
    Are you using C or C++? This code looks like C. If you are being taught this is C++ then you might want to look for a new place to learn C++. – NathanOliver Mar 01 '17 at 17:32
  • 1
    When you compile this program, you are getting an error message. ***Shame*** on you for not putting the error message in your question. – abelenky Mar 01 '17 at 17:37

1 Answers1

2
if((in=fopen(arg[1],"r"))==NULL);
  1. if-statements do NOT end with a semi-colon. That makes this a "do-nothing" statement

  2. arg is never declared anywhere (you have declared argc and argv, but not arg)

abelenky
  • 63,815
  • 23
  • 109
  • 159
  • my problem is not this I want to pass a text file in argv[1] – R.A Mar 01 '17 at 18:12
  • Your code will not even compile as is. You must fix compile errors before you can work on proper behavior. – abelenky Mar 01 '17 at 18:51
  • Is this the question you really mean to be asking?? http://stackoverflow.com/questions/298708/debugging-with-command-line-parameters-in-visual-studio – abelenky Mar 01 '17 at 19:43