1
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct stCrimeArchive {
   char id_student[10];
   int id_document;
   char judgement[30];
   int id_crime;
   char date[12];
   char id_police[12];
};

int main()
{
   struct stCrimeArchive *regi;
   FILE *filea;
   filea = fopen("crimearchives.dat", "r+b");          
   if(!filea) 
 filea = fopen("crimearchives.dat", "w+b");

   int i;
   char num[6];

   regi = (struct stCrimeArchive*)malloc (sizeof(struct stCrimeArchive));

   printf("ID DOCUMENT: ");
   fgets(num, 6, stdin);
   regi->id_document= atoi(num);

   printf("ID STUDENT: ");
   fgets(regi->id_student, 30, stdin);
   for(i = strlen(regi->id_student)-1; i && regi->id_student[i] < ' '; i--)
    regi->id_student[i] = 0;

   printf("CRIME CODE: ");
   fgets(num, 6, stdin);
   regi->id_crime = atoi(num);

   printf("DATE OF THE CRIME: ");
   fgets(regi->date, 30, stdin);
   for(i = strlen(regi->date)-1; i && regi->date[i] < ' '; i--)
    regi->date[i] = 0;   

   printf("ID POLICE: ");
   fgets(regi->id_police, 30, stdin);
   for(i = strlen(regi->id_police)-1; i && regi->id_police[i] < ' '; i--)
    regi->id_police[i] = 0;  

   printf("JUDGEMENT: ");
   fgets(regi->judgement, 30, stdin);
   for(i = strlen(regi->judgement)-1; i && regi->judgement[i] < ' '; i--)
    regi->judgement[i] = 0;     

   fseek(filea, 0, SEEK_END);
   fwrite(&regi, sizeof(struct stCrimeArchive*), 1, filea);

   free(regi);
   fclose(filea);
}  

上下文如下:我正在制作一个可变长度文件,并在其上使用上面显示的结构编写许多犯罪档案。为此,我需要为所涉及的结构做一个 malloc。问题是当我将它写入文件时,它什么也没写,而且我不知道我在做什么错误。

我也想从同一个文件中读取犯罪档案,但我也不知道我做错了什么。这是我正在尝试做的代码:

int main()
{ 
   struct stCrimeArchive *regi;
   regi = (struct stCrimeArchive*)malloc (sizeof(struct stCrimeArchive));
   FILE *filea;
   filea = fopen("crimearchives.dat", "r+b");          
   if(!filea) 
    filea = fopen("crimearchives.dat", "w+b");
   rewind(filea);
   while  (fread(&regi, sizeof(struct stCrimeArchive), 1, filea))
    printf("%2d %-11s %-11s %8d %-13s %-14s\n", regi->id_document, regi->id_police, regi->date, regi->id_crime, regi->id_student, regi->judgement);
   fclose(filea);
}
4

5 回答 5

4

如果我是你,我会从你的例子中完全忘记使用malloc。您不需要为此进行动态内存分配。

struct CrimeArchive regi;
regi.id_student = 42;
/* do all your stuff here */

FILE* fp = fopen("crimearchives.dat", "wb");
if (fp != NULL) {
    if (fwrite(&regi, sizeof regi, 1, fp) != 1)
       { /* handle error */ }

    fclose(fp);
}

当您从中阅读时也是如此:

struct CrimeArchive regi;

FILE* fp = fopen("crimearchives.dat", "rb");
if (fp != NULL) {
   if (fread(&regi, sizeof regi, 1, fp) != 1)
       { /* handle error */ }

   fclose(fp);
}

请记住,如果文件是在一个系统上写入并从另一个系统上读取,则不能保证像这样的文件向/从文件写入和读取结构是可移植的。

于 2012-09-06T02:03:55.530 回答
2

您将要写入结构的大小,而不是指向结构的指针的大小。

 fwrite(regi, sizeof(struct stCrimeArchive), 1, filea);

或者也许这会更清楚:

 fwrite(regi, sizeof(*regi), 1, filea);
于 2012-09-06T01:55:39.447 回答
1

Your fwrite call is wrong:

fwrite(regi, sizeof(struct stCrimeArchive*), 1, filea);

It's right that you're passing a pointer to the memory to be written, it's wrong that the size is that of a pointer and not the size of the actual struct you're intending to write. Chances are you'll only be writing 4 or 8 bytes of the start of the struct to file.

Change sizeof(struct stCrimeArchive*) to sizeof(struct stCrimeArchive), the same size you pass to malloc.

However, your code still doesn't compile since there's no such type stCrimeArchive (should be CrimeArchive?) and another reason being that you you're trying to assign an int to char[] in the following line:

regi->id_student = atoi(num);
于 2012-09-06T02:01:06.130 回答
0

很抱歉很晚才回答这个问题,但我想知道问题是如何解决的。答案之一是正确的,问题是写结构的大小。

对于写作过程:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct stCrimeArchive {
   char id_student[10];
   int id_document;
   char judgement[30];
   int id_crime;
   char date[12];
   char id_police[12];
};

int main()
{
   struct stCrimeArchive *regi;
   FILE *filea;
   filea = fopen("crimearchives.dat", "r+b");          
   if(!filea) 
 filea = fopen("crimearchives.dat", "w+b");

   int i;
   char num[6];

   regi = (struct stCrimeArchive*)malloc (sizeof(struct stCrimeArchive));

   printf("ID DOCUMENT: ");
   fgets(num, 6, stdin);
   regi->id_document= atoi(num);

   printf("ID STUDENT: ");
   fgets(regi->id_student, 30, stdin);
   for(i = strlen(regi->id_student)-1; i && regi->id_student[i] < ' '; i--)
    regi->id_student[i] = 0;

   printf("CRIME CODE: ");
   fgets(num, 6, stdin);
   regi->id_crime = atoi(num);

   printf("DATE OF THE CRIME: ");
   fgets(regi->date, 30, stdin);
   for(i = strlen(regi->date)-1; i && regi->date[i] < ' '; i--)
    regi->date[i] = 0;   

   printf("ID POLICE: ");
   fgets(regi->id_police, 30, stdin);
   for(i = strlen(regi->id_police)-1; i && regi->id_police[i] < ' '; i--)
    regi->id_police[i] = 0;  

   printf("JUDGEMENT: ");
   fgets(regi->judgement, 30, stdin);
   for(i = strlen(regi->judgement)-1; i && regi->judgement[i] < ' '; i--)
    regi->judgement[i] = 0;     

   fseek(filea, 0, SEEK_END);
   fwrite(regi, sizeof *regi, 1, filea);

   free(regi);
   fclose(filea);
}  

然后,对于阅读过程:

int main()
{ 
   struct stCrimeArchive *regi;
   regi = (struct stCrimeArchive*)malloc (sizeof(struct stCrimeArchive));
   FILE *filea;
   filea = fopen("crimearchives.dat", "r+b");          
   if(!filea) 
    filea = fopen("crimearchives.dat", "w+b");
   rewind(filea);
   while  (fread(regi, sizeof *regi, 1, filea))
    printf("%2d %-11s %-11s %8d %-13s %-14s\n", regi->id_document, regi->id_police, regi->date, regi->id_crime, regi->id_student, regi->judgement);
   fclose(filea);
}

我无话可说,但谢谢=)

于 2012-09-13T12:13:59.960 回答
0
while  (fread(&regi, sizeof(struct CrimeArchive), 1, filea))

我认为 fread 应该在这里接受 void* 但不是 void ** 。

于 2012-09-06T02:46:01.830 回答