Pointer Manipulation with strcat()

This is a discussion on Pointer Manipulation with strcat() within the C Programming forums, part of the General Programming Boards category; Hey, I have aquired a massive amount of .wma's from my father, and I have a command to convert the ...

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    8

    Pointer Manipulation with strcat()

    Hey, I have aquired a massive amount of .wma's from my father, and I have a command to convert the .wma's to .ogg's. I am gonna try and code a program that would do all of this for me. The program compiles with a warning of

    Code:
    radiohead@w00den-pickle:~/programming/C$ gcc wma_convert.c
    wma_convert.c: In function ‘main’:
    wma_convert.c:8: warning: initialization from incompatible pointer type
    It isn't that big of a deal...I think, but if it can be fixed, please tell me how.

    But that isn't why I am posting.

    The pointer to wma_file (ogg_file) is supposedly incremented until it reaches the end of the string or file name, then decremented until it reaches a '.', then add "ogg" in place of that. but what happens is that it deletes everything before the period and adds "ogg" to the end of the untouched filetype resulting in something like this.

    This includes compilation errors/warnings and what happens when program is run.

    Code:
    radiohead@w00den-pickle:~/programming/C$ gcc -o wma_convert wma_convert.c
    wma_convert.c: In function ‘main’:
    wma_convert.c:8: warning: initialization from incompatible pointer type
    radiohead@w00den-pickle:~/programming/C$ ./wma_convert
    What file (WMA) do you want to convert?
    :test.test
    Original filename:       test.test
    New filename:    .testogg
    radiohead@w00den-pickle:~/programming/C$
    Here is the source as of now. The command to convert is commented out as not to cause any confusion on the compiler's part.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main(int argc, char *argv)
    {
    	char wma_file[1024];
    	char *ogg_file = &wma_file;
    	char line[1024];
    	
    	printf("What file (WMA) do you want to convert?\n:");
    	fgets(line, sizeof(line), stdin);
    	sscanf(line, "%s", &wma_file);
    
    	printf("Original filename:\t %s\n", wma_file);
    	
    	while(*ogg_file != '\0')
    		*ogg_file = *ogg_file++;
    
    	while(*ogg_file != '.')
    		*ogg_file = *ogg_file--;
    
    	strcat(ogg_file, "ogg");
    
    	printf("New filename:\t %s\n", ogg_file);
    	
    //	system("gst-launch-0.8 filesrc location=\"file.wma\" ! decodebin ! vorbisenc ! filesink location=\"file.ogg\"");
    
    	return (0);
    }
    I hope you can help.

  2. #2
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Quote Originally Posted by radiohead
    Hey, I have aquired a massive amount of .wma's from my father, and I have a command to convert the .wma's to .ogg's. I am gonna try and code a program that would do all of this for me. The program compiles with a warning of

    Code:
    radiohead@w00den-pickle:~/programming/C$ gcc wma_convert.c
    wma_convert.c: In function ‘main’:
    wma_convert.c:8: warning: initialization from incompatible pointer type
    It isn't that big of a deal...I think, but if it can be fixed, please tell me how.
    All warnings can be fixed, and you should usually understand them.

    Code:
    	char *ogg_file = &wma_file;  /* Lose the "&", an array in value context is the address of the first element */
    	sscanf(line, "%s", &wma_file); /* ditto */
    
    		*ogg_file = *ogg_file++; /* Undefined behaviour */
    
    		*ogg_file = *ogg_file--; /* ditto */

  3. #3
    Registered User
    Join Date
    Sep 2005
    Posts
    8
    Ok, thanks alot. All the errors did go away, but I still have the same problem with the pointer manipulation. It is still deleting the filename and just adding "ogg" to the back of the filetype.

    Here is the code that has no errors:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main(int argc, char *argv)
    {
    	char wma_file[1024];
    	char *ogg_file = wma_file;
    	char line[1024];
    	
    	printf("What file (WMA) do you want to convert?\n:");
    	fgets(line, sizeof(line), stdin);
    	sscanf(line, "%s", wma_file);
    
    	printf("Original filename:\t %s\n", wma_file);
    	
    	while(*ogg_file != '\0')
    		ogg_file = ogg_file++;
    	while(*ogg_file != '.')
    		ogg_file = ogg_file--;
    
    	strcat(ogg_file, "ogg");
    
    	printf("New filename:\t %s\n", ogg_file);
    	
    //	system("gst-launch-0.8 filesrc location=\"file.wma\" ! decodebin ! vorbisenc ! filesink location=\"file.ogg\"");
    
    	return (0);
    }

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,852
    > ogg_file = ogg_file++;
    This is undefined behaviour
    ogg_file++;
    Is all you need

    > ogg_file = ogg_file--;
    This too is undefined behaviour, and is broken if the string you type in doesn't contain a '.'

    Seriously, why not do
    char *p = strrchr ( wma_file, '.' );
    if ( p != NULL ) strcat ( p, ".ogg" );

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 04-04-2009, 04:45 AM
  2. scope of a pointer?
    By Syneris in forum C++ Programming
    Replies: 6
    Last Post: 12-29-2005, 09:40 PM
  3. Pointer manipulation
    By shaun84 in forum C Programming
    Replies: 3
    Last Post: 10-23-2005, 04:27 PM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 01:04 PM

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