whats wrong with this ????!!!

This is a discussion on whats wrong with this ????!!! within the C++ Programming forums, part of the General Programming Boards category; Dear all , I try with this segment to open a file through passing the file name with argv. but ...

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,344
    > 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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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
    Portugal
    Posts
    7,383
    Do not include iostream.h. It will almost certainly include deprectated headers. Include <iostream>.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    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,317
    >> 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, 09: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, 11:01 AM
  5. Whats wrong?
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 07-14-2002, 01:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21