program printing ".N=?" at beginning of string

This is a discussion on program printing ".N=?" at beginning of string within the C Programming forums, part of the General Programming Boards category; I've written a program to read a string from the UNIX command line, and repeat or reverse it depending on ...

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    110

    program printing ".N=?" at beginning of string

    I've written a program to read a string from the UNIX command line, and repeat or reverse it depending on the command line options it's given, and it seems to be working correctly apart from for some reason printing ".N=?" (without quotes, and question mark in a bold hexagon) at the beginning of my string.

    here is my code:

    Code:
    #include <stdio.h>
    #include <unistd.h>
    #include <string.h>
    
    int main(int argc, char *argv[]) {	
    
    char option;				
    char string[100];			//To store string being processed
    char rstring[100];			//To store reverse string
    
    int n,i;				//n for number of repeats, i for iteration of loop
    opterr=0;				//Disable standard error messages 
    
    while((option=getopt(argc,argv,"hr:b"))!=-1){
    	printf("string=%s\n",string);	
    	i=1;							//skip parsing of program name	
    	while(i<argc){						//parse through strings 
    		if(!strcmp(argv[i],"-r")){ 			//if r option found...
    			i+=2;					//... skip option and argument
    		}else{
    			if(!strcmp(argv[i],"-b")){		//if b option found...
    				i++;				//...skip option
    			}else{
    				strcat(string,argv[i]);		//otherwise, add current string to string
    				strcat(string," ");		//add space between words of string		
    				i++;				//increment counter, move to next string		
    			}
    		}
    	}	
    
    /*then I have a switch statement to take the command line options and perform the 
    functions on the string, which I know work as I've tested seperately. 
    The printfs above are for debugging.
    This part of the code is just for reading the string from the command line (ignoring any options there may be), but for some reason it puts the ".N=?" at the beginning of the string. This happens even with the first printf before the nested if statements.

    Anyone know whats wrong?
    Last edited by bertazoid; 02-08-2009 at 01:33 PM.

  2. #2
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    Printing the string w/o initializing it would likely print garabage as in
    Code:
    printf("string=%s\n",string);

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    110
    Quote Originally Posted by itCbitC View Post
    Printing the string w/o initializing it would likely print garabage as in
    Code:
    printf("string=%s\n",string);

    I thought this might be the case, but thing is, i need an empty string to begin with, since I'm concatenating each word of the command line to the string.

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    It's kind of awkward to use getopt and then also try to get a parameter from the command line yourself. Why don't you just use getopt to get the string (so myprog -s "blah blah" -r or whatever) and then proceed from there?
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <unistd.h>
    
    int main(int argc, char *argv[]) {
    	int opt;
    	char *string;
    	if (argc<2) return 0;
    	while ((opt=getopt(argc,argv,"s:"))>0) switch(opt) { 
    		case ('?'): puts("error"); return 0;
    		case ('s'): string=malloc(strlen(optarg)+1);
    			strcpy(string,optarg); break;
    		default: break;
    	}
    	puts(string); free(string);	
    	return 0;
    }
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    If you need an empty string[] memset() it to NULL. Doing it this way prevents garbage characters from being printed.

  6. #6
    Registered User
    Join Date
    Dec 2008
    Location
    Black River
    Posts
    128
    Declaring a variable doesn't mean it will be empty, so you have to initialize it. In the case of static strings, you can use:

    Code:
    char string[100] = "";
    or simply set string[0] to '\0'

  7. #7
    Registered User
    Join Date
    Oct 2008
    Posts
    110
    mk27, I'm not familiar with the malloc function you've used, I haven't got that far yet in my course. Also, I'm trying to read in anything following the options, but not the options, option arguments, or program names themselves. and the options could appear in any order, or may be completely absent.

    I might try using the memset function. I'm assuming i just use memset(string);

  8. #8
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by bertazoid View Post
    I thought this might be the case, but thing is, i need an empty string to begin with, since I'm concatenating each word of the command line to the string.
    One thing you can do to have an "empty" string prepped for strcat is:
    Code:
    char string[256]="\0";
    This means string has a strlen of 0, and (string[0]=='\0') will be true. It won't contain any initial garbage for printf either.
    '\0' is the NULL TERMINATOR.
    Last edited by MK27; 02-08-2009 at 02:08 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  9. #9
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by bertazoid View Post
    mk27, I'm not familiar with the malloc function you've used, I haven't got that far yet in my course. Also, I'm trying to read in anything following the options, but not the options, option arguments, or program names themselves. and the options could appear in any order, or may be completely absent.

    I might try using the memset function. I'm assuming i just use memset(string);
    Okay, don't use malloc. Just use "char string[whatever size]" (and omit free()). But do believe what I am trying to say about getopt. It will make everything much simpler UNLESS the prof said specifically you cannot use "-s blah" to input the string. It doesn't matter to getopt what order the options are in either.

    Regarding memset(), read post #8 first.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  10. #10
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    memset() is one of the several ways of initializing a char array to NULL; follow the one posted by MK27 - it's simple and it works. Here's how you would use memset() - as an fyi.
    Code:
    memset(string, '\0', 256);

  11. #11
    Registered User
    Join Date
    Oct 2008
    Posts
    110
    Thanks for the replies, I forgot about the null terminator for strings. This is probably the easiest way for me to do what I want.

    getopt will only parse the options on the command line won't it? i.e. those strings with the format -x. I'm still unsure as to how I could get it to parse the following words as well. I'd like to do this using only getopt and no additional if statements like you say, but I don't think I understand it well enough, and I can probably get it working using what I have and initialising the string.

  12. #12
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    getopt() is all you need. It will parse the command line options as well as any arguments to it. The optarg extern variable helps you along the way. See here

  13. #13
    Registered User
    Join Date
    Oct 2008
    Posts
    110
    why is the following now giving me a blank string (rstring at end)?

    Code:
    char string[]="hello world!";
    char string2[100]="\0";
    int i;
    
    for(i=0;i<=strlen(string);i++){
    	printf("strlen(string)-i= %d-%d = %d\n",strlen(string),i,strlen(string)-i);
    	string2[i]=string[strlen(string)-i];
    	}
    printf("string=%s\n",string);
    printf("rstring=%s\n\n",string2);

  14. #14
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    because rstring[] is really string2[] which is set to NULL as in
    Code:
    char string2[100]="\0";
    ...
    printf("rstring=%s\n\n",string2);

  15. #15
    Registered User
    Join Date
    Oct 2008
    Posts
    110
    Thanks, but even if I declare string2 as char string2[100]; without initialising it, it still prints a blank string.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program Plan
    By Programmer_P in forum C++ Programming
    Replies: 0
    Last Post: 05-11-2009, 01:42 AM
  2. Help with string program
    By Duskan in forum C Programming
    Replies: 8
    Last Post: 04-02-2007, 08:27 AM
  3. Replies: 8
    Last Post: 03-31-2006, 07:15 AM
  4. Replies: 4
    Last Post: 03-03-2006, 01:11 AM
  5. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 03:58 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21