Friday, July 31, 2009

C Program Help?

Write a C program to copy a file to another file following the criteria that the line containing more than 20 characters will be copied to the destination file.

C Program Help?
If this is homework it goes without saying it is not going to help you on a test unless you learn this stuff for yourself. This provided in the spirit that you just needed a hand in getting started on overcoming a mental block.





#include %26lt;stdio.h%26gt;


#include %26lt;string.h%26gt;





int main(int argc, char *argv[])


{


FILE *in, *out;





const int BUFFER_SIZE = 200;





char buffer[BUFFER_SIZE];





if ((in = fopen("input_file.txt", "r")) == NULL)


{


perror("Input file");


return(0);


}








if ((out = fopen("output_file.txt", "w+")) == NULL)


{


perror("Output file");


return(0);


}








while (fgets(buffer, BUFFER_SIZE, in) != NULL)


{


if (strlen(buffer) %26gt;= 20)


{


fputs(buffer, out);


}


}





fclose(in);


fclose(out);


return(1);


}


No comments:

Post a Comment