#include<stdio.h>
#include<stdlib.h>
#define N 256
int main()
{
FILE *fp;
char *filename = "test.txt";
int s,p;
int i = 0;
//ファイルオープン処理
if((fp = fopen(filename, "r"))==NULL) {
fprintf(stderr,"%sのオープンに失敗.\n",filename);
exit(1);
}
//以下テキスト整形処理
printf("1行の文字数を指定してください\n");
scanf("%d", &p);
while((s = fgetc(fp)) != EOF){
if(s == '\n'){ //読み込んだ文字列が改行文字だった場合はiを初期化
i=0;
}
if(s==' ' && i>p-1){ //読み込んだ文字列が空白文字で、かつ1行が30文字を越している場合
printf("\n");
i=0;
}
putchar(s);
i++;
}
printf("\n");
fclose(fp);
return 0;
}
/*--------test.txt---------*/
A temple like that of Olympia was surrounded by statues of
victorious athletes dedicated to the gods .
結果としては、
A temple like that of Olympia was
[space]surrounded by statues of
victorious athletes dedicated
[space]to the gods .
こうなってしまいます。
[space]は半角スペースだとお考え下さい。
改行をしたあとにスペースが入ってしまうのが気になります。
改善の仕方が分からないので、お力をお貸しください。。