943,634 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 29022
  • C RSS
You are currently viewing page 1 of this multi-page discussion thread
Oct 24th, 2006
0

Copy argv to string in C(newbie question)

Expand Post »
hello. i'm new here and new to C programming so would appreciate any help with a problem i have. how can i copy the contents of a command line argument e.g. argv[1] to a string declared within main() e.g. filename[30]

cheers.....
PS this isn'y my homework:lol:
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
toxic is offline Offline
3 posts
since Oct 2006
Oct 24th, 2006
0

Re: Copy argv to string in C(newbie question)

Try memcpy or strcpy.
Quote originally posted by http://www.acm.uiuc.edu/webmonkeys/book/c_guide/2.14.html ...
2.14.4 memcpy

Declaration:

void *memcpy(void *str1, const void *str2, size_t n);

Copies n characters from str2 to str1. If str1 and str2 overlap the behavior is undefined.

Returns the argument str1.

2.14.13 strcpy

Declaration:

char *strcpy(char *str1, const char *str2);

Copies the string pointed to by str2 to str1. Copies up to and including the null character of str2. If str1 and str2 overlap the behavior is undefined.

Returns the argument str1.
Last edited by andor; Oct 24th, 2006 at 8:33 am.
Reputation Points: 251
Solved Threads: 29
Posting Whiz in Training
andor is offline Offline
274 posts
since Jun 2005
Oct 24th, 2006
0

Re: Copy argv to string in C(newbie question)

i've tried strcpy() but when i compile and run it the prog hangs then crashes. i'm probably doing something basically wrong in the fubction maybe.

  1. int main(int argc, char *argv[]){
  2.  
  3. char filename[30];
  4. strcpy(filename, argv[1]);
  5.  
  6. }
Reputation Points: 10
Solved Threads: 0
Newbie Poster
toxic is offline Offline
3 posts
since Oct 2006
Oct 24th, 2006
0

Re: Copy argv to string in C(newbie question)

Click to Expand / Collapse  Quote originally posted by toxic ...
i've tried strcpy() but when i compile and run it the prog hangs then crashes. i'm probably doing something basically wrong in the fubction maybe.

  1. int main(int argc, char *argv[]){
  2.  
  3. char filename[30];
  4. strcpy(filename, argv[1]);
  5.  
  6. }
Yes probably U R doing something wrong. What R U passing as arg[1]? Probably nothing and thats why it crashes.

  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. int main(int argc, char *argv[]){
  5.  
  6. char filename[30];
  7. if (argc != 2)
  8. {
  9. printf("Dude are you passing something at all");
  10. return 1;
  11. }
  12. strcpy(filename, argv[1]);
  13. printf("filename: %s\n", filename);
  14. return 0;
  15. }
This works for me.
PS: memcpy is safer
Last edited by andor; Oct 24th, 2006 at 8:59 am.
Reputation Points: 251
Solved Threads: 29
Posting Whiz in Training
andor is offline Offline
274 posts
since Jun 2005
Oct 24th, 2006
0

Re: Copy argv to string in C(newbie question)

D'oh! just going to crawl into a hole and die to save the embarassment!!:eek:
cheers for your help....
Reputation Points: 10
Solved Threads: 0
Newbie Poster
toxic is offline Offline
3 posts
since Oct 2006
Oct 24th, 2006
0

Re: Copy argv to string in C(newbie question)

Its not a big deal, everyone mistakes and we learn from it.
Reputation Points: 251
Solved Threads: 29
Posting Whiz in Training
andor is offline Offline
274 posts
since Jun 2005
Oct 31st, 2007
0

Re: Copy argv to string in C(newbie question)

I also want to read filename, but in addition I need to read some interger parameter. I used this, now want to change. How can I handle: ./progname infilename integer ?
C++ Syntax (Toggle Plain Text)
  1. int main(){
  2. ifstream input;
  3. input.open("filename");
  4. if (!input)
  5. {
  6. cout<<"File not open";
  7. exit(1);
  8. }
  9. getline(input, str);
  10. return 0;
  11. }
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Malaisary is offline Offline
5 posts
since Oct 2007
Nov 1st, 2007
0

