从 CSV 文件中检索值

发布于 2024-10-19 17:53:23 字数 56 浏览 2 评论 0原文

我想问,是否有任何方法可以通过使用C编程来检索最后一个for中第一列的值。

谢谢!

I want to ask, if there is any way, which I can retrieve the value of the first column in the last for, by using C programming.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

初熏 2024-10-26 17:53:23

假设我的 .csv 文件如下所示,
这些字段为商品编号类别编号供应商编号

1111,1414,1000  
1112,1010, 1001  
1113,1112,1002    

用于读取上述 .csv 文件值的 C 代码:

#include <stdio.h>  
#include <stdlib.h>  
#include <string.h>  

int main(int argc, char* argv[])  
{  
   if (argc < 2)  
   {  
      fprintf(stderr,"Usage: %s csv_file\n",argv[0]);  
      return(1);  
   }  

    FILE *f = fopen(argv[1], "rt");  
    char Line[256];  
    unsigned int AllocSize = 0, Size = 0, n;  
    char *L_text;  

    while(fgets(Line, sizeof(Line), f))  
    {  

      printf("Item = %s \n",strtok(Line, ", "));  
      printf("Class = %s \n",strtok(NULL, ", "));  
      printf("Supplier = %s \n",strtok(NULL, ", "));  

    }  
   return(0);  
}  

Suppose my .csv file looks like this,
The fields are Item Number, Class Number and Supplier Number.

1111,1414,1000  
1112,1010, 1001  
1113,1112,1002    

C code for reading values of above .csv file :

#include <stdio.h>  
#include <stdlib.h>  
#include <string.h>  

int main(int argc, char* argv[])  
{  
   if (argc < 2)  
   {  
      fprintf(stderr,"Usage: %s csv_file\n",argv[0]);  
      return(1);  
   }  

    FILE *f = fopen(argv[1], "rt");  
    char Line[256];  
    unsigned int AllocSize = 0, Size = 0, n;  
    char *L_text;  

    while(fgets(Line, sizeof(Line), f))  
    {  

      printf("Item = %s \n",strtok(Line, ", "));  
      printf("Class = %s \n",strtok(NULL, ", "));  
      printf("Supplier = %s \n",strtok(NULL, ", "));  

    }  
   return(0);  
}  
以为你会在 2024-10-26 17:53:23

也许你可以看看libcs​​v

Maybe you can look at libcsv.

弃爱 2024-10-26 17:53:23

我认为这取决于你的文件的大小。如果行不是太大,也不是太多,并且您不关心您的程序是否使用 GNU 以外的 libcs​​ 运行,您可以使用 getline 函数将整行读入内存,直到到达最后一个。那么它应该就像找到第一个逗号一样简单。

如果您有一个由小行组成的大文件,您可以 fseek 到文件末尾并逐字节备份,直到找到 '\n'。然后你就到了最后一行的开头。

如果文件和行都很大,也许对整个文件执行 mmap 到文件末尾,然后读回 '\n' 可能比所有这些 fseek 更有效。尽管 mmap 的可移植性可能不如 fseeks(因为 fseek 是标准 C)。

I think it depends on the size of your file. If the lines are not too big, there are not too many of them and you don't care if your program doesn't run with libcs other than GNU, you could use getline function to read entire lines into memory until you reach the last. Then it should be as simple as finding the first comma.

If you have a large file composed of small lines, you could fseek to the end of the file and back up byte per byte until you find a '\n'. Then you are at the beginning of the last line.

If both the file and lines are huge, perhaps doing a mmap on the entire file going to the end of it and then reading back to the '\n' might be more efficient than all those fseeks. Although mmap is arguably less portable than fseeks (since fseek is standard C).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文