printing to a text file...HELP PLEASE
-
I have created an MFC app and I am using the code that you have on the web to read and write. the first problem is 1. I think from a combo box you can only read the info as a CString (is that right?)and then I pass it to the readFile method and when I do all the casting to try to get it to write the string it only prints the first letter. Can you si what I am doing wrong? do you have suggestions? //Get current selections from edit and list-box controls CString szChoice; CString szResult; int nChoice; m_name.GetWindowText( szChoice); nChoice = m_name.GetCurSel(); //if(nChoice!=CB_ERR) { //if a valid choice was made from the list box, fetch //the item's text string. m_name.GetLBText(nChoice,szChoice); szResult="Closing after selecting "+ szChoice; fileRead(L"\\My Documents\\my_file.txt",szChoice); } void fileRead(TCHAR *szFileName, CString szChoice) { hFile = CreateFile(szFileName, GENERIC_WRITE, 0, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); //if (hFile != INVALID_HANDLE_VALUE) { // Note that we are writing a string, not a unicode string LPTSTR p = szChoice.GetBuffer( 10 );//not sure if this is nessecary wcscpy( p, L"Name: "+(szChoice)); //copying szChoice into p so I can cast(what else should I do)? szChoice.ReleaseBuffer( );//not sure about this it was in a EVC example(What is it?) const char *val = reinterpret_cast(p);//Casting p into a 'char' so I can print to file //so I could try to use strcpy AfxMessageBox (p); //gives the whole string (name:Rod) it i typed rod in combo box strcpy(buffer2, val);//copying val into buffer 2 strcpy(buffer1, "Hello dude" );//copying string "hello dude" into buffer WriteFile(hFile, buffer1, strlen(buffer), &dwBytesWritten, 0);//writing to file"hello dude WriteFile(hFile, buffer2, (szChoice.GetLength()), &dwBytesWritten, 0);//*********Only writes first letter'N' I want it //to write 'Name: Rod', what do I have to do? CloseHandle(hFile); } hFile = CreateFile(L"\\My Documents\\my_file.txt", GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); } I don't get it!!!!! When I switch the order that the buffers write, say write buffer 2 first then it only writes "N" and never gets to writing buffer1, if I write the buffer1 first it writes "hello dudeN". It is like when it gets to buffer2 it stops running and wi
-
I have created an MFC app and I am using the code that you have on the web to read and write. the first problem is 1. I think from a combo box you can only read the info as a CString (is that right?)and then I pass it to the readFile method and when I do all the casting to try to get it to write the string it only prints the first letter. Can you si what I am doing wrong? do you have suggestions? //Get current selections from edit and list-box controls CString szChoice; CString szResult; int nChoice; m_name.GetWindowText( szChoice); nChoice = m_name.GetCurSel(); //if(nChoice!=CB_ERR) { //if a valid choice was made from the list box, fetch //the item's text string. m_name.GetLBText(nChoice,szChoice); szResult="Closing after selecting "+ szChoice; fileRead(L"\\My Documents\\my_file.txt",szChoice); } void fileRead(TCHAR *szFileName, CString szChoice) { hFile = CreateFile(szFileName, GENERIC_WRITE, 0, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); //if (hFile != INVALID_HANDLE_VALUE) { // Note that we are writing a string, not a unicode string LPTSTR p = szChoice.GetBuffer( 10 );//not sure if this is nessecary wcscpy( p, L"Name: "+(szChoice)); //copying szChoice into p so I can cast(what else should I do)? szChoice.ReleaseBuffer( );//not sure about this it was in a EVC example(What is it?) const char *val = reinterpret_cast(p);//Casting p into a 'char' so I can print to file //so I could try to use strcpy AfxMessageBox (p); //gives the whole string (name:Rod) it i typed rod in combo box strcpy(buffer2, val);//copying val into buffer 2 strcpy(buffer1, "Hello dude" );//copying string "hello dude" into buffer WriteFile(hFile, buffer1, strlen(buffer), &dwBytesWritten, 0);//writing to file"hello dude WriteFile(hFile, buffer2, (szChoice.GetLength()), &dwBytesWritten, 0);//*********Only writes first letter'N' I want it //to write 'Name: Rod', what do I have to do? CloseHandle(hFile); } hFile = CreateFile(L"\\My Documents\\my_file.txt", GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); } I don't get it!!!!! When I switch the order that the buffers write, say write buffer 2 first then it only writes "N" and never gets to writing buffer1, if I write the buffer1 first it writes "hello dudeN". It is like when it gets to buffer2 it stops running and wi
rodneyk1 wrote: LPTSTR p = szChoice.GetBuffer( 10 );//not sure if this is nessecary wcscpy( p, L"Name: "+(szChoice)); //copying szChoice into p so I can cast(what else should I do)? First, this is bad. GetBuffer(10) guarantees you space for atleast 10 characters (20 bytes if UNICODE is defined, 10 otherwise), but you have no idea about the upper limit. Your second line can produce a buffer overrun. A safer way to do this is: szChoice = L"Name: " + szChoice; LPTSTR p = szChoice.GetBuffer( 10 ); Although I wouldnt recommend it either. rodneyk1 wrote: const char *val = reinterpret_cast(p);//Casting p into a 'char' so I can print to file The template argument to reinterpret_cast was lost, but Im guessing that you had char* or const char* there. The deal is that just because you can cast a LPTSTR variable (which stands for long pointer to T string, ie wchar_t* on wince) to a const char* variable does not convert the data pointed to by said variable to char's. rodneyk1 wrote: strcpy(buffer2, val);//copying val into buffer 2 (I dont see where you declare buffer2, but I will assume it's declared as char buffer2[255]; ) So, here we have val (const char*), which is pointing to the same memory area as p (wchar_t*), which points to whatever memory szChoice.GetBuffer() provides (also wchar_t*). So, if we examine the memory that val points to, it should look something like: 'N', 0, 'a', 0, 'm', ... which means that strcpy will grab the first character and then reach the 0-terminator => strcpy appends a \0 to buffer2 and then return. rodneyk1 wrote: WriteFile(hFile, buffer2, (szChoice.GetLength()), &dwBytesWritten, 0); Here, WriteFile will write 'N', 0 and then whatever garbage that buffer2 contains after the 0, until it has written szChoice.GetLength() characters. You can examine your file in a hex-editor to see what it has written, possibly all 0's after the 'N', depending on how buffer2 was declared. In short, you should read up on WideCharToMultiByte, and also look into fixing your memory managent issues. HTH Jonas --- “Our solar system is Jupiter and a bunch of junk” - Charley Lineweaver 2002