如何使用strtok打印来控制文本文件中的单个字符?

时间:2017-07-01 16:10:36

标签: c text char runtime-error

我有一个双轮表数据结构,填充了预订:

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);        

当我在No of Rooms Booked字段中编译时,它会向我显示一个字符(每次编译时,它都是另一个字符,并且它对所有5行都相似)。 我想错误来自这一行:

r.noRoomsBooked= (int)noRoomsBooked;  

但我不知道如何解决它。

enter image description here

1 个答案:

答案 0 :(得分:1)

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

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

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

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