Thread: whats wrong with this ????!!!

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    40

    whats wrong with this ????!!!

    Dear all , I try with this segment to open a file through passing the file name with argv. but the file couldn't be open. please comment :

    Code:
    #include <iostream.h>
    
    ifstream input_code;
    
    int main(int argc, char argv[])
    {    if(argc == 1)
            {cout << "You need to pass in a file name to the program via the command line\n";
             cin.get();
             return 1;}
        else
             input_code.open(argv);
    
    return 0;
    }
    thanks

  2. #2
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    This:
    Code:
     else
        input_code.open(argv);
    Should be:
    Code:
     else
        input_code.open(argv[2]);
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > int main(int argc, char argv[])
    It's
    int main(int argc, char *argv[])

    > if(argc == 1)
    < 2 would be a safer test

    > input_code.open(argv);
    With main fixed, this would be
    input_code.open(argv[1]);

    argv[0] is typically the program name
    argv[1] is the first command line parameter
    argv[argc] is NULL
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    Yeah, [1], not [2]. Sorry, I'm tired and not thinking straight
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  5. #5
    Registered User
    Join Date
    Nov 2006
    Posts
    40
    yes its successed , thank you so so much

  6. #6
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Do not include iostream.h. It will almost certainly include deprectated headers. Include <iostream>.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> Do not include iostream.h. It will almost certainly include deprectated headers. Include <iostream>
    Deprecated headers and features are discouraged but still standard and will still work on standard compliant compilers. <iostream.h> should not be used because it is completely non-standard, and does not work on many modern compilers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 07-15-2004, 03:30 PM
  2. Debugging-Looking in the wrong places
    By JaWiB in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 11-03-2003, 10:50 PM
  3. Confused: What is wrong with void??
    By Machewy in forum C++ Programming
    Replies: 19
    Last Post: 04-15-2003, 12:40 PM
  4. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 12:01 PM
  5. Whats wrong?
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 07-14-2002, 01:04 PM