gpt4 book ai didi

C:读取二进制问题

转载 作者:行者123 更新时间:2023-11-30 20:02:08 25 4
gpt4 key购买 nike

我知道fopen可以执行“rb”(读取二进制)和“wb”(写入二进制)来读取和写入二进制文件。这是我在这样的文件中阅读的内容:

(编辑:这是为那些想知道的人提供的结构)

typedef struct student
{
char lname[ 10 ], initial[1], fname[ 10 ];
unsigned long SID;
float GPA;
struct student *next;
} SREC;

这是我的变量:

FILE *fptr, *fout;
SREC *temp = NULL;
SREC *firstName = NULL;
SREC *lastName = NULL;
SREC *studentID = NULL;
SREC *grade = NULL;
char lname[10] = "";
char fname[10] = "";
char mid[1];
unsigned long SID = 0;
float GPA = 0.0;
char temp2[100];
char inString[100];
int servercmd = 0;
int fileOpen = 0;
int totalCount = 0;

...以下内容位于 main 内部:

if ((fptr = fopen("data", "rb+")) == NULL)
{
printf("No file detected. Creating...\n");
if ((fout = freopen("data","wb+",stdout)) == NULL)
{
fprintf(stderr,"Cannot generate file!\n");
exit(1);
}
else
fclose(fout);
}
else
{
if (!feof(fptr))
{
fileOpen = 1;
insert_student(&temp," "," "," ",00000,0.00,1);
while(!feof(fptr))
{
/* fscanf(fptr,"%s %s %c %d %f",lname,fname,&mid[0],&t1,&GPA); */

fread(&temp,sizeof(SREC),1,fptr);

/* Make sure it's not empty. */
printf("Is it strcmp?\n");
if (strcmp(temp->lname," ") != 0)
{
insert_student(&firstName,temp->lname,temp->initial,temp->fname,temp->SID,temp->GPA,1);
insert_student(&lastName,temp->lname,temp->initial,temp->fname,temp->SID,temp->GPA,2);
insert_student(&studentID,temp->lname,temp->initial,temp->fname,temp->SID,temp->GPA,3);
insert_student(&grade,temp->lname,temp->initial,temp->fname,temp->SID,temp->GPA,4);
totalCount++;
printf("%d\n",totalCount);
}
}
printf("Finished reading.\n");
}
else
{
printf("File is empty.\n");
}
}

这是我分配内存来存储信息的地方。

void insert_student(SREC **studentPtr, char last[10], char init[1], char first[10], unsigned long SID, float GPA, int mode)
{
SREC *newNode;
SREC *previous;
SREC *current;

newNode = (SREC*)malloc(sizeof(SREC));

if(newNode != NULL) /*Make sure memory was available*/
{
strcpy(newNode -> lname,last);
strcpy(newNode -> initial,init);
strcpy(newNode -> fname,first);
newNode->SID = SID;
newNode->GPA = GPA;
newNode->next = NULL;

previous = NULL;
current = *studentPtr;

/* Traverses list until end or larger data is found.
* If order doesn't matter, only append to the end by
* removing second condition or insert at the beginning
* in constant time. */
if (mode == 1) /*first name*/
{
while(current != NULL && strcmp(first,current->fname) > 0)
{
previous = current;
current = current -> next;
}
}
else if (mode == 2)
{
while(current != NULL && strcmp(last,current->lname) > 0)
{
previous = current;
current = current -> next;
}
}
else if (mode == 3)
{
while(current != NULL && SID > current->SID)
{
previous = current;
current = current -> next;
}
}
else if (mode == 4)
{
while(current != NULL && GPA > current->GPA)
{
previous = current;
current = current -> next;
}
}

if (previous == NULL) /*newNode is placed at the beginning*/
{
newNode -> next = *studentPtr;
*studentPtr = newNode;
}
else /*newNode is placed in middle or end*/
{
previous -> next = newNode;
newNode -> next = current;
}

}
else
{
printf("%s not inserted\n",last);
}
}

我的程序中 while 循环之后的 fread 部分发生段错误。数据通过

以二进制形式保存
fwrite(&lastName,sizeof(SREC),totalCount,fout);

每放入一条记录,totalCount 就会增加,每删除一条记录,totalCount 就会减少。所有这些东西都有效。它只是读取字节并尝试使其不会出现我遇到的问题。我做错了什么?

再次重申,这是出现段错误的问题代码:

fread(&temp,sizeof(SREC),1,fptr);

最佳答案

这一行

fread(&temp,sizeof(SREC),1,fptr);

正在将数据读入temp的位置。您想要将数据读入(大概)为 temp 分配的内存中。删除&

fread(temp,sizeof(SREC),1,fptr);

关于C:读取二进制问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23463366/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com