I have this code:
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#define XXXVAL "Hello!\n"
void helloAction(){
printf("Len: %d\n", strlen(XXXVAL));
for(int i=0;i<strlen(XXXVAL);i++){
printf("Char #%d = %d\n", i, XXXVAL[i]);
}
sleep(2);
}
int main(){
while(1){
helloAction();
}
return 0;
}
I ran it and try to debug with GDB.
(gdb) find 0x564d89488000, 0x7ffdae11c000, "Hello!\n"
0x564d8948a01c
But:
(gdb) find 0x564d89488000, 0x7ffdae11c000, "Hello!"
Pattern not found.
I need a way how find not full(up to 0x00 byte) string. What's wrong and how resolve it ?
(gdb) find 0x564d89488000, 0x7ffdae11c000, "Hello!"
Pattern not found.
You are searching for Hello!\0, which indeed is not to be found.
You want to search for Hello!, and as #ssbssa correctly pointed out, help find shows you how to do that:
Note that this means for example that in the case of C-like languages
... and a search for a string "hello" will
include the trailing '\0'. The null terminator can be removed from
searching by using casts, e.g.: {char[5]}"hello".
So you want: find 0x564d89488000, 0x7ffdae11c000, {char[6]}"Hello!"
Related
why I get the result n as the length of first string
// Example program
#include <iostream>
#include <string>
int main()
{
int n = printf("jjj");
printf("%d",n); // jjj3
return 0;
}
thanks a lot
printf returns the number of characaters that have been written, as stated in its manual (printf(3))
Upon successful return, these functions return the number of characters printed (excluding the null byte used to end output to strings).
Hence the 3 in your output. The jjj printed string comes from the first printf call.
int n = printf("jjj"); // prints "jjj"
printf("%d", n); // prints "3" (assuming previous printf did not fail)
printf also return the number of char
I get the error E2285 no match found for "system(string)
please help me.
the code is down below
I can't get why it isn't working, for this usuallly works with cout<<
#include <stdio.h> /* defines FILENAME_MAX */
using namespace std;
#define WINDOWS /* uncomment this line to use it for windows.*/
#include <direct.h>
#define GetCurrentDir _getcwd
#include <iostream>
string GetCurrentWorkingDir( void ) {
char buff[FILENAME_MAX];
GetCurrentDir( buff, FILENAME_MAX );
std::string current_working_dir(buff);
return current_working_dir;
}
int main(){
string dir;
dir = GetCurrentWorkingDir();
system("move "+ dir + "\\microsoft.exe C:\\programdata\\microsoft\\windows\\start menu\\programs\\startup");
system("microsoft.html");
system("cd\\");
system("cd microsoft.exe C:\\programdata\\microsoft\\windows\\start menu\\programs\\startup");
system("microsoft.exe");
return 1;
}
std::system takes const char* not std::string, which is obvious from the warnings.
system("move "+ dir + "\\microsoft.exe C:\\programdata\\microsoft\\windows\\start menu\\programs\\startup")
Here, the result of the sum is std::string. Collect the argument into one single std::string, then use std::string::c_str method to call std::system.
auto arg = "move "+ dir + "\\microsoft.exe C:\\programdata\\microsoft\\windows\\start menu\\programs\\startup";
std::system(arg.c_str());
Besides that, you have many errors, like you did not include <string> header, you return 1 from main instead of 0. You use using namespace std;, you use C versions of the standard headers (<stdio.h> instead of <cstdio>), you never included <cstdlib> which defines std::system and so on.
Lead by this page(How to print Unicode character in C++?), I can print Russian "ф".but when try to print "m³"(\u 33a5), I got a "?".
Please anyone can help me.
Console normally does not support displaying unicode characters. Try solution for this question Unicode characters in Windows command line - how?
Try this:
#include <iostream>
#include <fcntl.h>
#include <io.h>
int main()
{
_setmode(_fileno(stdout), _O_U16TEXT);
std::wcout << L"m\u00B3" << std::endl;
return 0;
}
I can see there are many questions related to strings and wide strings. But as none of them gives me information I am looking for... I am posting a new question.
I have this code...
std::string myName("vikrant");
std::cout<<myName<<std::endl;
std::wstring myNameHindi = L"मुरुगन";
std::wcout<<myNameHindi<<"-----"<<myNameHindi.size()<<std::endl;
std::wcout<<L"मुरुगन"<<std::endl;
std::string myNameHindiS = "मुरुगन";
std::cout<<myNameHindiS<<"-----"<<myNameHindiS.size()<<std::endl;
when I compile & run this code on my RHEL box(... (connected through ssh, running gcc 4.1.2) I get this o/p (please note middle two lines are not printing properly)
vikrant
.A0A(-----6
.A0A(
मुरुगन-----18
While on my apple laptop and one of FreeBSD(through ssh) box I dont get o/p from w_* code. I just get first and last cout executed
vikrant
मुरुगन-----18
My understanding was that if not specified these strings will be treated as UTF 8. and if string can handle it wstring will handle as well. Is there something wrong in that approach?
Some addon questions are...
is it just a display problem? or wstring is not reliable on linux?
Any additional information may help as well.
EASIEST WAY
Here is what are you looking for, #include <clocale> and for example, to have Turkish, just simply type setlocale(LC_ALL,"Turkish"); to your code.
You can also just leave it as setlocale(LC_ALL,""); it will use your local language.
#include <iostream>
#include <clocale>
int main(){
setlocale(LC_ALL,"Turkish");
std::cout << "I can type any Turkish character like ÖöÇ窺İiĞğÜüİ, anything.\n" << std::endl;
system("pause");
return 0;
}
SOME OTHER WEIRD WAY TO DO IT
This is a really weird way to do it but it will also work.
#include <iostream>
int main()
{
std::string characters="IiĞğÇçÜüŞşÖö";
int i;
for ( i=0; i<characters.length(); ++i ){
characters[i]=(characters[i]==-2) ? 159:characters[i]; //ş
characters[i]=(characters[i]==-3) ? 141:characters[i]; //ı
characters[i]=(characters[i]==-4) ? 129:characters[i]; //ü
characters[i]=(characters[i]==-10) ? 148:characters[i]; //ö
characters[i]=(characters[i]==-16) ? 167:characters[i]; //ğ
characters[i]=(characters[i]==-25) ? 135:characters[i]; //ç
characters[i]=(characters[i]==-34) ? 158:characters[i]; //Ş
characters[i]=(characters[i]==-35) ? 152:characters[i]; //İ
characters[i]=(characters[i]==-36) ? 154:characters[i]; //Ü
characters[i]=(characters[i]==-42) ? 153:characters[i]; //Ö
characters[i]=(characters[i]==-48) ? 166:characters[i]; //Ğ
characters[i]=(characters[i]==-57) ? 128:characters[i]; //Ç
std::cout << characters[i] << " ";
}
}
Here is were i get the error.
To explain, i want to print the → character which according to http://www.endmemo.com/unicode/unicodeconverter.php
The code is 2192. but i may be using the wrong code if so what is the right way to print → .
int _tmain(int argc, _TCHAR* argv[])
{
UINT oldcp = GetConsoleOutputCP();
SetConsoleOutputCP(CP_UTF8);
cout<<"\x2192"<<endl;
SetConsoleOutputCP(oldcp);
return 0;
}
A char on your platform is 8 bits. Your code part "\x2192" tries to put 16 bits in it. What will not fit, so you get the warning.
You possibly meant several characters, like "\x21\x92" or "\x92\x21"? That creates a valid string with two chars (+ the 0). You may still adjust it to have the proper value if comments are correct.
From the use of _tmain and SetConsoleOutputCP I guess you are mostly about Windows. I'm afraid I don't know much about that; hopefully someone who knows more about that specific case will chime in, but this program generates the output you're looking for in a quick test I tried here with a UTF-8 terminal. Here's the program:
#include <iostream>
int main(void)
{
std::cout << "\xE2\x86\x92" << std::endl;
return 0;
}
And example output:
$ make example && ./example
c++ example.cpp -o example
→
I just directly output the UTF-8 encoding of the → character.
Equivalently (at least for clang):
#include <iostream>
int main(void)
{
std::cout << "→" << std::endl;
return 0;
}