|
thanks for the suggestion.
But i need to know how to store the buffer in a file?
|
|
|
|
|
wat is the real problem ?
to read USB or to save the read buffer ?
|
|
|
|
|
i can read usb--no problem in that.
But how do I save the read buffer in to a file of desired location.?
|
|
|
|
|
sorry 4 delay
pBuff = USBReadBuff; // where pBuff and USBReadbuff are char*
CFile TestFile;
CString csFileName = C:\TestFile.txt; // csFileName is the path and name where u want to create the file
TestFile.Open( csFileName, CFile::modeCreate | CFile::modeWrite ))
TestFile.Write( &pBuff, no of bytes )
Is this ok for u
RinuRaj
|
|
|
|
|
thanks very much. I will try now in my program.
Can u also suggest how to have a different file name each time new set of data is received from USB.?
|
|
|
|
|
This is one way. It's based on Rinu_Raj's suggestion (file1_mmddyyyhhmmss)
CString csFileName;
CTime ct = CTime::GetCurrentTime();
csFileName = "file1_" + ct.Format("%m%d%Y%H%M%S");
Alcohol. The cause of, and the solution to, all of life's problems - Homer Simpson
|
|
|
|
|
thanks. When I tried the code in my program, it fails giving
:Debug assertion failed in filecore.cpp line238.
which is the CFile::Write() function.
the line is:
ASSERT(m_hFile != (UINT)hFileNull);
how to solve?
|
|
|
|
|
Did you acually open/create the file before trying to write the data?
See this suggestion[^]
Another thing, it's always wise to close a file manually, even if the CFile destructor does it for you....
Alcohol. The cause of, and the solution to, all of life's problems - Homer Simpson
|
|
|
|
|
i did exactly this:
CFile TestFile;<br />
CString csFileName = "C:/Documents/TestFile.txt"; TestFile.Open(csFileName,CFile::modeCreate | CFile::modeWrite);<br />
TestFile.Write(&Rx_Buf,3000);
what needs to be done?
|
|
|
|
|
Check the return value from the TestFile.Open call, such as:
<br />
CFile TestFile;<br />
CString csFileName = "C:/Documents/TestFile.txt"; <br />
if(TestFile.Open(csFileName,CFile::modeCreate | CFile::modeWrite)) {<br />
TestFile.Write(&Rx_Buf,3000);<br />
TestFile.Close();<br />
}<br />
else {<br />
}<br />
My guess is that there are no C:/Documents folder.
Alcohol. The cause of, and the solution to, all of life's problems - Homer Simpson
|
|
|
|
|
your guess is absolutely correct.
There is no C:/Documents..
I am storing the data values (numbers) in the file. but when I open the file TestFile.txt...i cant read the numbers. its all ASCII i guess.
In what format should I be storing the file as?
|
|
|
|
|
so please read the buffer and convert it in to readable text format then save the files.
RinuRaj
|
|
|
|
|
It depends, partly on the data you got, partly on how you want to represent it in the file.
You say you have "data values (numbers)" in the buffer. What kind of numbers?
How long are they?
One (IMO) good way to get a good grip on data is to do a hex dump of the data, and save the hex dump in the file.
Another way would be to open your saved file with a hex dump program, instead of looking at it with Notepad.
The you will see the values in it.
But I have one question for you. It's about this line of code:
TestFile.Write(&Rx_Buf,3000);
How is Rx_Buf declared? I assume it's an unsigned char[]?
In that case, remove the &-char, like this:
TestFile.Write(Rx_Buf,3000);
Else you will write something very different than your buffer in the file.
Rx_Buf is the same as the address of the first item in the array (Rx_Buf[0]).
So if you write &Rx_Buf, then C/C++ will get the *contents* of Rx_Buf[0] (and possibly following elements in the array), interpret them as an address, and then write the contents of that address to the file. That's not what you want...
Alcohol. The cause of, and the solution to, all of life's problems - Homer Simpson
|
|
|
|
|
the values read are 8 bit values (unsigned char). I declare the buffer as unsigned char* Rx_Buf = new unsigned char[3000];
The main purpose of storing the received values in a file is to retreive the file at a later stage and use the data values to plot a graph.
So when Iam reading the file again, I need to have the values as unsigned char for them to be plotted.
Whats the best way I do it?
I changed the &Rx_Buf to TestFile.Write(Rx_Buf,3000);
Thanks very much for that.
|
|
|
|
|
thathvamsi wrote: The main purpose of storing the received values in a file is to retreive the file at a later stage and use the data values to plot a graph.
So when Iam reading the file again, I need to have the values as unsigned char for them to be plotted.
You can do exactly the same thing you do when you write the file (provided you know the size of the buffer written in the file).
Just replace "CFile::modeCreate | CFile::modeWrite" with CFile::modeRead
Like this (not tested)
unsigned char Disp_Buf[3000];
CFile TestFile;
CString csFileName = "C:/Documents/TestFile.txt";
TestFile.Open(csFileName,CFile::modeRead);
TestFile.Read(&Rx_Buf,3000);
testFile.Close();
BUT!!!
Just to be sure, use CFile::typeBinary when you open/create the file.
Else, there is some text conversion (CRLF-combinations) that can mess upp your values.
So, on Create:
TestFile.Open(csFileName,CFile::modeCreate | CFile::modeWrite | CFile::typeBinary);
On Read:
TestFile.Open(csFileName,CFile::modeRead | CFile::typeBinary);
Alcohol. The cause of, and the solution to, all of life's problems - Homer Simpson
|
|
|
|
|
i did the same for reading the file.
but I get the "assertion failed" error message in filecore.cpp at
the following line:
ASSERT(AfxIsValidAddress(lpBuf, nCount));
the code that i did is as follows:
unsigned char* Rx_Buf = new unsigned char[3000000];<br />
unsigned char* read = new unsigned char[3000000];<br />
<br />
CFile TestFile;<br />
CString csFileName;<br />
CString csFileName_var;<br />
CTime ct = CTime::GetCurrentTime();<br />
csFileName = "File_" + ct.Format("%m%d%Y%H%M%S");<br />
csFileName_var = csFileName; TestFile.Open(csFileName,CFile::modeCreate|CFile::modeWrite|CFile::typeBinary);<br />
TestFile.Write(Rx_Buf,3000000);<br />
TestFile.Close();<br />
<br />
TestFile.Open(csFileName_var,CFile::modeRead | CFile::typeBinary);<br />
TestFile.Read(&read,3000000);<br />
TestFile.Close();
i delete the two buffers at the end using delete operator.
I tried debugging but in vain. Do u have any suggestions?
|
|
|
|
|
Now you are doing the same mistake as before:
<br />
TestFile.Read(&read,3000000);<br />
Try this instead:
<br />
TestFile.Read(read,3000000);<br />
Alcohol. The cause of, and the solution to, all of life's problems - Homer Simpson
|
|
|
|
|
you are correct. It works.
Thanks for the help.
It has been a great learning experience. thanks for your time.
|
|
|
|
|
You are welcome. I'm glad to be able to help you out.
Alcohol. The cause of, and the solution to, all of life's problems - Homer Simpson
|
|
|
|
|
hey kakan,
iam trying to do the following which is the next stage in my code.
As you know, the data gets stored in a group of files. the files have to be called back or opened at a later stage to plot.
on selecting a checkbox, the default or the first file name (of say, 4 files) should be displayed in an edit bar. the edit bar goes active only on clicking the check box. the edit bar should be able to scroll through to find all the available files. the files are stored with .txt extension.
the file that gets selected gets plotted which is done by clicking another pushbutton "plot".
I cannot get the file names to be displayed in the edit bar. how do i do it?
Thanks in advance.
|
|
|
|
|
Hello again thathvamsi!
I'm sorry for the delay, but I have som work to do too...
I suggest you do like this (two alternatives).
1.
Show a normal file selection dialog and let the user select the file to plot.
When the user clicks the "OK" button, start plotting the file.
2.
Search for the interesting files and put their names in a listbox or combobox.
When the user double-clicks a text line, start plotting the selected file.
Alcohol. The cause of, and the solution to, all of life's problems - Homer Simpson
|
|
|
|
|
Hello again.
I came to think about a thing you might want to change.
Now, you are using the extension .txt for your files, and that's not the best extension to use, since .txt implies it's a text file.
And that's not true for a file filled with binary values.
I suggest you use another extension for your files, preferably something special for the plot data files, such as (for instance) .plt (for "plot") or .bin (for binary file).
If you let the user select file to plot, it will be easier to use a special extension. Then you can make a selection filter for that type of extension, and the user will only see the correct files. That will lessen the user mistakes, since the user has to select the correct file type.
Imagine what the plot would be like if the user selects an ordinary text file...
-- modified at 9:50 Friday 1st September, 2006
Alcohol. The cause of, and the solution to, all of life's problems - Homer Simpson
|
|
|
|
|
if u waana create a directory for ur file write purpose pls use the API CreateDirectory()
RinuRaj
|
|
|
|
|
I have been succesfull in reading data from a binary file using a console application, and using cout to view thedata, but for the life of me, I can't seem to figure out how to load the data into a listview...
f.read(reinterpret_cast<char*>(&Items),sizeof(int));
m_List.InsertItem(0, Items);
What must I do to get the "Items" data loaded into the listview?
Thanks
Jeff
|
|
|
|
|
Jumpin` Jeff wrote: m_List.InsertItem(0, Items);
this sets label for that item, Do you forgot to call SetItemText ?
|
|
|
|