I am trying to remove a file from the current working directory. When I try to make it print "Invalid file" when file is not found, it throws me a segmentation fault. This snippet is part of a bigger program.
Code:
printf("\nEnter your current password:\t"); scanf("%s",temp2); comp2 = strcmp(password,temp2); file = (char*)malloc(100 * sizeof(char)); if(comp2 == 0) { int value; printf("\nEnter the file name:\t"); scanf("%s",file); sprintf(cmd,"rm -i %s",file); value=system(cmd); if(value == -1) printf("\nFile not Found. Exiting Program"); } else printf("\nThe password is incorrect. Please try again."); break; Any help please? Thanks in advance.
cmdallocated? Also,rm -iis an interactive command but you're not an interactive process. Also, C has anunlinkcall so you don't have to usesystem...rmin a subprocess isn't a good way to delete files from a C program. It's open to all sorts of potential problems, such as an unusual$PATHcausingrmnot to be found, or spaces in the filename causingrmto misunderstand what file you're trying to delete. Use a system call, such asunlink, to make your own program delete the file instead of running some other program to delete it.cmdbig enough to accomodatefileand the command? Even if it is, follow Wyzards advice. Finally, i suggest you signal successful completion somehow too...cmd?