Hi. Our C professor gave us the following problem:
I wrote a code I think will work, but for some reason Putty and TeraTerm are both hating my computer tonight so I can't try it. I'll have access to the school computer's before it's due, but not for long so I really need to work out any big kinks before I go, because I won't have much time in the computer lab to do trial and error. Does my code make sense and should it likely work or am I using commands incorrectly?Develop a program that reads in the file /home/elliott/hw/hw2_input.txt. Your program should replace the word ”lamb” with ”sheep” and write the output back out to a file hw2_input.out. Your program should use the input file name to construct the output file name. So, if the input file name were test.txt, it would write out test.out.
Code:#include <stdio.h> #include <stdlib.h> #include <string.h> main(){ FILE *fp1, *fp2; char str[80]; char changeword[10] = sheep; char *cp; char OutfileName[32]; fp1 = fopen("/home/elliott/hw/hw2_input.txt", "r"); //create output file and name it strcpy(OutfileName, fp1); cp = memchr(OutfileName, '.', strlen(OutfileName)-1); strcat(Outfilename, ".out", strlen(".out")); fp2 = fopen(OutfileName, "w"); //go through each line and find the lambs while (fgets(str, 80, fp)!=NULL){ if (strstr(str, "lamb")!=NULL) { strcpy(lamb, changeword); } fprintf("%s\n", str); } close(fp1); close(fp2); }



LinkBack URL
About LinkBacks


