Reading Data from a File

:)
Last edited on
@nonana
The file has 100 iterations of the data, and I need to convert it to a comma seperated format.

This does NOT look like a data file. What are the details of your assignment?
The assignment is C
a program which reads in the text file generated in part one and writes out the same data
as a comma separated values

The file should have a header line which identifies which column contains which values and
should look something like this:

Time,Switch0,Switch1,Switch2,Switch3,Potentiometer,Temperature,Light
0,1,1,1,1,0.50,0.14,0.90
40,1,1,1,1,0.65,0.20,0.89
Last edited on
I'm not so sure how to code this in C, (more familiar with C++ input/output).

However, you could have a loop which reads a string from the file until it finds "time:".

At that point, you are positioned at the start of a set of data. Read the time value, (integer), read and ignore the text "ms". Then read each of the following pairs, (string, number). Note the last 3 are floating-point, the others are integer.

Either output the values as you go along, or store until you have a complete set from time: to light: and then output the whole group, separated by commas and ending with a newline.

Repeat until there are no more strings to be read from the file. Or something a bit like that. It depends on how much validation is needed - will the file possibly contain incomplete or invalid data and so on.
okay,

so I am thinking of storing the ints and floats in an array until the set is complete. Does that sound like a viable way to start?
closed account (48T7M4Gy)
use strtok adapted to each line in the cycle of output by referring to this tutorial and the sample program. The delimiter is a space character

http://www.cplusplus.com/reference/cstring/strtok/?kw=strtok

Better still, why not get the Arduino to output the csv data? It would be a simple one-liner and wouldn't add more than a few precious bytes if you wanted both formats.
so I am thinking of storing the ints and floats in an array until the set is complete. Does that sound like a viable way to start?

Yes, that would be possible. You'd could use an array of 5 integers, and then another array of 3 floating-point values.


