#include <stdio.h>
#include <stdlib.h>
#define MAX 40
int main(void)
{
FILE *fp;
char words[MAX];
if ((fp = fopen("wordy", "a+")) == NULL)
{
fprintf(stdout,"Can't open \"words\" file.\n");
exit(1);
}
puts("Enter words to add to the file; press the Enter");
puts("key at the beginning of a line to terminate.");
while (gets(words) != NULL && words[0] != '\0')
fprintf(fp, "%s ", words);
puts("File contents:");
rewind(fp); /* go back to beginning of file */
while (fscanf(fp,"%s",words) == 1)
puts(words);
if (fclose(fp) != 0)
fprintf(stderr,"Error closing file\n");
return 0;
}
程序输出
C>addaword
Enter words to add to the file; press the Enter
key at the beginning of a line to terminate.
The fabulous programmer[enter]
[enter]
File contents:
The
fabulous
programmer
为什么下面的输出能把The fabulous programmer分开成单词而且还换行,fscanf()函数不读取空格类的字符,那么是怎么分开的呢?
#include <stdlib.h>
#define MAX 40
int main(void)
{
FILE *fp;
char words[MAX];
if ((fp = fopen("wordy", "a+")) == NULL)
{
fprintf(stdout,"Can't open \"words\" file.\n");
exit(1);
}
puts("Enter words to add to the file; press the Enter");
puts("key at the beginning of a line to terminate.");
while (gets(words) != NULL && words[0] != '\0')
fprintf(fp, "%s ", words);
puts("File contents:");
rewind(fp); /* go back to beginning of file */
while (fscanf(fp,"%s",words) == 1)
puts(words);
if (fclose(fp) != 0)
fprintf(stderr,"Error closing file\n");
return 0;
}
程序输出
C>addaword
Enter words to add to the file; press the Enter
key at the beginning of a line to terminate.
The fabulous programmer[enter]
[enter]
File contents:
The
fabulous
programmer
为什么下面的输出能把The fabulous programmer分开成单词而且还换行,fscanf()函数不读取空格类的字符,那么是怎么分开的呢?
