简体   繁体   English

从C中的文件读取令牌

[英]Reading tokens from a file in C

I'm trying to read tokens from a file and place them in an array. 我正在尝试从文件中读取令牌并将其放置在数组中。 I'm doing this in multiple steps, starting with figuring out how many tokens are in the file so i can estimate how large I need to the array to be. 我要分多个步骤进行操作,首先要弄清文件中有多少个令牌,以便可以估计数组需要多大的空间。 I was able to accomplish this using the getline() function. 我能够使用getline()函数完成此操作。

FILE *crack, *dic;
crack = fopen(crack_file, "r");
dic = fopen(dic_file, "r");

char *lineptr;
size_t n;

int dic_word_count, crack_word_count, crack_line_count;
dic_word_count = crack_word_count = crack_line_count = 1;

while(getline(&lineptr, &n, dic) != EOF)
{
    dic_word_count++;
}

char **dictionary = malloc(8*dic_word_count);

but when I try to do the same thing with a different file 但是当我尝试对不同的文件执行相同的操作时

while(getline(&lineptr,&n, crack) != EOF)
{
    crack_line_count++;
}
printf("%d",crack_line_count);

the print statement is never reached. 永远不会达到打印语句。 I'm really not sure what is going on and I'm just wondering if anybody has any ideas of what's happening and a possible fix. 我真的不确定发生了什么,我只是想知道是否有人对正在发生的事情以及可能的解决方案有任何想法。 Let me know if you'd like to see more of my code, I'm trying to keep this concise but thorough. 让我知道您是否想看更多我的代码,我正在尝试保持简洁而透彻。 thanks in advance. 提前致谢。

line needs to be initialized before the call to getline() and ... line需要在调用getline()和...之前进行初始化

getline() returns -1 on failure to read a line (including end-of-file condition). 如果无法读取行(包括文件结束条件),则getline()返回-1。 In the event of an error, errno is set to indicate the cause.". 如果发生错误,则将errno设置为指示原因。”

Use -1 as EOF is not necessarily -1. 使用-1作为EOF不一定是-1。

Ref 参考

char *lineptr = NULL;
size_t n = 0;
...
while(getline(&lineptr, &n, dic) != -1)

Note: robust code would use 注意:强大的代码将使用

size_t dic_word_count, crack_word_count, crack_line_count;
... 
printf("%zu", crack_line_count);

Problems that I see: 我看到的问题:

  1. getline is not a standard C library function. getline不是标准的C库函数。 I don't know which platform you are using and what the expectations are as far as memory allocation and deallocation. 我不知道您正在使用哪个平台,以及对内存分配和释放的期望是什么。 I would strongly suggest use of the standard C library function fgets . 我强烈建议使用标准C库函数fgets

  2. If getline expects a pointer to pre-allocated memory, then there is a problem since you are not allocating any memory. 如果getline需要一个指向预分配内存的指针,则存在问题,因为您没有分配任何内存。 If getline allocates memory, you need have code to deallocate that memory. 如果getline分配了内存,则需要有代码来释放该内存。 I see a problem either way. 两种方式我都遇到问题。

  3. You are incrementing dic_word_count for each line you are reading and then using 8*dic_word_count in the call to malloc . 您正在为要读取的每一行递增dic_word_count ,然后在对malloc的调用中使用8*dic_word_count Are you assuming something that translates to 8 ? 您是否假设要转换为8

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM