01/10/2018, 16:42

Giúp mình fix lỗi đọc file trong C với

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

/************************************************************************************************/

typedef struct sinhvien {
	
	char name[30];
	float DTB ;
	struct sinhvien *next;
	
} sinhvien , *sinhvien_ptr;

/************************************************************************************************/


sinhvien_ptr makeNode() {
	sinhvien_ptr newNode = (sinhvien_ptr)malloc(sizeof(sinhvien));
	fflush(stdin);
	puts("What's the name of student?");
	gets(newNode->name);
	puts("how muck are the GPA of student: ");
	scanf("%f",&(newNode->DTB));
	newNode->next = NULL;

	return newNode;
}
/* Nhap vao dau list */
void push(sinhvien **head) {
	sinhvien *newSV = makeNode();
	newSV->next = *head;	// new -> head-> list
	*head = newSV;			// head -> new -> list
	printf("
");
}
/* Nhap vao cuoi list */
void append(sinhvien **head) {
	sinhvien *newSV = makeNode();
	sinhvien* tmp = *head;
	while(tmp->next != NULL) {
		tmp = tmp->next;
	};
	tmp->next = newSV;
	printf("
");
}
/* duyet list*/
int travel(sinhvien *head) {
	int num_NODE = 0;
	
	while(head != NULL) {
		num_NODE++;
		puts(head->name);
		printf("%f
",head->DTB);
		head = head->next;
	}
	printf("
");
	return num_NODE;
}
/* Ghi list vao bifile.bin */
void ghi_FILE_bin(const char* stream, sinhvien* sv)
{
	FILE* f = fopen(stream,"ab");
	
	fwrite(sv,sizeof(sv),1,f);
	
	fclose(f);
}
/* Ghi list vao data.txt */
void ghi_FILE_txt(const char* stream, sinhvien* sv)
{
	FILE* f = fopen(stream,"a");
	fprintf(f,"%s
",sv->name);
	fprintf(f,"%f
",sv->DTB);
	
	fclose(f);
}
/* Doc File data.txt */
void doc_FILE_txt(const char* stream)
{
	FILE *f = fopen(stream,"r");
	sinhvien sv;
	while(feof(f) == 0)
	{
		fgets(sv.name,sizeof(sv.name),f);
		fscanf(f,"%f",&(sv.DTB));
		
		printf("%s",sv.name);
		printf("%f
",sv.DTB);
	}
	fclose(f);
}
/* Menu choices */
void menu(sinhvien **head) {
	sinhvien *tmp;
	while(1) {
		puts("
0. Quit !");
		puts("1. Push");
		puts("2. Append");
		puts("3. Travel List");
		puts("4. Ghi file txt");
		puts("5. Doc file txt!");
		puts("6. Ghi file binary");
		puts("7. Doc file binary!");
		puts("Please choice one in these number!");
		puts("**********************************");
		int choice = -1;
		scanf("%d",&choice);
		if(choice == 0)
		{
			break;
		}
		switch(choice) {
			case 1:
				system("cls");
				push(head);
				break;
			case 2:
				system("cls");
				append(head);
				break;
			case 3:
				system("cls");
				travel(*head);
				break;
			case 4:
				system("cls");
				tmp = *head;
				while(tmp != NULL)
				{
					ghi_FILE_txt("data.txt",tmp);
					tmp = tmp->next;
				}
				break;
			case 5:
				system("cls");
				doc_FILE_txt("data.txt");
				break;
			case 6:
				system("cls");
				tmp = *head;
				while(tmp != NULL)
				{
					ghi_FILE_bin("data.bin",tmp);
					tmp = tmp->next;
				}
				break;
			case 7:
				system("cls");
				break;
			default:
				puts("The choice not correct! Please choice one in these number");
				break;
		}
	}
}
/************************************************************************************************/
int main() {
	sinhvien *head = NULL;
	menu(&head);

}

chuyện là mình đang học phần file trong C, nhưng ghi vào thì được đọc ra lại sai, các tiền bối đi trước giúp với ạ. Output của mình nó cứ lỗi như này. Thanks các tiền bối.

Bài liên quan
0