I am new to C programming and am still at an early level in java and C#. I am posting regarding a segmentation fault error code I get when I try to run a program that I am developing. I am coding on an Ubuntu Linux computer and am compiling using gcc. Basically what the program is supposed to do is first using "scanf()" it will read in two values from the user into variables, and those variables will then be passed to a method which will use those variables to issue a "system()" function command which will use the arguments accepted from the user to issue an "mplayer" command to the command line. I have tried all that I know to fix the problem (which is basically substituting "%c" for "%s") but to be honest I really have absolutely no idea why I am getting a Segmentation Fault when I run this program(the error occurs after I have read in both values from the user, so I guess I could be passing to the method incorrectly, but really I just don't know). Below is the code for the program I have been talking about, if you can spot what is causing the segmentation fault and explain it to me and if possible how I can correct it I would really appreciate it, I look forward to participating in this forum.
Code:
#include <stdio.h> #include <string.h> #include <syscall.h> #include <unistd.h> #include <stdio.h> #include <sys/types.h> int main() { char *source; char *param; printf("Please Enter a Source URL to be recorded\n"); sscanf("%s",&source); printf("\nPlease Enter a file to hold the datastream from the URL\n"); sscanf("%s",¶m); getparameters(source, param); return 0; } int getparameters(char *url, char *outfile) { char *tempurl = url; char *tempoutfile = outfile; char *mplayer1 = "mplayer -dumpstream "; char *mplayer2 = " -dumpfile "; char *mplayexec; sprintf(mplayexec,"%c%c%c%c",mplayer1,tempurl,mplayer2,tempoutfile); printf("\n"); system(mplayerexec); return 0; }
Comment