how to show all ASCII character in RichEdit2 visual c++ - visual-c++

I want to display all ascii characters(http://msdn.microsoft.com/en-us/library/Aa245273) including NULL in richEdit2 control of visual c++ but i don't know how?
so please help help me how can i do this?
Actually i can show all character but when ASCII NUL is found from that richedit box doesn't show any further character and i want to display all character my code is given below please help me
my code is given below
void CclientCheckDlg::ReadFileData()
{
char *readfilename= configfilepath ;
//FILE *fp=fopen(readfilename ,"r");
std::ifstream openFile(readfilename,std::ios::out | std::ios::binary);
//string s;
unsigned char c;
//std::string s;
int i=0;
do
{
c=openFile.get();
if(c==EOF||i==999)
break;
unsignedCharPointer[i]=c;
i++;
printf("%c",c);
}while(!openFile.eof());
//fclose(fp);
CString cs(unsignedCharPointer);
OutputDebugString(cs);
// configArrStr=s.c_str();
int begin=m_rich_edit.GetTextLength();
m_rich_edit.SetSel(begin,begin);
// CA2CT ct(unsignedCharPointer);
lpctstr=(LPCTSTR)unsignedCharPointer;
OutputDebugString(lpctstr);
m_rich_edit.ReplaceSel(cs);
}
so please can anyone help me,how can i display null character?

//function to read binary file(Unicode) and write to richedit2
void function()
{
CFile cFile(TEXT("edit.moc"), CFile::modeRead);
EDITSTREAM es={0};
//HWND hwnd=getwin
OutputDebugStringA("........... OnBnClickedButton1() ");
es.pfnCallback= &EditStreamCallBack;
es.dwCookie = (DWORD) &cFile;
m_richEdit.StreamIn(SF_TEXT, es);
LRESULT result = SendMessageA(::GetDlgItem(hwnd, IDC_RICHEDIT21),EM_STREAMIN,SF_TEXT,(LPARAM)&es);
}
//callback function, declared in header file
DWORD CALLBACK EditStreamCallBack(DWORD_PTR dwCookie, LPBYTE pbBuff, LONG cb, LONG *pcb)
{
//MessageBoxA(L"hi...1");
OutputDebugStringA("........... OnBnClickedButton1() 2");
CFile* pFile = (CFile*) dwCookie;
*pcb = pFile->Read(pbBuff, cb);
return 0;
}
Thank you all.

Related

Writing rapidjson document to a file using PrettyWriter

I have been unable to find a direct answer to this question. After searching for some time, I've written the following code but I'm sure that there exists a simpler way of doing the same task.
int persistJSONChanges(rapidjson::Document& fa_cloneDoc, string jsonFilePath)
{
FILE* lp_file = fopen(jsonFilePath.c_str(), "w");
rapidjson::StringBuffer buffer;
rapidjson::PrettyWriter<rapidjson::StringBuffer> writer(buffer);
fa_cloneDoc.Accept(writer);
string temp=buffer.GetString();
unique_ptr<char[]>l_writeBuffer(new char[temp.size()]);
rapidjson::FileWriteStream l_writeStream(lp_file, l_writeBuffer.get(), temp.size());
rapidjson::PrettyWriter<rapidjson::FileWriteStream> l_writer(l_writeStream);
bool l_returnStatus=fa_cloneDoc.Accept(l_writer);
if(l_returnStatus==false)
{
cout<<endl<<"file update failed"<<endl;
return -1;
}
fclose(lp_file);
return 0;
}
I think you misused the FileWriteStream. It just needs a buffer of arbitrary size.
You simply needs:
FILE* fp = fopen(...);
char buffer[1024];
FileWriteStream fs(fp, buffer, sizeof(buffer));
PrettyWriter<FileWriteStream> writer(fs);
document.Accept(writer);
fclose(fp);
I have tried the following which worked for me:
int persistJSONChanges(rapidjson::Document& fa_cloneDoc, string jsonFilePath)
{
rapidjson::StringBuffer buffer;
rapidjson::PrettyWriter<rapidjson::StringBuffer> writer(buffer);
bool l_returnStatus=fa_cloneDoc.Accept(writer);
if(l_returnStatus==false)
{
fprintf(stdout,"\n[%s::%d] JSON File update failed\n",__FILE__,__LINE__);
return -1;
}
string temp=buffer.GetString();
ofstream out(jsonFilePath.c_str(),std::ofstream::trunc);
out<<temp;
return 0;
}
I don't use
rapidjson::Document
This is work for me:
namespace rpj = rapidjson;
FILE* fp_r = fopen("json.json", "w" );
char buf[BUF_SIZE];
rpj::FileWriteStream os(fp_r, buf, sizeof (buf));
rpj::PrettyWriter<rpj::FileWriteStream> writer(os);
writer.StartObject();
writer.String("hello");
writer.String("world");
writer.String("arr");
writer.StartArray();
writer.Int(33);
writer.Int(34);
writer.Int(36);
writer.EndArray();
writer.EndObject();
fclose(fp_r);

Change read_proc_t read_proc and write_proc_t write_proc of an existing file in /proc

I'm actually working lkm on linux 2.6.32, and I don't to understand one thing. I'm trying to change the original read_proc and write_proc of /proc/version with my functions. Thus I can to change the original value of read_proc and write_proc, with values of my function. I can see it, because values of read_proc and write_proc change to NULL to adress of my functions, but that have no effect... And I don't understand why. I don't arrive to find if version is protected (I tried to change the value of file's right with chmod), or why even after change the value, I can't write in /proc/version with echo XXXX > /proc/version. I'll be grateful for your help.
Code where I try to change values of read_proc and write_proc:
static void Proc_init()
{
int find = 0;
pde = create_proc_entry("test", 0444, NULL); //that permit to create new file in /proc, only to get some useful values
ptdir = pde->parent; //affect to ptdir the value of the pointer on /proc
if(strcmp(ptdir->name, "/proc")!=0)
{
Erreur=1;
}
else
{
root = ptdir;
remove_proc_entry("test", NULL);
ptr_subdir=root->subdir;
while(find==0)
{
printk("%s \n", ptr_subdir->name);
if(strcmp("version", ptr_subdir->name)==0)
find=1;
else
ptr_subdir=ptr_subdir->next;
}
//Save original write et read proc
old_read_proc=ptr_subdir->read_proc;
old_write_proc=ptr_subdir->write_proc;
// Before I have null values for prt_subdir->read_proc and ptr_subdir->write_proc
ptr_subdir->read_proc=&new_read_proc_t;
ptr_subdir->write_proc=&new_write_proc_t;
// after that, values of prt_subdir->read_proc and ptr_subdir- >write_proc are egual to values of &new_write_proc_t and &new_read_proc_t
}
}
static int new_read_proc_t (char *page, char **start, off_t off,int count, int *eof, void *data)
{
int len;
/* For example - when content of our_buf is "hello" - when user executes command "cat /proc/test_proc"
he will see content of our_buf(in our example "hello" */
len = snprintf(page, count, "%s", our_buf);
return len;
}
static int new_write_proc_t(struct file *file, const char __user *buf,unsigned long count, void *data)
{
/* If count is bigger than 255, data which user wants to write is too big to fit in our_buf. We don't want
any buffer overflows, so we read only 255 bytes */
if(count > 255)
count = 255;
/* Here we read from buf to our_buf */
copy_from_user(our_buf, buf, count);
/* we write NULL to end the string */
our_buf[count] = '\0';
return count;
}

