一个输出序号和单词的程序,#include #include #define MAX 40int main(void){FILE *fp;char words[MAX];int wordct = 0;if ((fp = fopen("wordy","a+")) == NULL){fprintf(stderr,"Can't open \"words\" file.\n");exit(1);}/* determine current number of

来源:学生作业帮助网 编辑:六六作业网 时间:2024/11/23 12:07:57
一个输出序号和单词的程序,#include#include#defineMAX40intmain(void){FILE*fp;charwords[MAX];intwordct=0;if((fp=fop

一个输出序号和单词的程序,#include #include #define MAX 40int main(void){FILE *fp;char words[MAX];int wordct = 0;if ((fp = fopen("wordy","a+")) == NULL){fprintf(stderr,"Can't open \"words\" file.\n");exit(1);}/* determine current number of
一个输出序号和单词的程序,
#include
#include
#define MAX 40
int main(void)
{
FILE *fp;
char words[MAX];
int wordct = 0;
if ((fp = fopen("wordy","a+")) == NULL)
{
fprintf(stderr,"Can't open \"words\" file.\n");
exit(1);
}
/* determine current number of entries */
rewind(fp);
while (fgets(words,MAX - 1,fp) = NULL)
wordct++;
rewind(fp);
puts("Enter words to add to the file.Enter one word per line,and ");
puts("press the Enter key at the beginning of a line to terminate.");
while (gets(words) = NULL && words[0] = '\0')
fprintf(fp,"%d:%s\n",++wordct,words);
puts("File contents:");
rewind(fp); /* go back to beginning of file */
while (fgets(words,MAX - 1,fp) = NULL)
fputs(words,stdout);
if (fclose(fp) = 0)
fprintf(stderr,"Error closing file\n");
return 0;}尤其是解释一下rewind();谢谢!

一个输出序号和单词的程序,#include #include #define MAX 40int main(void){FILE *fp;char words[MAX];int wordct = 0;if ((fp = fopen("wordy","a+")) == NULL){fprintf(stderr,"Can't open \"words\" file.\n");exit(1);}/* determine current number of
rewind()函数原型为void rewind(FILE *stream);
其功能是将文件内部的位置指针重新指向一个流(数据流/文件)的开头.这里要注意,不是文件指针而是文件内部的位置指针,随着对文件的读写文件的位置指针(指向当前读写字节)向后移动.而文件指针是指向整个文件,如果不重新赋值文件指针不会改变.
rewind函数作用等同于 (void)fseek(stream,0L,SEEK_SET);