简体   繁体   English

fseek,fread并返回二进制文件功能中的指针(C)不能正常工作

[英]fseek, fread and return the pointer (C) in binary file function not working properly

I want to return the pointer to the structure that I read from the file (it goes to another functions that basicaly just prints the data.) Function gets the (serial number -1) through int sem, and then the idea was, to set the file pointer to the beginning of the structure that I want to read with the fseek, and simply read the whole structure and return the pointer. 我想将指针返回到我从文件中读取的结构(它会转到另一个基本上只打印数据的函数。)函数通过int sem获取(序列号-1),然后进行设置我要用fseek读取的指向结构开头的文件指针,只需读取整个结构并返回指针即可。 (Example: If the number of the structures written in the file is 5 and I want to read the structure no. 4, the sem value that function gets is 3, then with fseek pointer skips the first 3 structures and function reads and return the strucutre no.4). (示例:如果文件中写入的结构数为5,而我想读取4号结构,则函数获得的sem值为3,然后使用fseek指针跳过前3个结构,函数读取并返回构造4)。 It works fine if the sem value is 0, but not so fine when I seek for any other structure. 如果sem值为0,它可以正常工作,但是当我寻找任何其他结构时,它就不会那么好。 I have no idea what goes wrong! 我不知道出什么问题了! Help? 救命? :) :)

type_seminar *file_seminars_search(int sem) {
     type_seminar *temp_s = (type_seminar*) malloc(sizeof(type_seminar));
     FILE *f_sem;
     if ((f_sem = fopen("seminars.bin", "r")) != NULL ) {
          fseek(f_sem, sem * sizeof(type_seminar), SEEK_SET);
          fread(temp_s, sizeof(type_seminar), 1, f_sem);
          fclose(f_sem);
          return (temp_s);
     } else
          printf("Cannot access file. \n ");
     return 0;
}

The definition of strucutre: strucutre的定义:

typedef struct {
     char s_title[A];
     char s_street[B];
     char s_town[C];
     int max_no_teachers;
     int no_applied_teachers;
} type_seminar;

And the function that prints data if the problem is in here: 还有问题所在的打印数据的功能:

void seminar_details(type_seminar *temp_s) {
      printf("   SEMINAR TITLE : %s", temp_s->s_title);
      printf("   Street: %s ", temp_s->s_street);
      printf("   Town: %s", temp_s->s_town);
      printf("   Max no of applied teachers: %d \n", temp_s->max_no_teachers);
      printf("   No of applied teachers: %d \n", temp_s->no_applied_teachers);
}

I feel that the way seminars.bin was created will determine the way the file is read. 我觉得workshops.bin的创建方式将决定文件的读取方式。 It may also be helpful to open your file in binary mode as compared to ascii mode. 与ascii模式相比,以二进制模式打开文件也可能会有所帮助。

I used the following function to create the seminars.bin 我使用以下功能创建了Workshops.bin

void createData()
{
FILE    *fcre;
type_seminar element;
int counter;

fcre = fopen("seminars.bin", "wb");
for(counter = 0; counter < 5; counter++)
{
    printf("Enter type_seminar.s_title:");
    scanf("%s", element.s_title);
    printf("Enter type_seminar.s_street:");
    scanf("%s", element.s_street);
    printf("Enter type_seminar.s_town:");
    scanf("%s", element.s_town);
    printf("Enter type_seminar.max_no_teachers:");
    scanf("%d", &element.max_no_teachers);
    printf("Enter type_seminar.no_applied_teachers:");
    scanf("%d", &element.no_applied_teachers);

    fwrite(&element, sizeof(type_seminar), 1, fcre);
}
fclose(fcre);
}

I modified a couple of lines in your original file as 我将原始文件中的几行修改为

if ((f_sem = fopen("seminars.bin", "rb")) != NULL ) 

and

 fread(temp_s, sizeof(type_seminar), 1, f_sem);

With these changes, I could get your code to work as per your expected design!! 通过这些更改,我可以使您的代码按照您的预期设计工作!!

Problem is at 问题出在

return 0;

line. 线。 because you're not returning int. 因为您不返回int。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM