Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Variable Recognition Problem
Index -> Programming, C -> C Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
AnubisProgrammer




PostPosted: Thu Dec 13, 2007 6:40 pm   Post subject: Variable Recognition Problem

For a school project I'm writing a program that basically recreates the Unix fold command. One of the parameters that gets passed is for the width setting. However, the user can either input it like this: -w5
or like this: -w 5
the difference is just in the spacing but it messes things up. Is there any way to say:

if(argv[2]=="-w(any integer)"){
etc, etc.
}


any help would be much appreciated, thank you very much.

ps. I'm using VS 2005
Sponsor
Sponsor
Sponsor
sponsor
Euphoracle




PostPosted: Thu Dec 13, 2007 9:50 pm   Post subject: RE:Variable Recognition Problem

Rather than -w5, why not use -w 5. With a space in between, you can simple use argv[3]. I assume you already split it by spaces. Most applications take this approach.
AnubisProgrammer




PostPosted: Fri Dec 14, 2007 12:26 am   Post subject: Re: Variable Recognition Problem

Unfortunately in the assignment it specifically mentions that the user should be able to put a space between the -w and the 5 if they want. I actually have an if statement already set up to handle if there's a space between them.
OneOffDriveByPoster




PostPosted: Fri Dec 14, 2007 9:41 am   Post subject: Re: Variable Recognition Problem

AnubisProgrammer @ Thu Dec 13, 2007 6:40 pm wrote:
if(argv[2]=="-w(any integer)"){

Check out strtol() and remember that (argv[2] + 2) is a string...
md




PostPosted: Fri Dec 14, 2007 2:40 pm   Post subject: Re: Variable Recognition Problem

OneOffDriveByPoster @ 2007-12-14, 9:41 am wrote:
AnubisProgrammer @ Thu Dec 13, 2007 6:40 pm wrote:
if(argv[2]=="-w(any integer)"){

Check out strtol() and remember that (argv[2] + 2) is a string...


argv[2]+2 is '\0' if argv[2] = "-w". Further more you are not guarunteed that argv will be in a contiguous block of memory.

Your best bet is to check to see if argv[2] is "-w" without an integer. If it is then you know the next argument (argv[3]) is an integer. if argv[2] does contain an integer then you don't need to use argv[3]. If you have multiple arguments then you probably want to write some code that steps through them and stores the arguments in a structure of your own creation, it'll simplify things tremendously
OneOffDriveByPoster




PostPosted: Fri Dec 14, 2007 4:54 pm   Post subject: Re: Variable Recognition Problem

md @ Fri Dec 14, 2007 2:40 pm wrote:
argv[2]+2 is '\0' if argv[2] = "-w".

*(argv[2] + 2) is '\0' if argv[2] is "-w". (argv[2] + 2) qualifies as a string in that case. I would admit that I forgot to tell the OP to check for the "-w" part first, but I thought the problem was telling if the rest of argv[2] was an "integer" (so the check was much too obvious). No one is suggesting that argv will be a contiguous block of memory. I was only talking about the "-w<something>" case (as indicated by the quote in my previous post).
AnubisProgrammer




PostPosted: Sat Dec 15, 2007 7:59 pm   Post subject: Re: Variable Recognition Problem

Alright, at first I wasn't quite sure what you meant by the argv[2]+2, but after playing with it for a bit I think it's working. Thanks a ton, strtol's a really helpful function.
Junkhead




PostPosted: Mon Jan 28, 2008 3:19 pm   Post subject: Re: Variable Recognition Problem

[quote="md @ Fri Dec 14, 2007 2:40 pm"]
OneOffDriveByPoster @ 2007-12-14, 9:41 am wrote:


argv[2]+2 is '\0' if argv[2] = "-w". Further more you are not guarunteed that argv will be in a contiguous block of memory.


I thought argv[] had to be a contiguous block of memory? If it wasn't, since C strings are defined as contiguous bytes of memory ended with a null character, then is argv[] really usable, if it isn't allocated in a contiguous block of memory? You couldn't do any operations on the string at all, if it wasn't. I mean, how could you possibly parse a string in the standard library, if the string contained "garbage" not associated with the string data at all? It's not like you could reliably filter it out. The standard C library even relies upon the fact that strings are defined as a contiguous block of memory, for it's pointer arithmetic, to allow for more speed. It would be totally useless otherwise. And isn't argv an array? Arrays are also defined as contiguous blocks of memory, so... I'm just wondering, where'd you read that at?
Sponsor
Sponsor
Sponsor
sponsor
md




PostPosted: Mon Jan 28, 2008 6:06 pm   Post subject: RE:Variable Recognition Problem

argv is a array of character pointers. It's probable that the each string follows the other in a continuous block of memory; but unless it's in the standard (which I admit to not having read) it's not required.

You could create your array of pointers, and them point them wherever you want for all it matters.

'Course... if you simply copy the original command line and replace all the non-escaped spaces with '\0' it gets you most of the way to setting up argv. From a practical implementation point of view that's almost certianly how I would do it.
Junkhead




PostPosted: Mon Jan 28, 2008 9:58 pm   Post subject: Re: Variable Recognition Problem

Oh! My mistake, I misunderstood what you were trying to say. Yes, that is entirely correct! I thought you were trying to say pointer arithmetic on a string given in argv was unsafe to do. Sorry!
Display posts from previous:   
   Index -> Programming, C -> C Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 10 Posts ]
Jump to:   


Style:  
Search: