Отредактировать текст, удаляя лишние пробелы между словами - C (СИ)
Формулировка задачи:
В текстовом файле хранится некоторый текст. Отредактировать текст, удаляя лишние пробелы между словами.
почему то мое предложение из файла удаляется,и он становится пустым#include<stdio.h>
#include<string.h>
FILE *f;
int main(void)
{
char str[250]="";
char res[250] = "";
int i=0,j=0;
f=fopen("file1.txt","wt");
if(f==NULL)
{
puts("error file");
return 1;
}
while ((str[i]=fgetc(f))!=EOF)
{
if (str[i]==' ')
{
if(j==0) continue;
if (str[i+1]==' ') continue;
}
res[j] = str[i];
j++;
i++;
}
i=strlen(res);
if(res[i-2] == ' ')
res[i-2] = '\0';
fprintf(f,"%s",res);
fclose(f);
return 0;
}Решение задачи: «Отредактировать текст, удаляя лишние пробелы между словами»
textual
Листинг программы
#include <stdio.h>
int main(int argc, char* argv[])
{
int sym, flag = 0;
char *fInName = "in.txt";
char *fOutName = "out.txt";
FILE *fIn, *fOut;
fIn = fopen(fInName, "r");
if (!fIn)
return 1;
fOut = fopen(fOutName, "w");
if (!fOut)
return 2;
while((sym = fgetc(fIn)) != EOF)
{
if (sym == ' ')
{
if (flag == 0)
{
fputc(sym, fOut);
flag = 1;
}
else
continue;
}
else
{
fputc(sym, fOut);
flag = 0;
}
}
fclose(fIn);
fclose(fOut);
remove(fInName);
rename(fOutName, fInName);
return 0;
}