从文本文件中读取一行并将数据用struct内的空格分隔 [英] Read a line from text file and place data separated by spaces inside struct
问题描述
我必须用C编写一个函数,该函数将文件指针作为输入并返回如下结构:
I've to write a function in C which takes as input a filepointer and returns a structure like this:
typedef struct product{
char * code_product;
char * name;
char * code_piece;
only_time_t enter;
only_time_t exit;
}product_t;
此结构使用另一个这样的结构:
This struct uses another struct like this:
typedef struct only_time{
int hour;
int minute;
int second;
}only_time_t;
为了从文件中读取,我使用了getline()
函数,并且我使用了strtok()
函数来创建令牌.这是我用来从文件中读取的功能:
In order to read from the file I use the getline()
function and the I use the strtok()
function to create tokens. Here is the function I use to read from the file:
product_t * read_line(FILE * fp){
char * line = NULL;
size_t len = 0;
product_t * temp;
int i = 0;
temp = (product_t *) malloc(sizeof(product_t));
temp->code_product = (char *) malloc(sizeof(char) * 4);
temp->name = (char *) malloc(sizeof(char) * 60);
temp->code_piece = (char *) malloc(sizeof(char) * 4);
//read a line from the file
getline(&line, &len, fp);
//handle line info info
char *tokens[80];
tokens[0] = strtok(line," ,.");
while (tokens[i] != NULL) {
i++;
tokens[i] = strtok(NULL," ,.");
}
temp->code_product = tokens[0];
temp->name = tokens[1];
temp->code_piece = tokens[2];
temp->enter = timestring_to_time(tokens[3]);
temp->exit = timestring_to_time(tokens[4]);
//cleanup
if (line)
free(line);
return(temp);
}
为了查看程序已读的内容,我使用了一个简单的函数来打印struct:
In order to see what the program has read I use a simple function which prints the struct:
void print_product(product_t * product){
printf("product_t code_product: %s \n", product->code_product);
printf("product_t name: %s \n", product->name);
printf("product_t code_piece: %s \n", product->code_piece);
printf("product_t enter: %d:%d:%d \n", product->enter.hour,product->enter.minute,product->enter.second);
printf("product_t exit: %d:%d:%d \n", product->exit.hour,product->exit.minute,product->exit.second);
}
我已经设置了一个测试用例,该测试用例在文本文件中有以下行(称为test.txt,并与可执行文件位于同一文件夹中):
I have setup a testcase which has the following line in the text file (called test.txt and placed in the same folder as the executalble):
H235 Sportello_dx N246 15:20:43 15:27:55
H235 Sportello_dx N246 15:20:43 15:27:55
但是程序的输出是:
product_t code_product: ��)��
product_t name:
product_t code_piece: N246
product_t enter: 15:20:43
product_t exit: 15:27:55
这是一个粘贴程序,可以运行所有代码: https://pastebin.com/9rz0vM5G
here is a pastebin with the entire code ready to run: https://pastebin.com/9rz0vM5G
为什么我在前两行中得到了这个奇怪的输出,但是其余的仍在工作?
why am I getting this wierd output for the first 2 lines but the rest is working?
推荐答案
strtok()在解析字符串时会对其进行修改,并返回指向该字符串内部的指针(在您的情况下为line
).因此,当您以后在free(line)
中释放temp
结构引用的内存.研究strdup()
.
strtok() modifies the string while it is parsed and returns pointers to inside that same string, in your case line
. So when you later free(line)
you free the memory the temp
structure references. Research strdup()
.
这篇关于从文本文件中读取一行并将数据用struct内的空格分隔的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!