|
You arose quite a discussion! Still, no answer to your original question I believe?
As far as I can see, your code should work just fine, but are you sure that, after changing your structure, you deleted the original file? Could it possibly be that you are reading information that was written with the old structure? In that case, anything could happen!
The problem here is that you simply write a block of data, assuming that it must be what you expect it to be. If you need to be more sure, also after changing the data's structure, you would need to write individual data elements, with some kind of indication as to thei meaning (something like ASN.1, but you could of course write your own simpler version).
If this is no problem to you, then what you are doing now should work. If you simultaneously write from one thread (or application) and read from the other, then the data you read might be corrupted! (imagine one thread being halfway writing a tm structure, while the other is reading it). The best way around this is using simple file-locking.
If you use the CFile method, this would be "CFile::LockRange ()" for example.
If you use the FILE * approach, you will have to use fsopen to open the file, using for example _SH_DENYWR to avoid someone else writing to the file.
Success,
William
|
|
|
|
|
Thanks willam,
Let me put it a bit more clearly,
The primary thread of my application or the main thread launches the second worker thread when it is initialized. the second thread runs in the background and after every 6 seconds looks for a file say "abcd.txt" where records are written. now records to this file are written from the primary thread. and this writing can take place at any moment the enduser wishes. say at a particular moment the secondary thread is reading the file and the enduser writes to it at that particular moment than the data read from the file will be corrupt. that is what my problem is. i don't want to deny the end user ,rather i would like to keep him waiting for a second or two and then once the read is complete, i would like to write into the file. and give him a success message.
The critical section point that james made above sounds nice. but if you have any other suggestion then please let me know.
One more question , is it a good idea, reading from a file every 6 seconds?
kss
|
|
|
|
|
My best suggestion would be to use locking. I don't know how big your file is going to be, but I assume that this will not be enormous? In that case, reading will only take some milliseconds, so your user will not even notice that he has been waiting for the read to complete.
On the other hand, locking is a two-way process: also your reading thread will have to wait for the write to complete. However, this too should take just milliseconds. Besindes, you could fine-tune all of this, by just locking the record that you are working with (the LockRange instruction allows you to specify a starting point and a size of the range you want to lock). This makes the chance of having to wait even smaller.
Success,
William
|
|
|
|
|
The file can contain up to 1000 such records. That makes it of 1.1 mb or 1.2 mb . I was looking at the MSDN and saw the CMemFile class can i use it in a Win32 Project ? is there any equivalent of LockRange win32 APIs ? or Do i have to use the CFile::LockRange ?
kss
|
|
|
|
|
In general, you do not delete items from an ImageList. An Imagelist associated with a ListView control usually does not contain one icon for each item, usually all of the images use one of a few different icons.
Think of it like Windows Explorer - most normal folders share the same image. Just because you remove the last folder in the view does not mean that the folder image gets deleted as well.
If you are trying to have a specific, unique icon for each ListView item, and you have lots of items, an ImageList is likely not the best way to go - I would look into using CustomDraw or virtual mode to draw the icons as needed.
Peace!
-=- James If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong! Avoid driving a vehicle taller than you and remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road! DeleteFXPFiles & CheckFavorites (Please rate this post!)
|
|
|
|
|
Hello !
I would like to print on the console (in a console application) the status of a calculation which runs in a separate thread. I would like in fact to put the evolution of the calculation (0 - 100 %). The problem is that I don't know if this is possible to write data always at the same position in the console (otherwise, this will look very ugly).
Thanks for the suggestions.
|
|
|
|
|
try gotoxy() or something
divin
|
|
|
|
|
|
there is a function to place the cursor at a specific (x ; y) couple -not GotoXY() which is borland specific), but i don't remember which.
what you can do is also to use the '\b' (backspace) character to go back on the characters you've just printed out...
printf("12.37");
printf("\b\b\b\b\b"
"14.17");
this will actually print the last 14.17 only...
you also have the possibility to use the '\r' line feed character to go back to the beginning of a same line...
TOXCCT >>> GEII power [toxcct][VisualCalc 2.24][3.0 soon...]
|
|
|
|
|
Easier; uise a carriage-return:
while (Loop)
{
... do something ....
printf ("12.12\r");
}
printf ("\n");
Don't forget the last line-feed, since otherwise the prompt will appear within this line after your process terminates.
Success,
William
|
|
|
|
|
yeah, i know, this...
but sometimes, you don't want to overwrite a whole line but only some characters...
TOXCCT >>> GEII power [toxcct][VisualCalc 2.24][3.0 soon...]
|
|
|
|
|
oh, i found it !
see if using this don't help you :
header : <wincon.h>
method : SetConsoleCursorPosition()
TOXCCT >>> GEII power [toxcct][VisualCalc 2.24][3.0 soon...]
|
|
|
|
|
Thanks to everybody !
Yes, of course I could have used the back character. I think this is the easiest solution.
|
|
|
|
|
I have a question about the printing process :
In my program, i want to make a button to print directly the window, without any dialog box. But when i call the OnFilePrint() function in my class CmyView, but it doesn't work.
How do I bypass this dialog box message ?
I have found this but it also doesn't work, the box is here again...
http://www.microsoft.com/msj/0197/c/c0197.aspx
-- modified at 5:55 Thursday 23rd February, 2006
|
|
|
|
|
|
It's exactly what I'm looking for, but I didn't think about the "bypass" word. Thanks a lot
-- modified at 4:50 Saturday 25th February, 2006
|
|
|
|
|
Hi all
I would like to get mac address from specical IP and use SendArp() of MSDN sample on my program(VC++.net).
It work on WinXp(SP2) but doen't work on Win2K(SP4).
It show error message "The procedure entry point freeaddrinfo could not be located in the dynamic library WS2_32.dll" , when I want to run on win2K
Do I neglect or need to note any key point ? Please provide any suggestion , Thanks a lot~
|
|
|
|
|
Dear all,
I have to build an application to read data from a smart card using C++ Builder. Is there anyone know any resources, tips, or sharing that could be useful?
I have seached in Google about PC/SC in C++, but there are not much information about it (especially in relation with C++ Builder).
Many thanks!
|
|
|
|
|
I'm writing 1D array class and want to overload operator () that is we can retrieve data from class and save it to it
class
{
...
long double *data;
long double &operator(int x)
{
return data[x];
}
...
}
like with this we can simply say
long double a;
a = vec(0);
a += 20;
vec(0) = a;
But how to overload it if I want to use different types of data in array
short *sdata;
float *fdata;
long double *ldata;
and for the overloaded operator always returning/writing to itself long double values casting them to apropriate type (short,float,...)
class vec(short);
long double a;
a = vec(0); //that's easy to always return long double
a += 10;
vec(0) = a; //cast it somehow in overloaded op?
And I dont want to use templates.
9ine
|
|
|
|
|
9ine wrote: a = vec(0);
As you can see the compiler can't undertand what return type that you want to use, it make a cast (if it is possible) between the return value and the type of a .
vec(0) don't helps!
but (here cames a solution)
vec (0) can give more informations:
is it possible for you to have different child classes? one for each data type? and then call the appropriate operator?
Have a nice code day
|
|
|
|
|
returning value from array is very simple
we code it as return type is always the same
long double operator()(int x)
{
switch(type)
case 1:
return (long double)cdata[x]; //if cdata is char
case 2:
return (long double)sdata[x]; //if sdata is short
..etc
}
the problem is that we need to code either a = vec(0) and vec(0) = a, where a is long double.
long double &operator()(int x)
{
//need to get a reference to type long double from all possible types as char,short,float,...
}
9ine
|
|
|
|
|
make a generic base class for that types you put in it (like CObject for instance), so that your subscript will be an instance of CObject. then, if you want to performs operations on these types, such as + , += , etc..., then each type/class that is supposed to be placed in the array must implement the operators...
the best you can do is providing virtual operators without implementation within the object base class...
one last thing. what' you're trying to achieve don't seem to be that good. what are you doing exactly ? and what for ?
TOXCCT >>> GEII power [toxcct][VisualCalc 2.24][3.0 soon...]
|
|
|
|
|
If all of you suggesting to use something like
Carray
Cchar : public Carray
Cshort : public CArray
Cfloat : public CArray
9ine
|
|
|
|
|
No, this is very important thing I'm trying to achieve.
I need to code for saving memory of loading large amount of ADC data.
This data can be of 8bit 16bit. Also I want to support not ADC data as floating numbers.
Due to the need of performing signal processing I want to be all the data converted to say double or long double to get more precision on arithmetic ops.
If we load ADC data which is 16bit quantized we convert it to double, same for 8bit data. And do some high presision calculation and plot results etc...
If our data sampled at 1000Hz 16bit for a 10hours we must allocate 36000*1000*sizeof(double) = 288Mb. But If we keep it as 16bit numbers converting to double and back at operator() which we need for saving also this will requier 36000*1000*sizeof(short) = 72Mb.
Great if this can be achieved with one class.
Another very good approach is to not load data from hdd at all keeping just FILE pointer, will slow than with memory, but reasonable if we got 2Gb of data that is it will go to swap file.
9ine
|
|
|
|
|
Hi,
why can't or won't you use templates
this could be a solution
codito ergo sum
|
|
|
|