How to convert guid to *char

I would like to convert a CLSID to a *char in c++ so I can display it in a text box. I am new to c++ so please make this as simple a s possible.
Thanks
C'ish solution:
/* 128 bit GUID to human-readable string */
char * guid_to_str(const GUID * id, char * out) {
int i;
char * ret = out;
out += sprintf(out, "%.8lX-%.4hX-%.4hX-", id->Data1, id->Data2, id->Data3);
for (i = 0; i < sizeof(id->Data4); ++i) {
out += sprintf(out, "%.2hhX", id->Data4[i]);
if (i == 1) *(out++) = '-';
}
return ret;
}
This assumes the output buffer has been already allocated, and should be of a size of 37 bytes (including the null terminating character).
The output is of the form "75B22630-668E-11CF-A6D9-00AA0062CE6C"
Usage example:
GUID g;
char buffer[37];
std::cout << guid_to_str(&g, buffer);
Note:
This code exists because I had to implement GUID parsing under Linux, otherwise I would have used the Windows API function StringFromCLSID mentioned by #krowe.
Here is a great example for converting GUID to string and vice versa that I am using in my projects:
std::string guidToString(GUID guid) {
std::array<char,40> output;
snprintf(output.data(), output.size(), "{%08X-%04hX-%04hX-%02X%02X-%02X%02X%02X%02X%02X%02X}", guid.Data1, guid.Data2, guid.Data3, guid.Data4[0], guid.Data4[1], guid.Data4[2], guid.Data4[3], guid.Data4[4], guid.Data4[5], guid.Data4[6], guid.Data4[7]);
return std::string(output.data());
}
GUID stringToGUID(const std::string& guid) {
GUID output;
const auto ret = sscanf(guid.c_str(), "{%8X-%4hX-%4hX-%2hX%2hX-%2hX%2hX%2hX%2hX%2hX%2hX}", &output.Data1, &output.Data2, &output.Data3, &output.Data4[0], &output.Data4[1], &output.Data4[2], &output.Data4[3], &output.Data4[4], &output.Data4[5], &output.Data4[6], &output.Data4[7]);
if (ret != 11)
throw std::logic_error("Unvalid GUID, format should be {00000000-0000-0000-0000-000000000000}");
return output;
}
In the example, it firsts uses char* before converting to string so this is exactly what you are looking for in an efficient way.
The Windows API has a function for this:
CLSID clsid;
HRESULT hr = CLSIDFromProgID ((OLESTR "Adobe.SVGCtl.3"),&clsid);
// Get class id as string
LPOLESTR className;
hr = StringFromCLSID(clsid, &className);
// convert to CString
CString c = (char *) (_bstr_t) className;
// then release the memory used by the class name
CoTaskMemFree(className);
// Now c is ready to use
A CLSID is the same as a UUID, so you can use the UuidToString() function
http://msdn.microsoft.com/en-us/library/windows/desktop/aa379352(v=vs.85).aspx

