69,371
社区成员
发帖
与我相关
我的任务
分享
/* use struct repeatedly to write and read from a file */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define LEN 41
#define PART 21
#define MAX 256
struct words {
char word[LEN];
char partofspeech[PART];
char meaning[MAX];
char example[MAX];
};
struct words *refreshstruct(void); //通过再次调用刷新struct的内容以供下一次循环使用
void getwordinfo(FILE *);
void showwordinfo(FILE *);
int main(void)
{
struct words * pwords;
FILE * fp;
if((fp = fopen("words.dat", "a+b")) == NULL)
{
fprintf(stderr, "Error opening %s", "wordbank.dat");
exit(EXIT_FAILURE);
}
getwordinfo(fp);
showwordinfo(fp);
fclose(fp);
system("PAUSE");
return 0;
}
struct words *refreshstruct(void)
{
struct words wordinfo; //由于wordinfo是auto类型的,所以每次调用函数都会重新产生
//从而达到刷新struct内容的目的,不知这样行不行
struct words * info = &wordinfo;
return info; //将stuct指针传递给主调函数
}
void getwordinfo(FILE *fp)
{
struct words * info;
puts("Enter the new word to be recorded.");
puts("To quit, press Enter at the beginning of a line.");
info = refreshstruct(); //初次使用struct
while(gets(info->word) != NULL && info->word[0] != '\0')
{
puts("Enter the part of speech: ");
gets(info->partofspeech);
puts("Enter the meaning:");
gets(info->meaning);
puts("Enter example(s): ");
gets(info->example);
fwrite(info, sizeof (struct words), 1, fp);
info = refreshstruct(); //刷新已使用过的struct以备下次使用
puts("Enter the new word to be recorded.");
puts("To quit, press Enter at the beginning of a line.");
}
}
void showwordinfo(FILE *fp)
{
struct words * info;
info = refreshstruct();
rewind(fp);
while(fread(info, sizeof (struct words), 1, fp) == 1)
{
printf("\n%s %s\nMeaning: %s\nExample(s): %s\n",
info->word, info->partofspeech, info->meaning, info->example);
info = refreshstruct();
}
}
void refreshstruct(FILE *fp)
{
struct words wordinfo;
struct words *pw = &wordinfo;
getwordinfo(pw, fp);
showwordinfo(pw, fp);
}
改为
void refreshstruct(FILE *fp)
{
struct words wordinfo;
getwordinfo(&wordinfo, fp);
showwordinfo(&wordinfo, fp);
}
/* use struct repeatedly to write and read from a file */
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#define LEN 41
#define PART 21
#define MAX 256
struct words {
char word[LEN];
char partofspeech[PART];
char meaning[MAX];
char example[MAX];
};
void refreshstruct(FILE *);
void getwordinfo(struct words *, FILE *);
void showwordinfo(struct words *, FILE *);
int main(void)
{
FILE * fp;
char choice;
if((fp = fopen("words.dat", "a+b")) == NULL)
{
fprintf(stderr, "Error opening %s", "wordbank.dat");
exit(EXIT_FAILURE);
}
while(1)
{
refreshstruct(fp);
puts("\nContinue? (Y/N)");
choice = getchar();
choice = toupper(choice);
if(choice == 'N')
break;
else if(choice == 'Y')
{
while(getchar() != '\n')
continue;
continue;
}
else
{
puts("Wrong input! Input again:");
puts("\nContinue? (Y/N)");
choice = getchar();
choice = toupper(choice);
while(choice != 'Y' && choice != 'N')
{
puts("Wrong input! Input again:");
puts("\nContinue? (Y/N)");
choice = getchar();
choice = toupper(choice);
while(getchar() != '\n')
continue;
}
}
}
fclose(fp);
system("PAUSE");
return 0;
}
void refreshstruct(FILE *fp)
{
struct words wordinfo;
struct words *pw = &wordinfo;
getwordinfo(pw, fp);
showwordinfo(pw, fp);
}
void getwordinfo(struct words *info, FILE *fp)
{
puts("Enter the new word to be recorded.");
puts("To quit, press Enter at the beginning of a line.");
while(gets(info->word) != NULL && info->word[0] != '\0')
{
puts("Enter the part of speech: ");
gets(info->partofspeech);
puts("Enter the meaning:");
gets(info->meaning);
puts("Enter example(s): ");
gets(info->example);
fwrite(info, sizeof (struct words), 1, fp);
puts("Enter the new word to be recorded.");
puts("To quit, press Enter at the beginning of a line.");
}
}
void showwordinfo(struct words *info, FILE *fp)
{
rewind(fp);
while(fread(info, sizeof (struct words), 1, fp) == 1)
printf("\n%s %s\nMeaning: %s\nExample(s): %s\n",
info->word, info->partofspeech, info->meaning, info->example);
}
/* use struct repeatedly to write and read from a file */
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#define LEN 41
#define PART 21
#define MAX 256
struct words {
char word[LEN];
char partofspeech[PART];
char meaning[MAX];
char example[MAX];
};
void refreshstruct(FILE *); //这里去掉了struct words *返回类型
void getwordinfo(struct words *, FILE *);
void showwordinfo(struct words *, FILE *);
int main(void)
{
FILE * fp;
if((fp = fopen("words.dat", "a+b")) == NULL)
{
fprintf(stderr, "Error opening %s", "wordbank.dat");
exit(EXIT_FAILURE);
}
while(1) //反复调用refreshstruct以便其中的struct word wordinfo变量不断地重生
{
refreshstruct(fp);
break; //当refreshstruct停止被调用则退出循环
}
fclose(fp);
system("PAUSE");
return 0;
}
void refreshstruct(FILE *fp)
{
struct words wordinfo;
struct words *pw = &wordinfo;
getwordinfo(pw, fp);
showwordinfo(pw, fp);
}
void getwordinfo(struct words *info, FILE *fp)
{
puts("Enter the new word to be recorded.");
puts("To quit, press Enter at the beginning of a line.");
while(gets(info->word) != NULL && info->word[0] != '\0')
{
puts("Enter the part of speech: ");
gets(info->partofspeech);
puts("Enter the meaning:");
gets(info->meaning);
puts("Enter example(s): ");
gets(info->example);
fwrite(info, sizeof (struct words), 1, fp);
puts("Enter the new word to be recorded.");
puts("To quit, press Enter at the beginning of a line.");
}
}
void showwordinfo(struct words *info, FILE *fp)
{
char choice;
rewind(fp);
while(fread(info, sizeof (struct words), 1, fp) == 1)
printf("\n%s %s\nMeaning: %s\nExample(s): %s\n",
info->word, info->partofspeech, info->meaning, info->example);
puts("\nContinue? (Y/N)");
choice = getchar();
choice = toupper(choice);
while(choice != 'N') //这个循环用以退出main()中的循环
{
choice = getchar();
choice = toupper(choice);
}
}
/* use struct repeatedly to write and read from a file */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define LEN 41
#define PART 21
#define MAX 256
struct words {
char word[LEN];
char partofspeech[PART];
char meaning[MAX];
char example[MAX];
};
struct words *refreshstruct(void); //通过再次调用刷新struct的内容以供下一次循环使用
void getwordinfo(FILE *);
void showwordinfo(FILE *);
int main(void)
{
struct words * pwords;
FILE * fp;
if((fp = fopen("words.dat", "a+b")) == NULL)
{
fprintf(stderr, "Error opening %s", "wordbank.dat");
exit(EXIT_FAILURE);
}
getwordinfo(fp);
showwordinfo(fp);
fclose(fp);
system("PAUSE");
return 0;
}
void getwordinfo(FILE *fp)
{
puts("Enter the new word to be recorded.");
puts("To quit, press Enter at the beginning of a line.");
char word[LEN] = {};
while(gets(word) != NULL && word[0] != '\0')
{
struct words info = {};
strcpy(info.word, word);
puts("Enter the part of speech: ");
gets(info.partofspeech);
puts("Enter the meaning:");
gets(info.meaning);
puts("Enter example(s): ");
gets(info.example);
fwrite(&info, sizeof (struct words), 1, fp);
puts("Enter the new word to be recorded.");
puts("To quit, press Enter at the beginning of a line.");
}
}
void showwordinfo(FILE *fp)
{
struct words info;
rewind(fp);
while(fread(&info, sizeof (struct words), 1, fp) == 1)
{
printf("\n%s %s\nMeaning: %s\nExample(s): %s\n",
info.word, info.partofspeech, info.meaning, info.example);
}
}