Thread: argv problems

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    85

    argv problems

    hi all

    i was just wondering if there is a way to point to a specific part of the argv structure

    this is my barrier at the moment as i am reading in multiple files and i cannot write them to anything as i do not know how to point to the last part in argv

    help is much appreciated
    kind regards

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Are you trying to get the contents of argv? Then argv[1]....argv[n] should be strings containing the parameters passed to the program. argv[0] is always the program name.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  3. #3
    Registered User
    Join Date
    Mar 2010
    Posts
    85
    my only issue is that there are multiple inputs so i dont know where the last input will be as that is the main one as it is the new file's name

    in other words i would like to select the last input where there are multiple inputs and nobody knows how many inputs there will be

    regards
    Last edited by aadil7; 12-04-2010 at 02:10 PM. Reason: better description

  4. #4
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    What are you talking about? I don't understand what these inputs are? Are you talking about program PARAMETERS?
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by aadil7 View Post
    my only issue is that there are multiple inputs so i dont know where the last input will be as that is the main one as it is the new file's name

    in other words i would like to select the last input where there are multiple inputs and nobody knows how many inputs there will be

    regards
    argc tells you how many argv arguments there are... so you want argv[argc-1]...

  6. #6
    Registered User
    Join Date
    Mar 2010
    Posts
    85
    Quote Originally Posted by CommonTater View Post
    argc tells you how many argv arguments there are... so you want argv[argc-1]...
    hi thanks i think this would work because i am using argc but i had just not tough of that yet...
    what would i do though if i need to use the last input as argc - 1 would take into consideration all the other inputs.

    how would i also put it together as the user will specify input files which will be read and written to an output file named and created by the user.
    i know what i am meant to be using and how to go about it, its just that i dont understand how i will use the last inputted name from argv and make that into a file.

    kind regards

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by aadil7 View Post
    hi thanks i think this would work because i am using argc but i had just not tough of that yet...
    what would i do though if i need to use the last input as argc - 1 would take into consideration all the other inputs.

    how would i also put it together as the user will specify input files which will be read and written to an output file named and created by the user.
    i know what i am meant to be using and how to go about it, its just that i dont understand how i will use the last inputted name from argv and make that into a file.

    kind regards
    You asked how to get the last argument... I told you how.

    If you need to make use of all arguments, you're going to have to cycle through them in a loop.

  8. #8
    Registered User
    Join Date
    Mar 2010
    Posts
    85
    thanks i appreciate your help but there must be a quicker more efficient way then cycling through in a loop!
    any ideas?

  9. #9
    Registered User
    Join Date
    Aug 2010
    Posts
    231
    It's easy:
    Code:
    #include <stdio.h>
    int main(int argc,char**argv)
    {
      while( argc-- )
        puts(*argv++);
      return 0;
    }

  10. #10
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Tell you what... write some code, get it working as far as you can... if you get stuck I'm sure several of us will jump in to help and offer advice. It's a local policy that we don't just hand people code... you need to show an effort first.

  11. #11
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by BillyTKid View Post
    It's easy:
    Code:
    #include <stdio.h>
    int main(int argc,char**argv)
    {
      while( argc-- )
        puts(*argv++);
      return 0;
    }
    Shouldn't that be
    Code:
    puts (argv[argc]);
    argc only tells you how many elements are in the argv array, it doesn't tell you how many characters there are. Moroever, if you're planning to use argv again, you don't want to destroy argc or the pointer values for argv.

  12. #12
    Registered User
    Join Date
    Mar 2010
    Posts
    85
    i am fully behind hard work and writing your own code as getting someone else to do it is pointless
    i am not faulting what you are saying but ive been thinking about this all day and have tried experimenting with code and have tried many different things.
    i have always said this will be my last of all resorts and now it has come down to that!

  13. #13
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Well this will teach you an important lesson then. DON'T LEAVE THINGS UNTIL THE LAST MINUTE. You could have asked these questions the day before or the day before that but you didn't.

    Nobody here is going to do your homework for you. It's unethical, unprofessional and a waste of your time as well as ours. It simply doesn't do anything other than lie to yourself.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  14. #14
    Registered User
    Join Date
    Mar 2010
    Posts
    85
    could someone please explain to me what the code is doing

    puts (argv[argc])
    seems like to me when argv is mentioned it is just reffering back to argc

  15. #15
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Have you tried google-ing puts?
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Visual C++ 2010 express problems
    By dnj23 in forum Windows Programming
    Replies: 6
    Last Post: 08-10-2010, 06:16 AM
  2. Most Common problems in C
    By Bayint Naung in forum C Programming
    Replies: 18
    Last Post: 06-02-2010, 08:20 PM
  3. argc, argv
    By Martas in forum C Programming
    Replies: 6
    Last Post: 11-19-2009, 09:39 AM
  4. fread problems or memory problems
    By Lechuza in forum C Programming
    Replies: 1
    Last Post: 03-22-2009, 12:45 PM
  5. more argv and argc
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 09-08-2001, 11:04 PM