Thread: argv problems

Hybrid View

Previous Post Previous Post   Next Post Next Post
  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
    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]...

  5. #5
    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

  6. #6
    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.

  7. #7
    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?

  8. #8
    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.

  9. #9
    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.

  10. #10
    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!

  11. #11
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by aadil7 View Post
    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!
    Well, if you want me to write it for you... no problem.
    My standard fee is $10,000cdn plus $11 per hour.

  12. #12
    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.

  13. #13
    Registered User
    Join Date
    Mar 2010
    Posts
    85
    Quote Originally Posted by claudiu View Post
    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.
    thanks for your input but just to let you know ive not let nothing till the last minute and this is not no homework!
    i take c seriously and want to learn the language, so i guess i am sorry for posting a genuine issue on a forum, even though a forum is where all different people come together to help solve issues and give help.

  14. #14
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Quote Originally Posted by aadil7 View Post
    thanks for your input but just to let you know ive not let nothing till the last minute and this is not no homework!
    i take c seriously and want to learn the language, so i guess i am sorry for posting a genuine issue on a forum, even though a forum is where all different people come together to help solve issues and give help.
    If you really did take this seriously you would have done all your research before coming to ask for help. You have not posted any kind of code that you WROTE since the beginning of the thread, you DON'T ASK QUESTIONS THE SMART WAY, and you generally just keep wasting our time.

    People here give help all the time, but only to people who take their work seriously and write THEIR OWN CODE.

    I think it's about time this thread was closed.
    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.

  15. #15
    Registered User
    Join Date
    Mar 2010
    Posts
    85
    Quote Originally Posted by claudiu View Post
    If you really did take this seriously you would have done all your research before coming to ask for help. You have not posted any kind of code that you WROTE since the beginning of the thread, you DON'T ASK QUESTIONS THE SMART WAY, and you generally just keep wasting our time.

    People here give help all the time, but only to people who take their work seriously and write THEIR OWN CODE.

    I think it's about time this thread was closed.
    if you do get the thread closed then fair enough! but then if people cant LEARN certain detailed things then i dont really know where the tag your resource for the C and C++ came from on this site.

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