gpt4 book ai didi

c - 如何使用 strtok 从文本文件打印到控制台单个字符?

转载 作者:太空宇宙 更新时间:2023-11-04 03:21:56 25 4
gpt4 key购买 nike

我有一个双轮列表数据结构,填充有保留:

struct reservation
{
unsigned int reservationID;
char *hotelName;
unsigned char noRoomsBooked;
char *clientName;
char *reservationPeriod;
float amount;
};

struct listNode
{
reservation inf;
listNode *next;
listNode *prev;
};

这是插入函数:

listNode *insertInList(listNode*head, listNode*tail, reservation r)
{
listNode *new= (nodLista*)malloc(sizeof(listNode));
new->inf.reservationId = r.reservationId;
new->inf.hotelName= (char*)malloc((strlen(r.hotelName) + 1) * sizeof(char));
strcpy(nou->inf.hotelName, r.hotelName);
new->inf.noRoomsBooked= r.noRoomsBooked;
new->inf.clientName= (char*)malloc((strlen(r.clientName) + 1) * sizeof(char));
strcpy(nou->inf.clientName, r.clientName);
new->inf.reservationPeriod= (char*)malloc((strlen(r.reservationPeriod) + 1) * sizeof(char));
strcpy(nou->inf.reservationPeriod, r.reservationPeriod);
new->inf.amount= r.amount;

if (head == NULL)
{
head = tail = new;
new->next = head;
new->prev = head;
}
else
{
listNode *temp = head;
while (temp->next != head)
{
temp = temp->next;
}
temp->next = new;
new->prev = temp;
new->next = head;
tail = new;
}
return head;

我想用我在 txt 文件中写的某个日期填充此列表:

这是我的txt文件:

1,Hotel1,2,Client 1,10.05.2017-12.05.2017,200.5
2,Hotel2,1,Client 2,10.06.2017-17.06.2017,100.5
3,Hotel3,3,Client 3,03.03.2017-10.03.2017,800.5
4,Hotel4,2,Client 4,11.04.2017-15.04.2017,300.5
5,Hotel5,1,Client 5,10.05.2016-12.05.2016,150.5

读取文件的程序是这样的:

void main()
{
listNode *head= NULL, *tail= NULL;
reservation r;

FILE *f = fopen("Text.txt", "r");

while (!feof(f))
{
char buffer[200];
fgets(buffer, 199, f);
char *idRezervare = strtok(buffer, ",");
char *hotelName= strtok(NULL, ",");
char *noRoomsBooked= strtok(NULL, ",");
char *clientName= strtok(NULL, ",");
char *reservationPeriod= strtok(NULL, ",");
char *amount= strtok(NULL, ",");

r.reservationID= atoi(reservationID);
r.hotelName= (char*)malloc((strlen(hotelName) + 1) * sizeof(char));
strcpy(r.hotelName, hotelName);
r.noRoomsBooked= (int)noRoomsBooked;
r.clientName= (char*)malloc((strlen(clientName) + 1) * sizeof(char));
strcpy(r.clientName, clientName);
r.reservationPeriod= (char*)malloc((strlen(reservationPeriod) + 1) * sizeof(char));
strcpy(r.reservationPeriod, reservationPeriod);
r.amount= atof(amount);

head=insertInList(head, tail, r);
}

fclose(f);
printList(head);

当我在预订房间数字段中编译时,它会显示一个字符(每次我编译时,它都是另一个字符,所有 5 行都相似)。我猜错误来自这一行:

r.noRoomsBooked= (int)noRoomsBooked;  

但是我不知道怎么解决。

enter image description here

最佳答案

reservation::noRoomsBookedunsigned char,变量 noRoomsBookedchar*。您正在分配一个指针而不取消引用它。您只需将指针的值转换为 int,而不是指针指向的值:

r.noRoomsBooked = atoi(noRoomsBooked); // now it works

顺便说一下,unsigned char 不是您的最佳选择。假设房间数可能大于 255 是合理的。

此外,如果您要将此数字打印到控制台,请记住将其转换为 int - 否则您会得到垃圾。这是将 unsigned char 替换为实际整数类型的另一个原因。例如无符号短

关于c - 如何使用 strtok 从文本文件打印到控制台单个字符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44862958/

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