使用数据-C编程

使用数据-C编程,c,file-handling,C,File Handling,我写了这段代码,从传感器读取数据: char c; do{ while(!read(fd, &c, 1)); } while (c!='$'); do{ while(!read(fd, &c, 1)); } while (c!=','); do{ while(!read(fd, &c, 1)); printf("%c",c); fp = fopen("/var/www/Sensor_data.tx

我写了这段代码,从传感器读取数据:

char c;
do{
    while(!read(fd, &c, 1));    
} while (c!='$');   

do{
    while(!read(fd, &c, 1));    
} while (c!=',');   

do{
    while(!read(fd, &c, 1));

    printf("%c",c); 
    fp = fopen("/var/www/Sensor_data.txt", "a");
    fprintf(fp, "%c%", c);
    fclose(fp); 

} while (c!='\n');
该文件看起来像

518.088131,0.272969,0.376242,0.522998,0.368944,0.347478,0.343025,0.252323,0.612587,0.552753,0.120302806.230415

然后我想要一种处理这些数据的方法,所以我将数据编号2,3,4,5,6,7,8以粗体显示,并将它们分别传输到新文件中。
任何人都可以举一个这样做的程序的例子吗?或者更愿意修改我的程序,使其生成一个包含所有数据的文件,以及7个其他文件,每个文件中包含1个数据。

类似的方法可以实现此目的。但可扩展性不是很强:S

// This part has to go before main function starts
char int_to_char(int n){
    return (char) n + (int)'0';
}

// and this replaces what you already had
int reading = 1;
do{
    while(!read(fd, &c, 1));          // <- you should change this

    printf("%c",c); 
    fp = fopen("/var/www/Sensor_data.txt", "a");
    fprintf(fp, "%c", c);
    fclose(fp); 

    if (c == ',')
        reading++ ;

    else{        
        if (reading >= 2 && reading <= 8){              // 2nd to 8th reading
            char temp[] = "/var/www/Sensor_data .txt";  // <- Added a space, which I'm 
                                                    //  gonna substitute with a number
            temp[20] = int_to_char(reading);            // Substitute the space
            fp = fopen(temp, "a");
            fprintf(fp, "%c", c);
            fclose(fp);
        }
    }

} while (c!='\n');
从int到char的转换仅在数字介于0和9之间时有效。

使用此代码:

FILE *fp;   
    char c;
    do{
        while(!read(fd, &c, 1));    
    }while (c!='$');    

    char int_to_char(int n){
    return (char) n + (int)'0';
}

int reading = 1;
do{
    while(!read(fd, &c, 1));          // <- you should change this

    printf("%c",c); 
    fp = fopen("/var/www/Sensor_data.txt", "a");
    fprintf(fp, "%c", c);
    fclose(fp); 

    if (c == ',')
        reading++ ;

    else{        
        if (reading >= 2 && reading <= 8){              // 2nd to 8th reading
            char temp[] = "/var/www/Sensor_data .txt";  // <- Added a space, which I'm 
                                                    //  gonna substitute with a number
            temp[20] = int_to_char(reading);            // Substitute the space
            fp = fopen(temp, "a");
            fprintf(fp, "%c", c);
            fclose(fp);
        }
    }

} while (c!='\n');
我在Sensor_data.txt文件中得到523.488072,0.326624,0.471786,0.472091,0.317701,0.430966,0.292683,0.260292,0.653686,0.608487,0.116710806.211857。 在其他7个文件中,我得到23个。4 8 8 0. 每个文件中有1个值。 我打算将标有粗体的7值放入7个单独的文件中。我不知道这是不是很清楚,如果这是发生在你身上的事,斯莱舍兹


请注意,int到char之间的转换只有在数字介于0-9之间时才有效,这可能是问题所在吗?

我不太同意您读取数据的方式。如果传感器读数出错,可能会卡在无限循环中。在do循环中打开和关闭是缓慢的。在do循环之前只打开一次,然后在循环退出后只关闭一次。最好先使用select执行读取,等待数据可用,将所有数据读取到缓冲区,然后丢弃前导$..,然后在一次写入中将剩余的写入输出文件。这将大大加快操作系统的速度,减轻操作系统的负担。嗯,我明白你的意思,但是当我尝试运行程序时,我发现了一个错误:/知道为什么吗?@Jacob我想我解决了这个问题。我在只读内存中分配temp,这导致了问题。请尝试这个版本,并告诉我它是否工作:Dhmm它可以编译和运行。但创建的文件名为Sensor_data?.txt,当我编写sudo nano Sensor_data?txt时,它会打开7个文件,其中填充了随机数。所以我不确定写入不同文件部分是否有效。看起来它至少没有得到正确的数据。它写了24个。1 8 3每个值本身都在一个文件中。第一次测量的值是524.126453,因此它似乎跳过了5,然后取下7个值,而不是跳到预期值2-8@Jacob我在修改代码时注意到了这个bug,但忘了在这里编辑它。问题是从int reading->char的转换不是很顺利。我正在更新我的帖子。@Jacob因为你在错误的地方定义了一个函数,所以这段代码不会运行。现在可以使用了:非常感谢你的帮助和耐心!斯莱舍尔茨