c - File write unexpected output -
i have made program copies text file file. result was, original file contents "this test" new file contents "this test" worked, commented part out , tried make output this. result wanted, original file contents "this test" new file contents,
"t h s s t e s t"
for reason output diagonal , messed up, sure because whitespace character messing up, tried check space, , not move next line when encounters whitespace.
#include <stdio.h> int main(void){ file *fp = fopen("originalfile.txt", "r"); char buffer[81]; fgets(buffer, 81, fp); fclose(fp); //copies file /* file *fp2 = fopen("newfile.txt", "w"); fputs(buffer, fp2); fclose(fp2); */ //copies moves next line after each character file *fp2 = fopen("newfile.txt", "w"); int x; char z = '\n'; int = z; printf("%d", z); (x = 0; x < strlen(buffer); x++){ fputc(buffer[x], fp2); int res = fgetc(fp2); if(isspace(res) != 0){ fseek(fp2,81,seek_cur); } else { printf("yeh got here"); } } }
rohan's solution works. have tested in machine, try this:
#include <stdio.h> #include<string.h> int main(void){ file *fp = fopen("originalfile.txt", "r"); char buffer[81]; fgets(buffer, 81, fp); fclose(fp); file *fp2 = fopen("newfile.txt", "w"); int x; (x = 0; x < strlen(buffer); x++){ fputc(buffer[x], fp2); fputc('\n', fp2); } return 0; }
Comments
Post a Comment