C ++ string too large, truncated characters

I am making a program that writes a predefined line to a file at startup. The file size is about 5 mb, so filling in a string of 5 mb of data in hexadecimal is a big variable. When I try to compile it, I get an error when the string is too large. Is 5mb really that? I split the line into 4 sections, but each section is still too large.: / What can I quickly and easily do to fix this situation.

Note: I consider myself a novice programmer, so try not to go too far above your head: P

An example of how I write a line to a file:

string file_hex("huge_a**_string_goes_here_or_in_separate_cpp"); ofstream file_out; file_out.open("tools\\c.exe", ios::binary | ios::trunc); string res; res.reserve(file_hex.size() / 2); for (int i = 0; i < file_hex.size(); i += 2) { std::istringstream iss(file_hex.substr(i, 2)); int temp; iss >> std::hex >> temp; res += static_cast<char>(temp); } file_out << res; file_out.close(); 
+6
source share
3 answers

The standard does not define a maximum limit for string literals, but it does offer a minimum size. From Appendix B - Scope of Implementation

Limits may limit quantities that include those described below or others. The number in parentheses after each quantity is recommended for at least that quantity. However, these values ​​are only recommendations and do not determine compliance.

  • Characters in a string literal (after concatenation) [65 536].

However, the same section of the standard also states the following

Because computers are finite, C ++ implementations are inevitably limited in the size of the programs that they can successfully process. Each implementation should document the limitations that are known. This documentation may refer to fixed limits where they exist, say how to calculate variable limits as a function of available resources, or say that fixed limits do not exist or are unknown.

We can safely assume that the 5 MB string literal will exceed the maximum limits imposed by the compiler. You should consult the documentation for your toolchain to determine what restrictions are placed on string literals.

If you use the Visual C ++ compiler, the maximum size of a string literal is 16,384 bytes . From the MSDN documentation

The maximum length of a string literal is 16.384 (16K) bytes. This restriction applies to strings of type char [] and wchar_t []. If the string literal consists of parts enclosed in double quotes, the preprocessor combines the parts into one string and adds an extra byte to the total number of bytes for each concatenated string.

+8
source

If you're trying to just be quick and dirty, use C-level 3 IO for this and use a static array to store the values.

 #include <stdio.h> static const unsigned char data[] = { 1, 5, 255, 128, 50, 192, // all the values of your file here, // don't add a trailing zero, this is not a string. }; int main(int argc, const char* argv[]) { FILE* fp = fopen("test.txt", "wb"); if ( fp == NULL ) { printf("No dice.\n"); return -1; } // write the entire array to the file without. fwrite(data, sizeof(data[0]), sizeof(data), fp); fclose(fp); return 0; } 
+2
source

instead of this:

 char* metaDescriptor = "very long string"; 

it should be:

 char* metaDescriptor = "very long" "string"; 
0
source

Source: https://habr.com/ru/post/946304/


All Articles