我知道fopen可以執行「rb」(讀取二進制文件)和「wb」(寫入二進制文件)來讀寫二進制文件。這是我對讀這樣的文件:C:讀取二進制文件
(編輯:這裏的結構對於那些想知道誰)
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;
...以下是主要內容:
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上升,每一條記錄被刪除時,它的股價下跌。所有這些東西的作品。它只是讀取字節並試圖使它不會導致我遇到問題的seg故障。我究竟做錯了什麼?
再次重申,這是問題的代碼這是越來越段錯誤:
fread(&temp,sizeof(SREC),1,fptr);
'temp'是如何聲明的? –
什麼是'temp','firstName','lastName'等?你沒有顯示足夠的代碼。 –
@MattiVirkkunen固定。 –