Thread: please help! adding to strings

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    16

    please help! adding to strings

    I am trying to write to files called something that I give in my command-line. For example I call "a.out blahblah.c16", and then I want to create two new strings "blahblah.o" and "blahblah.syms" and then write to them. I've tried strcat, and doing it character by character, but nothing seems to work.

    This is what I'm working with so far:
    Code:
    	char * name = malloc(sizeof(strlen(argv[1])));
    	strncpy(name, argv[1], s1);
    	printf("name is '%s'\n", name);
            char o[MAXWORDLEN]; //max is 20
    	char syms[MAXWORDLEN];
    
    	puto(o, name);
    	putsyms(syms, name);
    	printf("o is '%s'\n", o);
    	printf("syms is '%s'\n", syms);
    
    	input = fopen(argv[1], "r");
    	output = fopen(syms, "w");
    	output2 = fopen(o, "w");
    here are the methods
    Code:
    int puto(char *str, char *name) {
    	int i = 0;
    	char c = *name;
    	char * name2 = name;
    	while ( ((c>=48 && c <=122) || c == 46) && c!='\0') {
    		str[i] = c;
    		name++;
    		c = *name;
    		i++;
    	}
    	str[i++] = '.';
    	str[i++] = 'o';
    	str[i] = '\0';
    	name = name2;
    	return 0;
    }
    
    int putsyms(char *str, char *name) {
    	int i = 0;
    	char c = *name;
    		while (c != '\0') {
    			str[i] = c;
    			name++;
    			c = *name;
    			i++;
    		}
    		str[i++] = '.';
    		str[i++] = 's';
    		str[i++] = 'y';
    		str[i++] = 'm';
    		str[i++] = 's';
    		str[i] = '\0';
    		return 0;
    }

    >a.out llo.small.int.c16
    name is 'llo.small.int'
    o is ''
    syms is 'llo.small.int.syms'
    FINISHED
    HEX IS 8302
    Segmentation fault (core dumped)

    when I run it in eclipse, it writes to the files fine, but now I'm using a linux server and that doesn't work. Thanks in advance.

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> strlen(argv[1])

    Good start there, except you forgot to account for the null terminator.

    >> sizeof(strlen(argv[1]))

    strlen returns an int, and sizeof(int) isn't what you want.

    >> strncpy(name, argv[1], s1);

    Not sure what 's1' is, but a better approach would be to store the amount that you passed to malloc somewhere and usee that value instead. Also, not applicable here, but just keep in mind that strncpy doesn't guarantee a null terminator. You might create a wrapper function to make sure that gets done...

    Just a suggestion:

    Code:
    
    int puto(char *str, char *name) {
        int i = 0;
        char c;
        while ((c = name[i]) != '\0' && (c>=48 && c <=122)) {
            str[i++] = c;
        }
        str[i++] = '.';
        str[i++] = 'o';
        str[i] = '\0';
        return 0;
    }
    Also, it would be a better practice to pass a maximum length to the functions to prevent possible overflows.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. adding a space between two strings in strcat function
    By Emerald in forum C Programming
    Replies: 3
    Last Post: 08-25-2009, 12:31 PM
  2. Adding Strings to ListBox in a Dialog
    By kevins963 in forum C Programming
    Replies: 5
    Last Post: 09-10-2008, 04:36 PM
  3. adding strings?
    By Led4urhead123 in forum C++ Programming
    Replies: 2
    Last Post: 07-10-2008, 03:13 PM
  4. adding strings hard problem...
    By qubit67 in forum C Programming
    Replies: 28
    Last Post: 04-22-2007, 02:02 AM
  5. SVN Import Causes Crash
    By Tonto in forum Tech Board
    Replies: 6
    Last Post: 11-01-2006, 03:44 PM