我正在尝试将数据从文件("input.txt")插入队列,但遇到了问题。
输入.txt如下:
2 18.487.548-k Juan Rodriguez 35 m
122 19.658.568-0 Chewbacca Solo 900 m
5 16.658.487-6 John Travolta 45 m
23 10.658.479-5 Perl Diamond 18 f
注意:数据之间的"空格"是制表符("\t")而不是空格。
这是我的数据结构:
/* Person */
typedef struct person {
int attentionNumber;
char id [30];
char firstName [40];
char lastName [40];
int age;
char sex[20];
}Person;
/* Node */
typedef struct node {
Person *person;
struct node *next;
}Node;
/* Queue*/
typedef struct queue {
Node *head;
Node *tail;
int size;
}Queue;
我正在使用的功能:
Queue *createQueue();
Node *createNode(Person *aPerson, Node *ptro);
void insert(Person *aPerson, int attentionNumber, char id [30], char firstName [40], char lastName [40], int age, char sex [20]);
void read();
Node *headNode(Queue *c);
Node *tailNode(Queue *c);
bool emptyQueue(Queue *c);
程序:
Queue *createQueue()
{
Queue *aQueue;
/* memory for the Queue */
if((aQueue = (Queue *) malloc(sizeof(Queue))))
{
/* Queue initialize empty */
aQueue->size = 0;
/* head and tail pointer -> Null */
aQueue->head = aQueue->tail = NULL;
}
return aQueue;
}
Node *createNode(Person *aPerson, Node *pointer)
{
Node *aNode;
/* memory for the Node */
if((aNode = (Node *) malloc(sizeof(Node))))
{
/* values of Node */
aNode->person = aPerson;
aNode->next = pointer;
}
return aNode;
}
void insert(Person *aPerson, int attentionNumber, char id [30], char firstName [40], char lastName[40], int age, char sex[20])
{
if(aPerson = (Person *) malloc(sizeof(Person)))
{
aPerson->attentionNumber = attentionNumber;
strcpy(aPerson->id, id);
strcpy(aPerson->firstName, firstName);
strcpy(aPerson->lastName, lastName);
aPerson->age = age;
strcpy(aPerson->sex, sex);
}
else
{
printf("Couldn't insert datan");
exit(0);
}
}
/* first node of Queue */
Node *headNode(Queue *c)
{
return c->head;
}
/* last node of Queue */
Node *tailNode(Queue *c)
{
return c->tail;
}
/* returns true if Queue is empty, false otherwise */
bool emptyQueue(Queue *c)
{
return (c->head == NULL) ? true : false;
}
void read()
{
Queue * C;
C = createQueue();
/* Files to read and write, respectively */
FILE *input;
FILE *output;
/* String to be read */
char data[201];
/* Temp auxiliar Person */
Person tempPerson;
/* Tokenize */
char * token;
/* Open input file */
input = fopen("input.txt" , "r");
/* not sure abour this */
if(input == NULL)
{
printf("Couldn't open filen");
exit(0);
}
/* Open output file for test */
output = fopen("output.txt" , "w");
/* Read string */
while(fgets(data, 201, input) != NULL)
{
/* Tokenize by "t" (tab) */
token = strtok(data,"t");
/* Count */
int count = 0;
while (token != NULL)
{
if(count == 0)
{
tempPerson.attentionNumber = atoi(token);
}
if(count == 1)
{
strcpy(tempPerson.id, token);
}
if(count == 2)
{
strcpy(tempPerson.firstName, token);
}
if(count == 3)
{
strcpy(tempPerson.lastName, token);
}
if(count == 4)
{
tempPerson.age = atoi(token);
}
if(count == 5)
{
strcpy(tempPerson.sex, token);
}
count++;
token = strtok(NULL, "t");
}
/* Call insert function */
insert(tempPerson, tempPerson.attentionNumber, tempPerson.id, tempPerson.firstName, tempPerson.lastName, tempPerson.age, tempPerson.sex);
/* Write output file for test */
fprintf(output, " %i ", tempPerson.attentionNumber);
fprintf(output, "%s ", tempPerson.id);
fprintf(output, "%s ", tempPerson.firstName);
fprintf(output, "%s ", tempPerson.lastName);
fprintf(output, "%i ", tempPerson.age);
fprintf(output, "%s", tempPerson.sex);
}
/* Close files */
fclose(input);
fclose(output);
}
int main (int argc , char ** argv)
{
/* Call read function */
read();
return 0;
}
我得到的错误:
In function ‘read’:
error: incompatible type for argument 1 of ‘insert’
insert(tempPerson, tempPerson.attentionNumber, tempPerson.id, tempPerson.firstName, tempPerson.lastName, tempPerson.age, tempPerson.sex);
^
note: expected ‘struct Person *’ but argument is of type ‘Person’
void insert(Person *aPerson, int attentionNumber, char id [30], char firstName [40], char lastName[40], int age, char sex[20])
我做错了什么/没有做什么?
好的,
而不是:
insert(tempPerson, tempPerson.attentionNumber, tempPerson.id, tempPerson.firstName, tempPerson.lastName, tempPerson.age, tempPerson.sex);
它应该是:
insert(&tempPerson, tempPerson.attentionNumber, tempPerson.id, tempPerson.firstName, tempPerson.lastName, tempPerson.age, tempPerson.sex);