Trying to replace % with %% in a string

This is a discussion on Trying to replace % with %% in a string within the C Programming forums, part of the General Programming Boards category; Howdy, I have a program that reads filenames from a file and then displays them on the screen. The problem ...

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    5

    Question Trying to replace % with %% in a string

    Howdy,

    I have a program that reads filenames from a file and then displays them on the screen. The problem is that some of the file names contain the percent sign and that throws off printf.

    I know that %% will display the percent sign, but how would I go through a string and replace % with %% everytime it appears in the filename in C?.


    What is the easiest way to do this?

    Is there a way to get around searching for the %, then reallocating the character array, then inserting the first piece of the code, add % , then add the second piece...blah blah blah (then the sound of me jumping off the bridge since my deadline ain't met.)

    Thanks

    Will

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    The buffer must be large enough to hold the extra '%' character(s). use a pointer to step through the string. When a '%' is found, call memmove() to shift everything right one byte then insert another '%'. Don't forget to increment the pointer.

  3. #3
    Registered User
    Join Date
    Apr 2005
    Posts
    5

    Unhappy Anyway to show that to a new guy?

    I'm kind of new at C. Could you run me through how to do that? Use memmove to create an extra byte on the array?

    Thanks,
    Will
    Last edited by willz99ta; 02-21-2006 at 03:52 PM.

  4. #4
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    This is incorrect:
    Code:
    printf(filename);
    You should never pass user input (or input from a file) to the first argument of printf. This is precisely because of the problem of possible embedded % characters. In general, you should only use a string literal.

    Any of the following are correct:
    Code:
    printf("%s", filename);
    fputs(filename, stdout);
    puts(filename); /* Adds a new-line to end of the output. */

  5. #5
    Registered User
    Join Date
    Apr 2005
    Posts
    5

    Thumbs up BAM, your help completely solved my problems

    Thanks, your printf help completely solved my problem and it has been fixed on time!

    Thanks again,
    Will

  6. #6
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    I have a program that reads filenames from a file and then displays them on the screen. The problem is that some of the file names contain the percent sign and that throws off printf.

    well, it reads for me

    Code:
    #include<stdio.h>
    
    int main()
    {
        char str[30];
        FILE *fp;
        
        if((fp = fopen("test%test.txt","r"))==NULL)
        {
            printf("Error: File cannot be opened\n");
            getchar();
            return 1;
        }
        fgets(str,30,fp);
        printf("%s",str);
        getchar();
        return 0;
    }
    /* my output
    hello%hello
    
    test file
    hello%hello
    */
    ssharish2005

  7. #7
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    Quote Originally Posted by willz99ta
    I'm kind of new at C. Could you run me through how to do that? Use memmove to create an extra byte on the array?

    Thanks,
    Will
    memmove does not create anything, it just moves text from one pace to another, which opens up one or more bytes so that you can change them to something else.
    Code:
    int main(int argc, char* argv[])
    {
    	char str[255] = "c:\\Hello%World%Another%World";
    	char * ptr;
    	for(ptr = str; *ptr; ptr++)
    	{
    		if(*ptr == '%')
    		{
    			memmove(ptr+1,ptr,strlen(ptr));
    			*ptr++ = '%';
    		}
    	}
    	printf("%s\n", str);
    	system("pause");
    	
    	return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 04:30 PM
  2. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  3. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  4. Errors with program to replace string with another
    By Dan17 in forum C Programming
    Replies: 3
    Last Post: 09-14-2006, 10:15 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