I want to load a txt file into an array like file() does in php. I want to be able to access different lines like array[N] (which should contain the entire line N from the file), then I would need to remove each array element after using it to the array will decrease size until reaching 0 and the program will finish. I know how to read the file but I have no idea how to fill a string array to be used like I said. I am using gcc version 4.4.3 (Ubuntu 4.4.3-4ubuntu5) to compile.

我想将一个txt文件加载到一个数组,比如php中。我希望能够访问像数组[N]这样的不同的行(它应该包含来自文件的整行N),然后在将数组元素使用到数组后,需要删除每个数组元素,直到达到0,程序将完成。我知道如何读取文件,但我不知道如何填充字符串数组,就像我说的那样。我正在使用gcc 4.4.3版(Ubuntu 4.4.3-4ubuntu5)编译。

How can I achieve this?

我如何做到这一点?

3 个解决方案

#1


1

I suggest you read your file into an array of pointers to strings which would allow you to index and delete the lines as you have specified. There are efficiency tradeoffs to consider with this approach as to whether you count the number of lines ahead of time or allocate/extend the array as you read each line. I would opt for the former.

我建议您将文件读入一个指向字符串的指针数组,这样您就可以按指定的方式索引和删除这些行。这种方法可以考虑效率的权衡,比如在阅读每一行时,是否要提前数出行数,或者分配/扩展数组。我会选择前者。

  1. Read the file, counting the number of line terminators you see (ether \n or \r\n)
  2. 读取文件,计算您看到的行终止符的数量(以太\n或\r\n)
  3. Allocate a an array of char * of that size
  4. 分配一个相同大小的char *数组
  5. Re-read the file, line by line, using malloc() to allocate a buffer for each and pointed to by the next array index
  6. 使用malloc()逐行重新读取文件,为每个文件分配一个缓冲区,并由下一个数组索引指向它

For your operations:

对你的操作:

  • Indexing is just array[N]
  • 索引数组只是[N]
  • Deleting is just freeing the buffer indexed by array[N] and setting the array[N] entry to NULL
  • 删除只是释放数组[N]索引的缓冲区,并将数组[N]项设置为NULL

UPDATE:

更新:

The more memory efficient approach suggested by @r.. and @marc-van-kempen is a good optimization over malloc()ing each line at a time, that is, slurp the file into a single buffer and replace all the line terminators with '\0'

@r提出的内存效率更高的方法。@marc-van-kempen是对malloc()每次对每一行进行优化的一个很好的优化,也就是说,将文件拖放到一个缓冲区中,并用'\0'替换所有的行终止符

Assuming you've done that and you have a big buffer as char *filebuf and the number of lines is int num_lines then you can allocate your indexing array something like this:

假设你已经这样做了并且你有一个很大的缓冲区叫char *filebuf并且行数是int num_lines那么你可以分配你的数组索引像这样:

char *lines[] = (char **)malloc(num_lines + 1); // Allocates array of pointers to strings
lines[num_lines] = NULL; // Terminate the array as another way to stop you running off the end

char *p = filebuf; // I'm assuming the first char of the file is the start of the first line
int n;
for (n = 0; n < num_lines; n++) {
  lines[i] = p;
  while (*p++ != '\0') ; // Seek to the end of this line
  if (n < num_lines - 1) {
    while (*p++ == '\0')  ; // Seek to the start the next line (if there is one)
  }
}

With a single buffer approach "deleting" a line is merely a case of setting lines[n] to NULL. There is no free()

使用单一的缓冲区方法“删除”一行仅仅是将行[n]设置为NULL的情况。没有免费的()

更多相关文章

  1. Bash脚本删除目录中多个文件名末尾的'x'字符数量?
  2. Bash脚本不删除给定目录中的文件
  3. 为什么“可执行文件”操作系统是独立的?
  4. 实体机与虚拟机linux文件互拷贝
  5. 如何使用for-each循环在bash中迭代文件路径?
  6. Linux学习总结(十五)文件查找 which whereis locate find
  7. 将现有数组中的所有元素传递给xargs
  8. 嵌入式Linux文件系统及其存储机制分析
  9. Oracle表按字段和|分格符导出文件

随机推荐

  1. SSL 证书选择指南
  2. 一文带你理解java中的同步工具类CountDow
  3. 你应该要理解的java并发关键字volatile
  4. android通过服务实现消息推送
  5. 一个同步工具类CyclicBarrier的详解(干货
  6. java中一个重要的原子类AtomicInteger详
  7. Docker Swarm 下搭建 MongoDB 分片+副本+
  8. 微服务初级设计指南
  9. 基于角色的访问控制
  10. java并发编程CAS机制原理分析