Append char to string - the NXC language

I want to write myself a function similar to PHP's str_repeat. I want this function to add specified amount of characters at the end of string.
This is a code that does not work (string argument 2 expected!)
void chrrepeat(const char &ch, string &target, const int &count) {
for(int i=0; i<count; i++)
strcat(target, ch);
}
I don't exactly know what language is that (C++?), but you seem to be passing a char to strcat() instead of a null-terminated string. It's a subtle difference, but strcat will happily access further invalid memory positions until a null byte is found.
Instead of using strcat, which is inefficient because it must always search up to the end of the string, you can make a custom function just for this.
Here's my implementation in C:
void chrrepeat(const char ch, char *target, int repeat) {
if (repeat == 0) {
*target = '\0';
return;
}
for (; *target; target++);
while (repeat--)
*target++ = ch;
*target = '\0';
}
I made it return an empty string for the case that repeat == 0 because that's how it works in PHP, according to the online manual.
This code assumes that the target string holds enough space for the repetition to take place. The function's signature should be pretty self explanatory, but here's some sample code that uses it:
int main(void) {
char test[32] = "Hello, world";
chrrepeat('!', test, 7);
printf("%s\n", test);
return 0;
}
This prints:
Hello, world!!!!!!!
Convert char to string.
void chrrepeat(char ch, string &target, const int count) {
string help = "x"; // x will be replaced
help[0] = ch;
for(int i=0; i<count; i++)
strcat(target, help);
}

Initial assignment a Char Array using a Function in C

as we know it in C, a string defining is,
char string[] = "Hello World";
That is OK,
But I want to use a function and at initial same up,
I tried those, For example;
char * to_string()
{
return "Hello World";
}
Or;
char * to_String(void) // Function
{
char buff[16];
sprintf(buff, "%s", "Hello World");
return buff;
}
main() // main function
{
char Initial_String[] = to_String();
}
How to make this or any idea same another way.
I find what I dont send address of char Initial_String[] to fill into. No. is there Another method.
Thanks.
When you compile this, atleast in GCC, it will give you the following warning:
b.c:9: warning: function returns address of local variable
Why? Because buff[] is a local variable of function to_string(). Its scope is only inside the function to_string(). main() does not have any access to this variable. Try making buff[] a global variable instead.
Second problem: char Initial_String[] = to_String(); cannot be assigned value in this way. to_string() returns a char pointer, hence assign the value thus:
char *Initial_String = to_String();
The code below will work:
char buff[16];
char* to_String(void) // Function
{
//char buff[16]; /*this is a local variable*/
sprintf(buff, "%s", "Hello World");
return buff;
}
int main(void) // main function
{
char *Initial_String = to_String();
printf("%s", Initial_String);
return 0;
}
Yes You are right about local buffer mismake,
But This is not my wanting,
if I edit some differently,
char buff[16];
char* to_String(void) // Function
{
//char buff[16]; /*this is a local variable*/
sprintf(buff, "%s", "Hello World");
return buff;
}
int main(void) // main function
{
char *Initial_String_1 = to_String();
char *Initial_String_2 = to_String();
char *Initial_String_3 = to_String();
printf("%s", Initial_String_1 );
printf("%s", Initial_String_2 );
printf("%s", Initial_String_3 );
in this case, all strings will be same, because They have same buffer address,
I want to open the topic little more.
struct
{
long aaa;
short bbb;
int ccc;
char ddd;
.
.
. // the list goes on
}elements;
typedef struct
{
int lengt;
int *adress;
char name[10];
}_list;
char* to_String(long variable) // Function
{
sprintf(buff, "%ld", variable);
return buff;
}
int main (void)
{
_list My_List[] = {
{ sizeof(elements.aaa), &elements.aaa , to_string( elements.aaa) },
{ sizeof(elements.bbb), &elements.bbb , to_string( elements.bbb) },
{ sizeof(elements.ccc), &elements.ccc , to_string( elements.ddd) },
.
.
. //// the list goes on
};
I do not know, Do I make myself clear.
Here, string must be filled into name array, without assigning it the address.
I may have syntax mistake. the code is not tested with compiler. the idea is for illustrative purposes only.
I am trying to find a method for The purpose.
Thanks.

Resources