Answer the question
In order to leave comments, you need to log in
How to read file in parts and write to const char**?
There is a file. Suppose its size is 50455 kb, I need to read it completely, but write it as const char * every 1024 kb.
What is the best way to do this?
I am writing under ubuntu, there is an internal read method, but with it you can read the entire file at once, is it possible to somehow do at the time of reading immediately form the final array from const char* ? Can this be done somehow correctly through fread?
This is how I, for example, read the entire file:
size_t filesize = (std::filesystem::file_size("путь"));
char *buf = (char*) malloc(filesize);
FILE *file = fopen("путь до файла", "b");
fread(buf, 1, filesize, file);
fclose(file);
char *inputData = malloc(dataLen);
char *p = inputData;
size_t bytesLeft = dataLen;
while (bytesLeft) {
size_t bytesRead = fread(p, 1, 1024, f);
bytesLeft -= bytesRead;
p += bytesRead;
if (ferror(f) != 0) {
fprintf(stderr, "ERROR: fread() failed\n");
free(inputData);
fclose(f);
Answer the question
In order to leave comments, you need to log in
Well, you create a char** of size (dataLen + 1023) / 1024 and repeat this code for reading in parts, assigning to the next element of this array the created char* of size 1024, into which you read such a piece of data.
fstream fs("13w87wmd.exe", std::ifstream::binary);
const int len = 1024;
char buff[len];
while(fs.readsome(buff, len)) {
cout << buff << endl;
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question