ファイルを取り、.txtすべてのスペースをハイフンに置き換える単純な C プログラムを作成しました。ただし、プログラムは無限ループに入り、結果はハイフンの無限配列になります。  
これは入力ファイルです:
a b c d e f
これは、プロセスがクラッシュした後のファイルです。
a----------------------------------------------------------------------------
----------------------------------------... (continues thousands of times)... 
fread()、fwrite()、fseek()、またはこれらの関数の私の誤解の予期しない動作の理由を推測します。これは私のコードです:
#include <stdlib.h>
#include <stdio.h>
#define MAXBUF 1024
int main(void) {
    char buf[MAXBUF];
    FILE *fp;
    char c;
    char hyph = '-';
    printf("Enter file name:\n");
    fgets(buf, MAXBUF, stdin);
    sscanf(buf, "%s\n", buf);   /* trick to replace '\n' with '\0' */
    if ((fp = fopen(buf, "r+")) == NULL) {
        perror("Error");
        return EXIT_FAILURE;
    }
    fread(&c, 1, 1, fp);
    while (c != EOF) {
        if (c == ' ') {
            fseek(fp, -1, SEEK_CUR); /* rewind file position indicator to the position of the ' ' */
            fwrite(&hyph, 1, 1, fp); /* write '-' instead */
        }
        fread(&c, 1, 1, fp); /* read next character */
    }
    fclose(fp);
    return EXIT_SUCCESS;
}
ここで何が問題なのですか?