したがって、DLLから印刷する場合、すべてのレコードについて、[姓]、[名]、[住所]、[居住地]のフィールドで同じ文字列値を取得します。これらのフィールドはすべて文字列値を保持します。印刷するノードごとに、顧客ID、住所番号、郵便番号、支出などの正しい算術値を取得します。これが私のメインです:
#include <stdio.h>
#include <stdlib.h>
#include "ClientList.h"
#define SIZE_T 5000
#define YES 1
#define NO 0
main(int argc, char *argv[]){
FILE *fp=NULL;
unsigned long customerid;
char clname[SIZE_T];
char cfname[SIZE_T];
char Address[SIZE_T];
unsigned int AddressNumber;
char PlaceOfResidence[SIZE_T];
unsigned int PostalCode;
float Expenditure;
ClientList *List = ClientList_create();
fp=fopen(argv[1],"r");
while(fscanf(fp,"%lu %s %s %s %d %s %u %f \n", &customerid, clname, cfname, Address, &AddressNumber, PlaceOfResidence, &PostalCode, &Expenditure) != EOF){
//printf("+++ Just read: %lu %s %s %s %d %s %u %.02f \n",customerid, clname, cfname, Address, AddressNumber, PlaceOfResidence, PostalCode, Expenditure);
ClientNode *Node = ClientNode_create(customerid, clname, cfname, Address, AddressNumber, PlaceOfResidence, PostalCode, Expenditure);
ClientList_printNode(Node);
ClientList_pushfront(List, Node);
}
int K = size(List);
unsigned long custid;
char *name;
printf("The size of the list is %d records \n",K);
printf("Enter Customer ID you wish to search:\n");
scanf("%lu",&custid);
int M = ClientList_search(List, custid);
if(M == YES)
printf("YES\n");
else
printf("NO\n");
Print_List(List);
ClientList_destroy(List);
fclose(fp);
exit(0);
}
また、ここに私の挿入関数とprint_list関数があります。
void ClientList_pushfront(ClientList *list, ClientNode *node){
node->next = list->head;
node->previous = NULL;
if(list->head != NULL){
node->next = list->head;
list->head->previous = node;
}
else{
list->tail = node;
}
list->head = node;
list->size ++;
}
void Print_List(ClientList *list)
{
ClientNode *current = malloc(sizeof(ClientNode));
current = list->head;
while(current)
{
printf("Customer ID: %lu | Last Name: %s | First Name: %s | Address: %s | Number: %u | Place of Residence: %s | Postal Code: %d | Expenditure: %.02f |\n", current->customerid, current->LastName, current->FirstName, current->Address, current->AddressNumber, current->PlaceOfResidence, current->PostalCode, current->Expenditure);
current = current->next;
}
}
私のCreate_Node関数:
ClientNode *ClientNode_create(unsigned long customerid, char *LastName, char *FirstName, char *Address, unsigned int AddressNumber, char *PlaceOfResidence, unsigned int PostalCode, float Expenditure){
ClientNode *client = malloc(sizeof(ClientNode));
client->Expenditure = Expenditure;
client->customerid = customerid;
client->FirstName = FirstName;
client->LastName = LastName;
client->Address = Address;
client->AddressNumber = AddressNumber;
client->PostalCode = PostalCode;
client->PlaceOfResidence = PlaceOfResidence;
client->next = NULL;
client->previous = NULL;
return client;
}
そして、これは私が得る出力の一部です:
Customer ID: 14260622 | Last Name: Pickett | First Name: Norma | Address: Todd | Number: 333 | Place of Residence: Robinwood | Postal Code: 23209 | Expenditure: 1030.00 |
Customer ID: 18723325 | Last Name: Pickett | First Name: Norma | Address: Todd | Number: 264 | Place of Residence: Robinwood | Postal Code: 42473 | Expenditure: 924.00 |
Customer ID: 16243937 | Last Name: Pickett | First Name: Norma | Address: Todd | Number: 350 | Place of Residence: Robinwood | Postal Code: 34297 | Expenditure: 402.00 |
Customer ID: 16451445 | Last Name: Pickett | First Name: Norma | Address: Todd | Number: 253 | Place of Residence: Robinwood | Postal Code: 14361 | Expenditure: 449.00 |
あなたではClientNode_create
機能、あなたはchar型のポインタではなく、コピーした値をchar型のポインタに。これは、すべての構造体のテキストフィールドがすべてmain
。の同じcharバッファを指すことを意味します。
一つの解決策は、使用することですmalloc
とstrcpy
。
client->FirstName = malloc(strlen(FirstName)+1);
strcpy(client->FirstName, FirstName));
すべての文字列に対して。または、これを実行する関数を記述します。多くのライブラリには、strdup
まさにこれを実行する関数も含まれています。
そしてfree
、後で思い出に忘れないでください!
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加