The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
.
google unix.com



High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
comparing strings agarwal Shell Programming and Scripting 3 04-16-2008 06:29 AM
Search for strings & copy to new file amitrajvarma Shell Programming and Scripting 2 11-25-2007 11:51 PM
Sun's 'Project Copy Linux' not a Linux copy - Register iBot UNIX and Linux RSS News 0 07-29-2007 06:31 AM
How to concatenate two strings or several strings into one string in B-shell? fontana Shell Programming and Scripting 2 08-26-2005 12:58 PM
ksh - strings itzcoolbuddy Shell Programming and Scripting 1 06-22-2005 11:26 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 05-15-2002
area area is offline
Registered User
  
 

Join Date: May 2002
Posts: 2
How can you copy strings?

Running into a bit of difficulty making duplicates of strings that enter the program via the command line. Evidently char p[] = q[]; doesn't work, and I can't do this:
[i]
for( i=0 ; q[i] != EOF ; i++ )
p[i] = q;

as the string I'm trying to duplicate comes from the command line, from argv[1], so I can't wedge the i in anywhere. Any ideas how I can get around this?
  #2 (permalink)  
Old 05-15-2002
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon
  
 

Join Date: Aug 2001
Location: Ashburn, Virginia
Posts: 9,131
strcpy(p,q); would be the way I would go. But just to make a point...
Code:
#include <stdio.h>
main(argc,argv)
int argc;
char *argv[];
{
      int i,k;
      for(i=0; argv[i]; i++) {
           printf("i= %d \n", i);
           for(k=0; argv[i][k]; k++)
                printf("   char = %c\n", argv[i][k]);
      }
      exit(0);
}

Now, I wouldn't write code like that in a real program. It is, however, legal c.
  #3 (permalink)  
Old 05-15-2002
area area is offline
Registered User
  
 

Join Date: May 2002
Posts: 2
Nice, thanks... I fiddled with that and I seem to have a copy of the string. However I'm trying to append ".bat" onto the end of said string by means of strcat(), but it's giving me evil warnings and stackdumps when I try to compile / run it.

It allows me to strcat(argv[1], ".bat") but not, for some odd reason, strcat(*dup, ".bat"), where dup[] is a duplicate of argv[1].
  #4 (permalink)  
Old 05-15-2002
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon
  
 

Join Date: Aug 2001
Location: Ashburn, Virginia
Posts: 9,131
Overwriting the arguments like you show in your first use of strcat is illegal too, even if the compiler doesn't catch and your cpu succeeds in running the resulting code.

Your second example of strcat is so bizarre that I'm not sure what to think. My best guess is that dup is an array of characters. If so, try strcat(dup, ".bat");
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 07:26 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0