ASCII to string

Hallo c-plus plus,

Is there a way, to get the letter ASCII(code:10)
to print to a string.


I will keep looking on the net, on this form.
tnx










Last edited on
std::cout << (char)10 << '\n';
Tnx Ganado :-)

but now I get an error by using the code to replace, the \n ( newline ) that comes from a text file.


1
2
3
4
5
6
7
8
9
10
        string new_line = (char)10;
        string searchFora("(crc_code_10)");
        string replaceBya(new_line);
        size_t found;

        found = data.find(searchFora);
        while (found != string::npos) {
            data.replace(found, sizeof(searchFora), replaceBya);
            found = data.find(searchFora);
        };

but code is not valid . and gif an error in construction fault the '('

string new_line = (char)10;
Surprisingly, out of the dozen or so std::string constructors, not one of them takes as an argument a single character.
https://en.cppreference.com/w/cpp/string/basic_string/basic_string

Your options are as follows:
1
2
    std::string str("\n");
   

1
2
   std::string str2(1, '\n');
    

1
2
   std::string str3(1, (char)10);
    
Last edited on
I still don't get the enter return from the text file.

in text file text.txt "The planet is a star, (enter )
and the sun in the galaxy ....."

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
    
        // open file : Text.txt
    if (std::ifstream ifs{ "Text.txt" }) {
        ifs >> std::noskipws;
        std::string data((std::istream_iterator<char>(ifs)), std::istream_iterator<char>());
    
        std::string new_line(1, char(13));
        string searchFora("(crc_code_10)");
        string replaceBya(new_line);
        size_t found;

        // replace the enter return 
        found = data.find(searchFora);
        while (found != string::npos) {
            data.replace(found, sizeof(searchFora), replaceBya);
            found = data.find(searchFora);
        };
        
    
        // update data here
        SetWindowTextA(hWndEdit, LPCSTR(data.c_str()));
    }
    else
        MessageBox(hWnd, L"No file found", L"Error", MB_ICONERROR);







I can't make sense of that last post, but are you trying too hard here?

string s; //empty string
s += (char)(10); //put the char 10 into a string.
cout << s;

Note that gui text boxes may require you to enable 'want multiline' or whatever the setting is called before it will work.
To display multi-line text in a message box use '\n' just as you can use in a console mode app.
1
2
3
4
5
6
7
8
9
10
11
#include <windows.h>

int WINAPI wWinMain(_In_     HINSTANCE hInstance,
                    _In_opt_ HINSTANCE hPrevInstance,
                    _In_     PWSTR szCmdLine,
                    _In_     int iCmdShow)
{
   MessageBoxW(NULL, L"Hello\nWorld!", L"Hello Message", MB_OK);

   return 0;
}
Line 15: sizeof(searchFora) is wrong. Use searchFora.size() instead.

Since both data.find(...) and data.replace(...) also take c-style strings you don't need to convert them to std::string.
Last edited on
If you're trying to do a multiple-replace by replacing all occurrences of one string with another, then possibly:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <fstream>
#include <iostream>
#include <string>
#include <iterator>
#include <string_view>

void replace_all(std::string& str, std::string_view fnd, std::string_view rep) {
	for (size_t found {}; (found = str.find(fnd, found)) != std::string::npos; found += rep.size())
		str.replace(found, fnd.size(), rep);
}

int main() {
	if (std::ifstream ifs { "Text.txt" }) {
		std::string data((std::istreambuf_iterator<char>(ifs)), std::istreambuf_iterator<char>());

		replace_all(data, "(crc_code_10)", "\n");

		// use data here
		std::cout << data << '\n';
	} else
		std::cout << "Cannot open file\n";
}

Last edited on
He its fix's
but the problem is not was not in the reading of the file but in the CreateWindow.

that I forgot CreateWindow(ES_MULTILINE)

tnx for the assistance.
Glad you found that... I was trying to say as much, but I assumed you were using a WYSIWYG where its an option to click on your window or widget properties rather than doing it in code.
Topic archived. No new replies allowed.