Mình chưa có thời để tìm hết lỗi và sửa cho bạn. Nhưng mình thấy bạn gặp lỗi tại 2 dòng sau:
C++ Code:
pNode->Hoten = Hten; pNode->Diemso = Diem;
Bạn thử dùng strcpy() hoặc strncopy() thay vào và test lại nhé.
Em có đoạn code bên dưới đọc được nội dung file input.txt nhưng khi add vào danh sách liên kết đơn và xuất ra màng hình toàn bị mã, bác nào biết giúp e sửa với
file input.txt co noi dung
Tuan 1 A
Sang 0 B
Monh các bác giúp đỡ
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <ctype.h>
#pragma warning(disable : 4996)
//#include <stdafx.h>
using namespace std;
char tenfile[FILENAME_MAX] = "input.txt";
struct HOCVIEN
{
char* Hoten;
int Gioitinh;
char* Diemso;
};
struct Node
{
//HOCVIEN Data;
char* Hoten;
int Gioitinh;
char* Diemso;
struct Node *pNext;
};
struct list
{
Node *pHead;
Node *pTail;
};
//Node* CreateNode(HOCVIEN Hv)
Node* CreateNode(char Hten[100], int GTinh, char Diem[2])
{
Node *pNode = new Node;
if (pNode == NULL) return NULL;
//pNode->Data = Hv;
pNode->Hoten = Hten;
pNode->Gioitinh = GTinh;
pNode->Diemso = Diem;
pNode->pNext = NULL;
return pNode;
}
void Init(list &l)
{
l.pHead = l.pTail = NULL;
}
void Addtail(list &l, Node *new_e)
{
if (l.pHead == NULL)
{
l.pHead = l.pTail = new_e;
}
else
{
l.pTail->pNext = new_e;
l.pTail = new_e;
}
}
bool AddHead(Node* &pHead, char Hten[100], int GTinh, char Diem[2])
{
Node* pNode = CreateNode(Hten,GTinh,Diem);
if (pNode == NULL)
{
return false;
}
else
{
if (pHead == NULL)
{
pHead = pNode;
}
else
{
pNode->pNext = pHead;
pHead = pNode;
}
return true;
}
}
// Doc noi dung file
void Noidungfile(list &l)
{
FILE *pFile;
pFile = fopen(tenfile, "rt"); //Mo file
char Ten[100]; int Gt[1]; char Diem[2];
int i = 0;
Node* pRoot;
pRoot = NULL;
while (fscanf(pFile, "%s %d %s", Ten, Gt, Diem) != EOF) // Doc file
{
i++;
printf("%d. %s %d %s\n", i, &Ten, *Gt, &Diem); // Xuất ra ma hinh
pRoot = CreateNode(Ten, *Gt, Diem);
Addtail(l, pRoot);
}
fclose(pFile);//Dong file
//getch();
}
void PrintList(list &l)
{
Node *pNode = l.pHead;
while (pNode != NULL)
{
printf("%s %d %s\n", pNode->Hoten, pNode->Gioitinh, pNode->Diemso);
pNode = pNode->pNext;
}
}
int main()
{
list l;
Node *pNode;
Init(l);
Noidungfile(l);
PrintList(l);
getch();
}
Mình chưa có thời để tìm hết lỗi và sửa cho bạn. Nhưng mình thấy bạn gặp lỗi tại 2 dòng sau:
C++ Code:
pNode->Hoten = Hten; pNode->Diemso = Diem;
Bạn thử dùng strcpy() hoặc strncopy() thay vào và test lại nhé.
KISS
Skype: giapnguyen_bka89
Bạn có thể nói rõ hơn không, mình mới học C++ nên chưa hiểu rõ strcpy() và strncopy()
- - - Nội dung đã được cập nhật ngày 05-02-2015 lúc 09:42 AM - - -
Các bác giúp với em đang làm đồ án gấp lắm