一个C语言程序输出序号和单词,#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 numbe

来源:学生作业帮助网 编辑:作业帮 时间:2024/11/06 05:39:52
一个C语言程序输出序号和单词,#include #include #define MAX 40int main(void){FILE *fp;char words[MAX];int wordct = 0;if ((fp = fopen(
xTmKQ+g'wrWS -;ÝLDC TJ.Kf/:^텢`{9ys=Y=Up%.mũZq._yag4V҂yΥMcǥ_. ar~smc굾^pLnb01;a'!qu 5q6 ;>cz:zڄ1Sb K_!T32]Ff26 Lsy ӗGjQCnJ"&H:БoLjtZ5Q5SN.&"2sI)iȘKm$zcl@wH"].m8̜c"X->0Qc q1Mڠ |qB1H\J]:$ҳd "%BҩH4=[v$ojKTL&U榊yȐ:n4`[ Ee9C lu|h4:ɍxѕO8zCADh#aQI^~lEOKYy332q >lsO-R22{[&6/,8>~j_:Ec iV ^D+eZY=)ʾJ3eN}Ld"/ݸǧY_noUA>ŧ|~-0~y*BZU611D8P{oVA3N0gV_ᳯPz%QE)V},|NNΦߢ=b4W

一个C语言程序输出序号和单词,#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 numbe
一个C语言程序输出序号和单词,
#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();谢谢!

一个C语言程序输出序号和单词,#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 numbe
#include
#include
#define MAX 40
int main(void)
{
FILE *fp;
char words[MAX];
int wordct = 0;
if ((fp = fopen("wordy","a+")) == NULL) //打开文件,是指针fp指向文件wordy
{
fprintf(stderr,"Can't open \"words\" file.\n");
exit(1);
}
/* determine current number of entries */
rewind(fp); //rewind()是一个反绕函数,作用是使指针回到文件的开头,在对文件读写的过程中,指针是一直变化的,但是这个函数会把指针重新置在文件的开头
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); //追加新单词到文件中,并标注序号wordct
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;}