Re: Copy argv to string in C(newbie question)

> PS: memcpy is safer
No it's not. It will try to copy n characters whether it hits an architecture-dependent read boundary or not. Use strncpy(). It is fully standard and stops in the right place.

To convert a string to an integer #include <cstdlib> and use the atoi() function:
int i = atoi( "42" );

Also, this is the C forum. If you plan to use C++ please post in the C++ forum. While the same people frequent both, the advice you get will vary depending on the language. (C and C++ are significantly different languages.)

Hope this helps.
Featured Poster
Reputation Points: 1140
Solved Threads: 229
Postaholic
Duoas is offline Offline
2,039 posts
since Oct 2007
Nov 1st, 2007
0

Re: Copy argv to string in C(newbie question)

>i'm probably doing something basically wrong in the fubction maybe.
Are you passing any arguments to your program? If not, argv[1] isn't guaranteed to be there, and if it is, it's pretty much sure to be NULL. If you're passing something, make sure that it's less than 30 characters or you'll overflow the buffer. strncpy is the logical choice, but it's really poorly named and doesn't do quite what you're expecting. I'd say use strncat instead:
  1. #include <string.h>
  2.  
  3. int main ( int argc, char *argv[] )
  4. {
  5. char buffer[30];
  6.  
  7. if ( argc > 1 ) {
  8. /* strncat won't work if buffer isn't already a string */
  9. buffer[0] = '\0';
  10. strncat ( buffer, argv[1], 29 );
  11. }
  12.  
  13. return 0;
  14. }
>It will try to copy n characters whether it hits an
>architecture-dependent read boundary or not.
An architecture-dependent read boundary? Can you describe what you mean by this?

>Use strncpy(). It is fully standard and stops in the right place.
memcpy is fully standard. Also, strncpy always copies n characters. If the string isn't shorter than the buffer, strncpy will pad the buffer all the way to the count with null characters. If the string is longer than the buffer, strncpy will fail to null terminate the buffer. Despite the name, strncpy shouldn't be used as a "count restricted strcpy".

>To convert a string to an integer #include <cstdlib> and use the atoi() function
I wouldn't recommend atoi unless you've completely validated the input string first. It's basically impossible to tell if atoi failed without prior validation, and if the string isn't representable as an integer then calling atoi produces undefined behavior. It's too risky, so you should use strtol instead:
  1. char *end;
  2. long temp;
  3. int x;
  4.  
  5. errno = 0;
  6. temp = strtol ( buffer, &end, 0 );
  7.  
  8. if ( end == buffer )
  9. puts ( "No conversion made" );
  10. else if ( *end != '\0' )
  11. puts ( "Partially invalid string" );
  12. else if ( temp < INT_MIN || temp > INT_MAX )
  13. puts ( "Out of range value" );
  14. else
  15. x = (int)temp;
Reputation Points: 44
Solved Threads: 8
Junior Poster in Training
Ptolemy is offline Offline
62 posts
since Oct 2007
Nov 1st, 2007
0

Re: Copy argv to string in C(newbie question)

I never said memcpy() wasn't standard. Only that strncpy() was also.
The difference in choice is this: do you own/did you allocate the memory you are accessing? If not, you may very well be playing with fire to try reading (or writing) past the end of memory you have not allocated.

The strncpy() function always pads the target out to n bytes. However, it stops reading the source when it hits the end of the string, which the C specification guarantees to be there in argv[].

You are correct about watching the end of string, as strncpy() does not guarantee that the target is null-terminated.

I will agree that the use of strncat() is the best choice so far, since it addresses both issues nicely.

The strtol() has the same validation problems as stroi(). Just because it returns something different... But you are right in that it is a little more convenient for catching incomplete conversions...

Cheers.
Featured Poster
Reputation Points: 1140
Solved Threads: 229
Postaholic
Duoas is offline Offline
2,039 posts
since Oct 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC