请教:反复使用一个struct可以实现吗?
我想试验一下能否反复使用一个struct而不用使用struct数组,写了一个代码,作用是将一个英语词的各个信息存入一个struct然后将这个struct的内容写入file,重复这个过程直到输入者自行退出,最后也是只用一个struct读取file中的内容到屏幕上。代码如下,但达不到预期效果。
/* 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();
}
}
/* 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);
}
}
When the "r+", "w+", or "a+" access type is specified, both reading and writing are allowed (the file is said to be open for “update”). However, when you switch between reading and writing, there must be an intervening fflush, fsetpos, fseek, or rewind operation. The current position can be specified for the fsetpos or fseek operation, if desired.
[解决办法]
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);
}