User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C section within the Software Development category of DaniWeb, a massive community of 422,830 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,338 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C advertiser: Programming Forums
Views: 5633 | Replies: 19
Reply
Join Date: Oct 2006
Posts: 3
Reputation: toxic is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
toxic toxic is offline Offline
Newbie Poster

Copy argv to string in C(newbie question)

  #1  
Oct 24th, 2006
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:
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jun 2005
Location: Novi Sad, Serbia
Posts: 273
Reputation: andor has a spectacular aura about andor has a spectacular aura about andor has a spectacular aura about 
Rep Power: 6
Solved Threads: 29
andor's Avatar
andor andor is offline Offline
Posting Whiz in Training

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

  #2  
Oct 24th, 2006
Try memcpy or strcpy.
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 7:33 am.
If you want to win, you must not loose (Alan Ford)
Reply With Quote  
Join Date: Oct 2006
Posts: 3
Reputation: toxic is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
toxic toxic is offline Offline
Newbie Poster

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

  #3  
Oct 24th, 2006
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.

int main(int argc, char *argv[]){
    
    char filename[30];
    strcpy(filename, argv[1]);
    
}
Reply With Quote  
Join Date: Jun 2005
Location: Novi Sad, Serbia
Posts: 273
Reputation: andor has a spectacular aura about andor has a spectacular aura about andor has a spectacular aura about 
Rep Power: 6
Solved Threads: 29
andor's Avatar
andor andor is offline Offline
Posting Whiz in Training

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

  #4  
Oct 24th, 2006
Originally Posted by toxic View Post
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.

int main(int argc, char *argv[]){
    
    char filename[30];
    strcpy(filename, argv[1]);
    
}

Yes probably U R doing something wrong. What R U passing as arg[1]? Probably nothing and thats why it crashes.

#include <stdio.h>
#include <string.h>

int main(int argc, char *argv[]){
    
   char filename[30];
   if (argc != 2)
   {
      printf("Dude are you passing something at all");
      return 1;   
   }
   strcpy(filename, argv[1]);
   printf("filename: %s\n", filename);
   return 0;
}
This works for me.
PS: memcpy is safer
Last edited by andor : Oct 24th, 2006 at 7:59 am.
If you want to win, you must not loose (Alan Ford)
Reply With Quote  
Join Date: Oct 2006
Posts: 3
Reputation: toxic is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
toxic toxic is offline Offline
Newbie Poster

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

  #5  
Oct 24th, 2006
D'oh! just going to crawl into a hole and die to save the embarassment!!:eek:
cheers for your help....
Reply With Quote  
Join Date: Jun 2005
Location: Novi Sad, Serbia
Posts: 273
Reputation: andor has a spectacular aura about andor has a spectacular aura about andor has a spectacular aura about 
Rep Power: 6
Solved Threads: 29
andor's Avatar
andor andor is offline Offline
Posting Whiz in Training

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

  #6  
Oct 24th, 2006
Its not a big deal, everyone mistakes and we learn from it.
If you want to win, you must not loose (Alan Ford)
Reply With Quote  
Join Date: Oct 2007
Posts: 5
Reputation: Malaisary is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
Malaisary Malaisary is offline Offline
Newbie Poster

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

  #7  
Oct 31st, 2007
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 ?
  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. }
Reply With Quote  
Join Date: Oct 2007
Location: Cherry Hill, NJ
Posts: 1,876
Reputation: Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold 
Rep Power: 11
Solved Threads: 193
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

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

  #8  
Nov 1st, 2007
> 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.
Reply With Quote  
Join Date: Oct 2007
Posts: 62
Reputation: Ptolemy is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 8
Ptolemy's Avatar
Ptolemy Ptolemy is offline Offline
Junior Poster in Training

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

  #9  
Nov 1st, 2007
>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;
Reply With Quote  
Join Date: Aug 2005
Posts: 4,782
Reputation: iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light 
Rep Power: 17
Solved Threads: 319
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Industrious Poster

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

  #10  
Nov 1st, 2007
I think stroi is a typo.
I'm not a programmer. My attitude starts with ignorance, holds steady at conversation, and ends with a trip to the hospital. Get used to it.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C Forum

All times are GMT -4. The time now is 10:42 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC