简体   繁体   English

处理数据-C编程

[英]Working with data - C programming

Ive written this code, to read data from a sensor: 我编写了以下代码,以从传感器读取数据:

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');

The file then looks like 然后文件看起来像

518.088131, 0.272969,0.376242,0.522998,0.368944,0.347478,0.343025,0.252323 ,0.612587,0.552753,0.120302,806.230415 518.088131,0.272969,0.376242,0.522998,0.368944,0.347478,0.343025,0.252323,0.612587,0.552753,0.120302,806.230415

I would then like a way to procces this data, so I take data number 2,3,4,5,6,7,8 (the ones in bold) and transfer to new files one for each. 然后,我想找到一种处理这些数据的方法,因此我将数据编号为2,3,4,5,6,7,8(以粗体显示),然后每个文件传输到一个新文件中。 Anyone can give an example of a program doing this? 任何人都可以举一个程序的例子吗? or preferred to modify my program so it makes a file with all data, and 7 other files with 1 data in each. 或最好修改我的程序,以使它制作一个包含所有数据的文件,以及一个其他七个文件,每个文件包含1个数据。

Something like this will do the trick. 这样的事情将解决问题。 Not very scalable though :S 虽然不是很可扩展: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');

The conversion from int to char only works if the number is between 0 and 9. 从int到char的转换仅在数字介于0和9之间时才有效。

With this code: 使用此代码:

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');

I get 523.488072, 0.326624,0.471786,0.472091,0.317701,0.430966,0.292683,0.260292 ,0.653686,0.608487,0.116710,806.211857 in the Sensor_data.txt file. 我得到523.488072,0.326624,0.471786,0.472091,0.317701,0.430966,0.292683,0.260292,0.653686,0.608487,0.116710,806.211857在Sensor_data.txt文件。 In the other 7 files i get 2 3 . 在其他7个文件中,我得到2 3。 4 8 8 0. 1 value in each file. 4 8 80。每个文件中有1个值。 What i intend is to get the 7 value marked with bold into 7 separate files. 我打算将7值用粗体标记为7个单独的文件。 I dont know if this was clear and if its whats happening for you SlySherZ? 我不知道这是否很清楚,SlySherZ是否对您有影响?

Just read that the conversion between int to char only works if the number it between 0-9 could this be the problem? 只是读到int到char之间的转换仅在0-9之间的数字有效时才有效,这可能是问题吗?

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

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