" C Array" read from a file

Hello,
As a beginner in C/C++ .I am having the following problem.

In one of my works ,I need to read data from a file.The file is what is called a "C Array" form.In the file (.txt) there are a series of character single dimensional arrays char peer0_0[],char peer0_1[],...... which are storing hexadeximal values and the array size is fixed. I want to know if there a way to directly read an array into an array in the program and directly use it.Or what is the other way to deal with this huge data. Its basically a UDP packet.I need to read the data and the parse it.

Below is how my file content is.
char peer0_0[] = {

0xff, 0xdd, 0x21, 0x27, 0x05, 0x09, 0x43, 0xb2,

0x08, 0x74, 0xa0, 0x0d, 0x7e, 0xde, 0x0d, 0x3f,

0xd9, 0x08, 0xa2, 0xcc, 0x08, 0x85, 0xed, 0x07,

0x68, 0xfe, 0x07, 0x81, 0x32, 0x09, 0x52, 0xa1,

0x09, 0x4f, 0x23, 0x08, 0x62, 0x00, 0x00, 0x0f
.........
}
char peer0_1[] = {
..................} so on


Thank you.
You could directly #include the .txt file in your program, and then compile it.

If that is not convenient, maybe because the file is very large, or the content will change frequently, then you will probably have to read and parse it as an ordinary text file. That means for example reading this part char peer0_0[] = { and doing whatever validation you feel necessary. In particular, the opening brace '{' would be recognised by your code, then you could read the values and store them in an array or vector until the closing brace is reached.

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
#include <iostream>
#include <fstream>
#include <string>
#include <vector>

    using namespace std;

int main()
{
    const char datafilename[] = "code.txt";
    ifstream datafile(datafilename);

    if (!datafile.good())
    {
        cout << "Could not open file: " <<  datafilename << endl;
        return 1;
    }

    vector <unsigned char> data1;

    string datatype, dataname;
    char equalssign, openbrace;

    datafile >> datatype >> dataname >> equalssign >> openbrace;

    if (openbrace != '{')
    {
        cout << "file parse error: " <<  datafilename << endl;
        return 1;
    }

    string hexvalue;
    char ** endptr = 0; // for strtol;
    
    // Read the actual data into the vector
    while (datafile >> hexvalue && hexvalue[0] == '0')
    {
        long n = strtol(hexvalue.c_str(), endptr, 0 );
        data1.push_back(n);
    }

    // Output the contents of the vector
    cout << "Number of items: " << data1.size() << endl;
    for (int i=0; i<data1.size(); i++)
    {
        cout << hex << uppercase << int(data1[i]) << " ";
    }

    return 0;
}
Last edited on
Thanks Chervil,
As I am not a seasoned programmer,
Why hexvalue && hexvalue[0]
35
36
37
38
39
40
    // Read the actual data into the vector
    while (datafile >> hexvalue && hexvalue[0] == '0')
    {
        long n = strtol(hexvalue.c_str(), endptr, 0 );
        data1.push_back(n);
    }


It's quite common to read something from an input stream, and verify that the read was successful, like this:
1
2
3
4
    while (datafile>>hexvalue)
    {
        // do something with the item which was read
    }

Here, this part does the actual input from the file, datafile>>hexvalue
To test whether the input was successful, we could then test the state of the input stream, like this, if (datafile.good()), but there is an abbreviated version, where we can do both the input and the check in one line, like this: if (datafile>>hexvalue)

All of that is pretty much a typical usage. The next part is specific to this program. We are interested in the individual items of data, like this: 0xff,, so this test hexvalue[0] == '0' is a basic verification that the first character is a zero. Eventually the item read will be the closing brace "}" and that test will give false, thus ending the loop.

So to clarify, perhaps extra parentheses will make the meaning clearer, and don't do any harm.
35
36
37
38
39
40
    // Read the actual data into the vector
    while ( (datafile >> hexvalue) && (hexvalue[0] == '0') )
    {
        long n = strtol(hexvalue.c_str(), endptr, 0 );
        data1.push_back(n);
    }


One more thing. We are interested in this "0xff" but are actually getting this "0xff," with a trailing comma. In this case it doesn't matter, as the function strtol() will stop at the first character which isn't part of a valid number.

Finally, I'm assuming the format of the input file doesn't have any extra spaces. That is, I expect this 0xff, 0xdd, 0x21, rather than this 0xff , 0xdd , 0x21 ,. The second version is valid c code, but would require a small adjustment to the code I gave or it will halt at the first comma.

Last edited on
Topic archived. No new replies allowed.