Hi,
I'm learning C++ language and I have a problem with a binary read file.
I know how to read binary file line at line and store each line into char variable. For input file I must know how many lines it contains, then store each lines into uint32_t array.
I used following code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
infile = fopen(input_file.c_str(), "rb");
if (!infile) {
LOG("File" + input_file + " not found.");
}
char line_buffer[32];
rows_number = 0;
while (fgets(line_buffer, sizeof(line_buffer), infile)) {
++rows_number;
}
uint32_t* rows = new uint32_t[rows_number];
memset(rows, 0, rows_number * sizeof(rows[0])); // I DONT KNOW IF IT IS USEFUL
for (unsigned i = 0; i < rows_number && fgets(line_buffer, sizeof(line_buffer), infile); ++i) {
rows[i] = (uint32_t)line_buffer;
}
First, I know that above code is not optimized, but I'm learning c++ then optimization is my second issue...first is have working code ;).
Second, I read file two times: first to get line number and second to get line content. In the second 'for', I wish to store content into uint32_t array, but I get a cast error while do it. Actually I don't know how to realize it, I looking for a function like fgets that reads the content into uint32_t or something suitable to make a cast on...but I found nothing.
I ask an advice on how to resolve this issue.
Any help is welcome.
I know how to read binary file line at line and store each line into char variable.
You're confusing the two terms. If you want to read a line at a time, you don't open the file as binary and you can use line at a time functions like fgets and fprintf.
If you don't care about lines you open the file as binary and use fread/fwrite/fseek/ftell.
std::ifstream is;
is.open (input_file, std::ios_base::binary );
// get length of file:
is.seekg (0, std::ios::end);
length = is.tellg();
DUMP("Len of "+input_file+" is: "+to_string(length));
rows_number = /* ROW NUMBERS */
;
DUMP("Rows number "+to_string(rows_number));
uint32_t* rows = new uint32_t[rows_number];
memset(rows, 0, rows_number * sizeof(rows[0]));
unsigned i = 0;
while(!is.eof()) {
is.read(buff, 32);
rows[i] = (uint32_t) buff;
}
is.close();
than now I read file block at block (=32bit). But I have always the same issue while trying to store buff into rows (uint32_t array). If I try to do cast i get: error: cast from ‘char*’ to ‘uint32_t’ loses precision.
Does it exist a way to do that ?
Now I can read binary content and store it into uint32_t array...
Only one thing...in your first code, I can read file content but it is stored into buff char * array....then I must convert it in uint32_t array element to use it.
Second code works perfectly !! I read file content and store it directly into uint32_t array.
That will only be an issue for you if you're dealing with data from different kinds of computer. If you're just doing stuff on one kind of computer, it's not something you need to worry about (yet).
The two versions of code should have identical effect. But I haven't tested it, so maybe not.