Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/58.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用C语言从文本文件中提取列表的一列_C_Parsing_File Management - Fatal编程技术网

用C语言从文本文件中提取列表的一列

用C语言从文本文件中提取列表的一列,c,parsing,file-management,C,Parsing,File Management,这可能是一个简单的问题,答案很直截了当,但在网站上搜索时,除了python代码之外,我什么也没找到(可能是因为我对C编程不熟悉),我已经编写了python代码,效率很低 假设我在timestamps.txt文件中有一个数据列表,格式如下: <large integer>, <integer between 1 and 8> <large integer>, <integer between 1 and 8> 我相信有一个更快的方法,但我似乎无法使

这可能是一个简单的问题,答案很直截了当,但在网站上搜索时,除了python代码之外,我什么也没找到(可能是因为我对C编程不熟悉),我已经编写了python代码,效率很低

假设我在
timestamps.txt
文件中有一个数据列表,格式如下:

<large integer>, <integer between 1 and 8>
<large integer>, <integer between 1 and 8>
我相信有一个更快的方法,但我似乎无法使任何事情都起作用。
任何帮助都将不胜感激。

您的想法还不错,但您应该根据是否要复制当前输入数据,将变量
设为0或1。你必须用每一行重新设置支票

或者,您可以对当前字段进行计数,并在该字段为所需字段时复制数据

下面是一个将由
sep
逐字分隔的列
want
复制到输出文件的版本:

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

int main(int argc, char const *argv[])
{
    FILE *in = stdin;
    FILE *out = stdout;

    int want = 1;
    int col = 0;
    int sep = ',';

    for (;;) {
        int c = getc(in);

        if (c == EOF) break;

        if (c == sep) {
            col++;
        } else if (c == '\n') {
            col = 0;
            putc(c, out);
        } else if (col == want) {
            putc(c, out);
        }
    }

    return 0;
}
#包括
#包括
#包括
int main(int argc,char const*argv[]
{
文件*in=stdin;
FILE*out=stdout;
int=1;
int col=0;
int sep=',';
对于(;;){
int c=getc(in);
如果(c==EOF)中断;
如果(c==sep){
col++;
}else如果(c=='\n'){
col=0;
putc(c,out);
}else if(col==want){
putc(c,out);
}
}
返回0;
}

(我使用了
stdin
stdout
,因为我很懒,不想做打开和关闭电影的事情。)

使用
fgets
fputs
比多次调用
getc
putc
要快,你所需要的只是一个缓冲区(在这种情况下是一个小缓冲区)要存储当前行,请执行以下操作:

int main(int argc, char const *argv[])
{
    FILE *input_file;
    FILE *output_file;
    char buf[128];
    char *ptr;

    input_file = fopen("timestamps.txt","r");
    output_file = fopen("singles.dat","w");
    if (!input_file)
        return -1; /* use EXIT_FAILURE instead of -1 */
    /* you forget to check output_file */
    while (fgets(buf, sizeof buf, input_file)) {
       ptr = strchr(buf, ','); /* find the comma */
       if (ptr != NULL) {
           fputs(ptr + 1, output_file); /* +1 to skip the comma */
       }
    }
    fclose(input_file);
    fclose(output_file);
    return 0;
}

你现在得到了什么?除了我刚刚发布的代码外,我只是尝试了各种尝试,尝试了
fscanf
fprintf
的组合,但都不起作用。我这样做是因为逗号后面有一个空格,所以当它遇到逗号时,我将其设置为2,然后空格使它变为1,最后它满足了我需要的数字。是的,但是你只会得到空格,因为在那之后,
check
对于每个字符都是递减的。您可以更改每个字符的状态,但它只应在找到分隔符(逗号或换行符)时更改。不,如果在找到逗号时将check设置为1,则将获得空格。if在减量之前,所以类似于:if->not successment:check是负数;如果->不满意:逗号->检查到2;如果->无:空格->检查为1;如果->满意!复制的数字:数字->检查变为零,但数字已经被复制了Kay,但这是一个非常专业的版本,依赖于有一个空格和一位数字。(我知道你说过它是一个从1到8的整数。不管怎样,似乎你已经得到了你想要的答案。)这个方法非常有效。不过,我没有将1添加到
ptr
,而且效果很好。我写的是
while((fgets(line,sizeof(line),input_file))!=NULL)
{ptr=strchr(line.);fputs(ptr,output_file);}
是因为逗号后面的空格
int main(int argc, char const *argv[])
{
    FILE *input_file;
    FILE *output_file;
    char buf[128];
    char *ptr;

    input_file = fopen("timestamps.txt","r");
    output_file = fopen("singles.dat","w");
    if (!input_file)
        return -1; /* use EXIT_FAILURE instead of -1 */
    /* you forget to check output_file */
    while (fgets(buf, sizeof buf, input_file)) {
       ptr = strchr(buf, ','); /* find the comma */
       if (ptr != NULL) {
           fputs(ptr + 1, output_file); /* +1 to skip the comma */
       }
    }
    fclose(input_file);
    fclose(output_file);
    return 0;
}