Or you might use a structure (I'm used to doing this in C++, but it can be useful in C as well).
1
2
3
4
5
6
7
typedef struct {
    int time;
    short Switch[4];
    double potentiometer;
    double temperature;
    double light;    
} Measurement;

Thanks for the tip! Does that work like a function?
@Chervil
Does that work like a function?

No. It's just a way of grouping a set of variables together. If you're not familiar with using struct so far, you might be better to use arrays instead.

I was going to link to a tutorial page, but a C++ tutorial won't be much use. This might help
http://www.cprogramming.com/tutorial/c/lesson7.html
OP: here's a C++ way of doing your task, you are welcome to slice and dice it into C in whichever way you wish if that helps. The CSV format of the output was checked here: http://www.convertcsv.com/csv-viewer-editor.htm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <iostream>
#include <fstream>
#include <sstream>

using namespace std;

struct Reading
{
    double time;
    double switch0;
    double switch1;
    double switch2;
    double switch3;
    double potentiometer;
    double temperature;
    double light;
};

ofstream& operator << (ofstream& os, const Reading& r)
{
    os << r.time << " ," << r.switch0 << " ," << r.switch1 << ", " << r.switch2 << " ," << r.switch3 <<
    " ," <<r.potentiometer << " ," << r.temperature << ", " << r.light << '\n';
    return os;
}

using namespace std;

int main()
{
    ifstream infile("C:\\test.txt");
    ofstream outfile ("D:\\result.csv");
    outfile << "Time"<<','<<"Switch0"<<','<<"Switch1"<<','<<"Switch2"<<','<<
    "Switch3"<<','<<"Potentiometer"<<','<<"Temperature"<<','<<"Light\n";

    if(infile)
    {
        Reading r;
        string dummy1;
        getline(infile, dummy1);//read and discard first two lines;
        string dummy2;
        getline (infile, dummy2);
        while(infile)
        {
            string dummy;
            infile >> dummy >> r.time >> dummy;
            infile >> dummy >> r.switch0;
            infile >> dummy >> r.switch1;
            infile >> dummy >> r.switch2;
            infile >> dummy >> r.switch3;
            infile >> dummy >> r.potentiometer;
            infile >> dummy >> r.temperature;
            infile >> dummy >> r.light;
            infile >> dummy;
            if (infile)
            {
               outfile << r;
            }
        }
    }
}


ofstream& operator << (ofstream& os, const Reading& r)
{
os << r.time << " ," << r.switch0 << " ," << r.switch1 << ", " << r.switch2 << " ," << r.switch3 <<
" ," <<r.potentiometer << " ," << r.temperature << ", " << r.light << '\n';
return os;
}

I am having trouble converting this part ^^ to C. I understand how the struct works, but this part I am not understanding. Thank you!
 
ofstream& operator << (ofstream& os, const Reading& r)

This is a function using C++ features.
In C it might look like this:
 
void print(FILE * os, const Reading * r)


This bit
 
os << r.time << ", "
might look like this in C
 
fprintf(os, "%f, ", r->time); 

You might need to adjust the format string "%f, " depending on the type, e.g. integer of double etc of the value being printed, number of decimal places etc.
http://www.cplusplus.com/reference/cstdio/fprintf/



Last edited on
closed account (48T7M4Gy)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main() {
    
    FILE * fp_read = NULL;
    FILE * fp_write = NULL;
    
    char * line = NULL;
    char* pch = NULL;
    
    size_t len = 0;
    int line_count = 0;
    int token_count = 0;
    
    // OPEN READ-FROM FILE
    fp_read = fopen("arduino-read.txt", "r");
    if (fp_read == NULL)
        exit(EXIT_FAILURE);
    
    //  OPEN WRITE-TO FILE
    fp_write = fopen("arduino-write.txt", "w");
    if (fp_write == NULL)
        exit(EXIT_FAILURE);
    
    // HEADINGS
    printf("time, switch0, switch1, switch2, switch3, potentiometer, temperature, light\n");
    fprintf(fp_write, "time, switch0, switch1, switch2, switch3, potentiometer, temperature, light\n");
    
    while ((getline(&line, &len, fp_read)) != -1)
    {
        // FILTER OUT IRRELEVANT LINES
        if(line_count > 1 && (line_count - 1) % 9 != 0)
        {
            // TOKENIZE EACH LINE - ACCEPT 2nd TOKEN
            token_count = 0;
            
            pch = strtok (line, " ");
            while (pch != NULL)
            {
                pch = strtok (NULL, " \n");
                
                if (token_count == 0)
                {
                    printf ("%s", pch);
                    fprintf (fp_write, "%s", pch);
                }
                token_count++;
            }
            
            //GET THE COMMAS RIGHT
            if((line_count) % 9 == 0)
            {
                printf("\n");
                fprintf (fp_write, "\n");
            }
            else
            {
                printf(", ");
                fprintf (fp_write,  ", ");
            }
        }
        
        line_count++;
    }
    
    fclose(fp_read);
    fclose(fp_write);

    exit(EXIT_SUCCESS);
}


arduino_read.txt
Press and release switch 0
Release switch 0
time: 0 ms
switch0: 0
switch1: 0
switch2: 0
switch3: 0
potentiometer: 3.97
temperature: 0.78
light: 0.37
---------------------------
time: 40 ms
switch0: 0
switch1: 0
switch2: 0
switch3: 0
potentiometer: 12.34
temperature: 0.79
light: 0.42
---------------------------
time: 22 ms
switch0: 0
switch1: 0
switch2: 0
switch3: 0
potentiometer: -8.97
temperature: 0.79
light: 0.22
---------------------------
time: 40 ms
switch0: 0
switch1: 0
switch2: 1
switch3: 0
potentiometer: 3.97
temperature: 0.79
light: 0.23
---------------------------


arduino_write.txt
time, switch0, switch1, switch2, switch3, potentiometer, temperature, light
0, 0, 0, 0, 0, 3.97, 0.78, 0.37
40, 0, 0, 0, 0, 12.34, 0.79, 0.42
22, 0, 0, 0, 0, -8.97, 0.79, 0.22
40, 0, 0, 1, 0, 3.97, 0.79, 0.23
Program ended with exit code: 0
Last edited on
Thank you all for helping.
I got the program to read in the numbers and passing them by characters, but at the very bottom of the file it is reading from there are some word like "The file is complete". How can I omit this from being passed into the file being written to. I was thinking of having using an if statement and checking it to see if it matched the string "The file is complete" and then stop reading.
closed account (48T7M4Gy)
How come you removed your OP?
...
Last edited on
I was trying to be incognito mode.
closed account (48T7M4Gy)
Why?
closed account (48T7M4Gy)
I think your question about the last line is one you should be able to solve yourself in your in cognitive state. So please just green tick this thread as complete.

Good luck and have a nice day. :)
Last edited on
Topic archived. No new replies allowed.