#include <stdio.h>#include <stdlib.h>int main() { FILE *fin, *fout; char inFileName[] = "fcopy.in"; char outFileName[] = "fcopy.out"; char c, prev = '\0'; int count = 0; fin = fopen(inFileName, "r"); if (fin == NULL) { printf("Cannot open %s\n", inFileName); exit(EXIT_FAILURE); } fout = fopen(outFileName, "w"); if (fout == NULL) { printf("Cannot open %s\n", outFileName); exit(EXIT_FAILURE); } while ((c = fgetc(fin)) != EOF) { if (c == ' ' || c == '\t') { if (prev != ' ' && prev != '\t') { if (count == 0) { fputc(' ', fout); } count++; } } else { if (count > 0) { fputc(' ', fout); count = 0; } fputc(c, fout); } prev = c; } if (count > 0) { fputc(' ', fout); } fclose(fin); fclose(fout); return 0;}此段代码有格式错误,请修改
时间: 2024-02-14 19:14:35 浏览: 49
#include <stdio.h>
#include <stdlib.h>
int main() {
FILE *fin, *fout;
char inFileName[] = "fcopy.in";
char outFileName[] = "fcopy.out";
char c, prev = '\0';
int count = 0;
fin = fopen(inFileName, "r");
if (fin == NULL) {
printf("Cannot open %s\n", inFileName);
exit(EXIT_FAILURE);
}
fout = fopen(outFileName, "w");
if (fout == NULL) {
printf("Cannot open %s\n", outFileName);
exit(EXIT_FAILURE);
}
while ((c = fgetc(fin)) != EOF) {
if (c == ' ' || c == '\t') {
if (prev != ' ' && prev != '\t') {
if (count == 0) {
fputc(' ', fout);
}
count++;
}
} else {
if (count > 0) {
fputc(' ', fout);
count = 0;
}
fputc(c, fout);
}
prev = c;
}
if (count > 0) {
fputc(' ', fout);
}
fclose(fin);
fclose(fout);
return 0;
}
阅读全文