|
Hi cedric
i am giving here my code:
..........................
CFile flCPhone;
CStdioFile fl;
char strFilter[] = { "Text Files (*.txt)|*.txt|" };
CFileDialog FileDlg(FALSE, ".txt", NULL, 0, strFilter);
if( FileDlg.DoModal() == IDOK )
{
if( flCPhone.Open(FileDlg.GetFileName(), CFile::modeCreate | CFile::modeWrite) == FALSE )
{ return;}
CArchive ar(&flCPhone, CArchive::store);
ar<
|
|
|
|
|
Ouch... Sorry, I have mixed CFile with FILE*
But I think your question has already been answered
|
|
|
|
|
Hi cedric
its ok..
I wasn't using the member functions of CFile..
There is no function for appending text to a file..in CFile (..I suppose)..
How do i go about it?
|
|
|
|
|
Then you can use FILE* and open it in appending mode ("-a"). Take a look at fopen in the doc:
FILE* pFile = fopen("YourFile.txt","a");<br />
fwrite(.....);
|
|
|
|
|
mpallavi wrote:
There is no function for appending text to a file..in CFile (..I suppose)..
Sure there is. Just go to the end of the file before writing to it. Remember to use CFile::modeNoTruncate when opening the file.
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
|
|
|
|
|
hi david..
thanx
I could append text to a already existing file.. I want the newly entered text to appear on a new line.. I used CFile::seektoend().. how to take the pointer to new line? ..
regards
pal
|
|
|
|
|
Insert a CR and/or LF character first.
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
|
|
|
|
|
try this :
FILE *file;
char *FileName = "C:\\TestFile.txt";
char *string_to_save = "this is a test";
file = fopen(FileName,"w");
if(!file)
{
AfxMessageBox("Unable to open the specified file");
return;
}
fputs(string_to_save,file);
fputs("\n",file);
fclose(file)
return;
regards,
Eli
|
|
|
|
|
hey friends ..
i did it.. it was simple
.................
CFile flCPhone;
flCPhone.Open(FileDlg.GetFileName(), CFile::modeCreate | CFile::modeWrite);
int len;
len=strlen(m_strInput);
flCPhone.Write(m_strInput,len);
.....................
thanx for showing me the write track.. i was simply running behind CArchive..
(A beginner in vc++.. getting into it..)
thanx
pal
|
|
|
|
|
if you allow the user to choose the file name and position with File dialog,
in your flCPhone.open() use FileDlg.GetPathName() instead FileDlg.GetFileName().
regards,
Eli
|
|
|
|
|
ya fine..
thanx Eli
..
pal
|
|
|
|
|
i want get HDD serial number, and encoding it to binary number, and write this binary number into file application (example: myapplication.exe) ?
i can't completed, help me !
Regards.
|
|
|
|
|
|
mpallavi wrote:
The first character in the saved file is always a garbage value
It is the number of bytes in the CString object that were written to the archive. You should be able to verify this. Take a look at CArchive 's << operator. If the length of the CString object is >= 255 and < 0xfffe, an extra 0xff is written to the archive followed by the length of the CString object.
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
|
|
|
|
|
I am curious if thee is a form event that fires when a form gets focus after a called form is closed.
I am using a form, in VC++ .net, that calls another form to generate a text string and passes that string to a global variable. Now I want to "refresh" that calling window to load that string into a text box. "When called window closes, set text box->text = global string." or some such (I know, my techno-speak is malfunctioning. Pseudo-pseudo-code?
Thanks
Thom
--"Late to bed, Early to rise,-- -Work like hell and Advertise."- Wernher von Braun
|
|
|
|
|
I met a problem relating to use CArray in MFC.
CArray<CMyClass*,CMyClass*> m_pArr;
CMyClass is very simple
Class CMyClass
{
CString m_szName;
CString m_szId;
long m_i4Age;
}
allocate memory:
for (int i=0;i<1000;i++)
{
m_Arr.Add(new CMyClass);
}
destroy memory
for (int i = 0;i<m_Arr.GetSize();i++)
{
CMyClass*p = m_Arr[i];
if (p)
{
delete p;
p = NULL;
}
}
m_Arr.RemoveAll();
m_Arr.FreeExtra();
When I build this code in two mode: debug and release
1. In debug mode, It seems not be leaked
2. In release mode, leak!!! memory is allocates for application never
decrease although i used above code to free the memory. I don't know why there is the difference like that.
Anybody can explain me why? and give me a solution to configure in order to It can run without leak in both mode debug and release.
Thanks a lot
Tin Le,
|
|
|
|
|
I'm not familiar with CArray ( I use C++ containers ), but I don't see any problem with this. It would be easier to read your code if you checked 'Do not treat <'s as HTML tags' so it was all visiable though.
Christian
I have several lifelong friends that are New Yorkers but I have always gravitated toward the weirdo's. - Richard Stringer
|
|
|
|
|
lvantin wrote:
CArray m_pArr;
Correct Usage off CArray class is something like this, Ok let take example of your scenario only
Class CMyClass
{
CString m_szName;
CString m_szId;
long m_i4Age;
}
then you declare CArray something like this
CArray<CMyClass,CMyClass&> m_pArr;
[Vote One Here, Complete my Survey....]
Alok Gupta visit me at http://www.thisisalok.tk
"I Think Believe this Will Help"
|
|
|
|
|
It seems okay to me. Did you use a profiler to measure, or are you using Task Manager? If it is the latter, there is a good probability that the C++ Runtime Environment is not releasing the memory immediately to the OS. That is perfectly OK.
BTW, you don't really need to check if p is not NULL before deleting it, delete(p) itself handles the case where p is NULL. And setting p to NULL has no effect too.
Regards
Senthil
_____________________________
My Blog | My Articles | WinMacro
|
|
|
|
|
lvantin wrote:
2. In release mode, leak!!! memory is allocates for application never
decrease...
How are you confirming this?
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
|
|
|
|
|
I used Performance monitor, a tool is supported by Windows, to investigate this. Private Bytes of the application always increases when i used that code to allocate memory and never descreases when release it.
|
|
|
|
|
lvantin wrote:
Private Bytes of the application always increases when i used that code to allocate memory and never descreases when release it.
Which is perfectly normal.
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
|
|
|
|
|
Hey all,
I'd like to start by saying that I am an electrical engineering student and have therefore had very little experience with Visual C++. However, a part of my senior design project requires a simple rs232 interface in visual c++. My design partner and I have designed the hardware and software needed to implement a traction control system for an R/C car. The traction system works fine, but the other part of our project is wireless transmission of the traction status and speed of the 4 wheels of the car to a laptop the user will have with them. The wireless hardware has been designed and built and is sending the traction status and wheel speed measurements formatted in bytes in the following manner:
0h10, traction status, 0h20, front left wheel speed, 0h30, front right wheel speed, 0h40, rear left wheel speed, 0h50, rear right wheel speed, and then back to 0h10, ...
(where the wheels speeds are also just byte values)
I pieced together what I could find online to write a simple windows program that would display this data using the 0h10, 0h20, ..., .. qualifiers to indicate what the next byte value received will represent (right front, left, etc).
In the program I assume that I read 1 byte at a time from the serial buffer and immediately process it before the next byte arrives which should be very possible since we are using a baud rate of only 2400 bps. I simply set a flag using the qualifiers to indicate what the next byte of data will represent. This method seems like it should work, but Im not sure if I am actually reading 1 byte from the serial buffer corrently or not.
The program compiles and initially displays the current vehicle wheel speeds and traction status but then fails to update these values at a usable rate. If after the program starts any wheel speed changes, the program doesn't register the change in incoming serial data for nearly a minute and a half. I tested the incoming serial data using a regular terminal program and the serial data changes instantly in response to vehicle wheel speed changes. Its as if the data on the screen is updated at a very slow rate and windows just stores all the incoming serial data until it finishes displaying what arrived earlier. I noticed using the windows task manager that the program continues to use more memory the entire time it is running which would reinforce my hypothesis that it is storing the incoming serial data, or there could be just a memory leak I am unaware of. I believe the cause of this to be related to the way I am reading the serial buffer using Cserial. Although like I said earlier, I dont have much experience with visual c++.
I have pasted below the code I am currently using. I know some of you are experts in visual c++ that can give me a simple explanation and/or fix to my problem. I am very close, as like I said, the serial data is read and displayed correctly when the program starts, it just fails to update using the incoming data at anything other than minute or two intervals which makes no sense to me.
Thanks for any and all suggestions.
Jordan
---------------------
Visual C++ Code
---------------------
#include <string>
#include <cstring>
#include <stdlib.h>
#include <afx.h>
#include "SerialWnd.h"
#include "Resource.h"
using namespace std;
HDC hdc ;
DCB dcb;
HANDLE hCom;
char buff[2];
char buffout[2];
HDC MemDCExercising;
PAINTSTRUCT Ps;
HBITMAP bmpExercising;
HINSTANCE hInst;
BOOL fSuccess;
unsigned long dwBytesRead = 0;
BYTE abBuffer[100];
char *pcCommPort = "COM1";
DWORD dwCommEvent;
DWORD dwMask;
DWORD inqueue;
DWORD outqueue;
int TractionData=0;
int WLF=0;
int WLR=0;
int WRF=0;
int WRR=0;
char fleft[26] = "Front Left Wheel Speed: ";
char fright[27] = "Front Right Wheel Speed: ";
char rleft[25] = "Rear Left Wheel Speed: ";
char rright[26] = "Rear Right Wheel Speed: ";
char tstatus[19] = "Traction Status: ";
CSerialWnd serial;
LRESULT CALLBACK Handler(HWND hWnd, UINT iMsg, WPARAM wParam, LPARAM lParam){
if (iMsg == WM_PAINT)
{
hdc = BeginPaint(hWnd, &Ps);
// Load the bitmap from the resource
bmpExercising = LoadBitmap(hInst, MAKEINTRESOURCE(IDB_BITMAP1));
// Create a memory device compatible with the above DC variable
MemDCExercising = CreateCompatibleDC(hdc);
// Select the new bitmap
SelectObject(MemDCExercising, bmpExercising);
// Copy the bits from the memory DC into the current dc
BitBlt(hdc, 0, 0, 782, 522, MemDCExercising, 0, 0, SRCCOPY);
// Restore the old bitmap
DeleteDC(MemDCExercising);
DeleteObject(bmpExercising);
EndPaint(hWnd, &Ps);
hdc = GetDC(hWnd);
SetTextColor(hdc, RGB(0,0,0));
//SetBkColor(hdc, RGB(0,0,0));
TextOut(hdc,385,435,fleft,26);
TextOut(hdc,5,175,fright,27);
TextOut(hdc,600,350,rleft,25);
TextOut(hdc,190,100,rright,26);
TextOut(hdc,330,30,tstatus,19);
}
if (iMsg == CSerialWnd::mg_nDefaultComMsg)
{
// A serial message occurred
const CSerialWnd::EEvent eEvent = CSerialWnd::EEvent(LOWORD(wParam));
const CSerialWnd::EError eError = CSerialWnd::EError(HIWORD(wParam));
switch (eEvent)
{
case CSerialWnd::EEventRecv:
hdc = GetDC(hWnd);
SetTextColor(hdc, RGB(0,0,0));
SetBkColor(hdc, RGB(0,0,0));
serial.Read(abBuffer,1,&dwBytesRead,0,INFINITE);
sprintf(buffout,"%X",(LPCTSTR)abBuffer[0]);
SetTextColor(hdc, RGB(255,255,255));
//TextOut(hdc,0,80,buffout,2);
// Sets flags based on qualifiers
if ((buffout[0] == '1') && (buffout[1] == '0')) {
TractionData = 1;
break;
}
if ((buffout[0] == '2') && (buffout[1] == '0')) {
WLF = 1;
break;
}
if ((buffout[0] == '3') && (buffout[1] == '0')) {
WLR = 1;
break;
}
if ((buffout[0] == '4') && (buffout[1] == '0')) {
WRF = 1;
break;
}
if ((buffout[0] == '5') && (buffout[1] == '0')) {
WRR = 1;
break;
}
// Displays data based on flags
if (TractionData == 1) {
SetTextColor(hdc, RGB(0,0,0));
SetBkColor(hdc, RGB(255,255,255));
TextOut(hdc,434,30,buffout,1);
TractionData = 0;
break;
}
if (WLF == 1) {
SetTextColor(hdc, RGB(0,0,0));
SetBkColor(hdc, RGB(255,255,255));
TextOut(hdc,547,435,buffout,2);
WLF = 0;
break;
}
if (WLR == 1) {
SetTextColor(hdc, RGB(0,0,0));
SetBkColor(hdc, RGB(255,255,255));
TextOut(hdc,756,350,buffout,2);
WLR = 0;
break;
}
if (WRF == 1) {
SetTextColor(hdc, RGB(0,0,0));
SetBkColor(hdc, RGB(255,255,255));
TextOut(hdc,177,175,buffout,2);
WRF = 0;
break;
}
if (WRR == 1) {
SetTextColor(hdc, RGB(0,0,0));
SetBkColor(hdc, RGB(255,255,255));
TextOut(hdc,360,100,buffout,2);
WRR = 0;
break;
}
break;
}
return 0;
}
if (iMsg == WM_DESTROY) {
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hWnd, iMsg, wParam, lParam);
}
int PASCAL WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR szCmdLine, int iCmdShow){
//Declare variables
const char* szAppName = "MyWin";
WNDCLASSEX wndclass;
HWND hWnd;
MSG msg;
hInst = hInstance;
wndclass.cbSize = sizeof(wndclass);
wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = Handler;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = szAppName;
wndclass.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
RegisterClassEx(&wndclass);
hWnd = CreateWindow(szAppName, "Wireless Telemetry Monitor", WS_OVERLAPPEDWINDOW,0,0, 782, 522, NULL, NULL, hInstance, NULL);
ShowWindow(hWnd, iCmdShow);
UpdateWindow(hWnd);
serial.Open(pcCommPort, hWnd, WM_NULL, 0, inqueue, outqueue);
serial.Setup(CSerial::EBaud2400,CSerial::EData8,CSerial::EParNone,CSerial::EStop1);
serial.SetupHandshaking(CSerial::EHandshakeOff);
serial.SetMask(CSerial::EEventRecv);
serial.SetupReadTimeouts(CSerial::EReadTimeoutNonblocking);
while (GetMessage(&msg, NULL,0,0)){
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
|
|
|
|
|
Hi, Is there anyone who knows how to enumerate all the available domains on a network. I want to list them down on a combo box. (Same as Windows login). Thanks. Ü
SDE
|
|
|
|
|
There's a program inside Programming Windows called Blowup
hdc = GetDC (hwnd) ;
hdcMem = CreateCompatibleDC (hdc) ;
hBitmap = CreateCompatibleBitmap (hdc,
abs (ptEnd.x - ptBeg.x),
abs (ptEnd.y - ptBeg.y)) ;
SelectObject (hdcMem, hBitmap) ;
StretchBlt (hdcMem, 0, 0, abs (ptEnd.x - ptBeg.x),
abs (ptEnd.y - ptBeg.y),
hdc, ptBeg.x, ptBeg.y, ptEnd.x - ptBeg.x,
ptEnd.y - ptBeg.y, SRCCOPY) ;
Before entering this snippet the mouse was captured by SetCapture(), ptBegin and ptEnd are the points when the left button was clicked and released, So after a click and release a rectangle was produced, ptBegin lies in the upper-left corner and ptEnd is at right-bottom. "hdc" is the program's window handle
I know even part of this rectangle lies out side of program's window(drag cursor out of the client area), the region created by ptBegin and ptEnd still valid (SetCapture()), but what makes me wonder is how's the image outside the program's client area can be used as a source HDC to be copied to hdcMem.
|
|
|
|
|