|
dear all
how to create 10 3x4 random value arrays with ofstream?for example.. double a[3][4] = {{(double)rand()/RAND_MAX, (double)rand()/RAND_MAX, (double)rand()/RAND_MAX,( double)rand()/RAND_MAX},{(double)rand()/RAND_MAX,(double)rand()/RAND_MAX,(double)rand()/RAND_MAX,(double)rand()/RAND_MAX},{(double)rand()/RAND_MAX,(double)rand()/RAND_MAX,(double)rand()/RAND_MAX,(double)rand()/RAND_MAX}};
and save these 10 arrays as certain name..thanks
Li Zhiyuan
5/10/2006
|
|
|
|
|
I think do you mean array serialization. I.e. taht you need to
(1) Create an array or (some arrays) of values, inititalise it.
(2) Store the array values to disk
(3) (Later, maybe another application) retrieve the array values from disk to initialise a newly created array.
I give you a minimal example (error handling left to the reader):
int a[]={0,1,10,8,7};
ofstream ofs;
int i, count;
ofs.open("array.bin", ios::binary);
count = sizeof(a)/sizeof(a[0]);
ofs.write( (char*)&count, sizeof(count));
for (i=0; i<count; i++)
{
ofs.write( (char*) &a[i], sizeof(a[i]));
}
ofs.close();
int * pArray;
ifstream ifs;
int i, count;
ifs.open("array.bin", ios::binary);
ifs.read( (char*)&count, sizeof(count));
pArray = new int[count];
for (i=0; i<count; i++)
{
ifs.read( (char*) &(pArray[i]), sizeof(pArray[i]));
}
ifs.close();
delete [] pArray;
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
[my articles]
|
|
|
|
|
dear friend
i can't understand your code,can you give me detail about step 2 and step 3, thanks a lot,
by the way, how to create 10 arrays by struct.....thanks
Li Zhiyuan
5/10/2006
|
|
|
|
|
Step 2:
The array must be stored on disk (using ostream) in order to retrieve it later.
The routine first stores the element count of the array then every element.
What is you difficult about?
Step 3:
The routine reads the array content from disk and builds up in memory an equivalent array.
First of all it reads the array element count from the file, then build dynamically (using new operator)an (empty) array having such element count, and finally reads the element values from disk and assigns such values to the array items.
What is your difficult about?
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
[my articles]
|
|
|
|
|
how to understand the following code?
for (i=0; i{ ofs.write( (char*) &a[i], sizeof(a[i]));}
Li Zhiyuan
5/10/2006
|
|
|
|
|
Sorry were typos on formatting, now I have fixed them, hence you can read again the OP code.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
[my articles]
|
|
|
|
|
already solved my problem, but i can't understand the following code:
count = sizeof(a)/sizeof(a[0][0]);
ofs.write( (char*)&count, sizeof(count));
Li Zhiyuan
5/10/2006
|
|
|
|
|
dear friend
i would like to create 10 3x4 arrays,how to create? pls give me detail, thanks a lot.
Li Zhiyuan
5/10/2006
|
|
|
|
|
actually it was:
count = sizeof(a)/sizeof(a[0]);
ofs.write( (char*)&count, sizeof(count));
The first line computes the element count of the array (you can also use the hard-wired count=5; , since we know in advance that array a has five elements).
The last line writes the element count to the file. Here you can use also
ofs.write( (char*)&count, sizeof(int));
Still troubles?
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
[my articles]
|
|
|
|
|
dear friend
i can understnad this, how to create 10 3x4 arrays using this way, i can't finish it, thanks a lot.
so urgent!!!!!!!!
Li Zhiyuan
5/10/2006
|
|
|
|
|
dear friends
any one has write() and read() specification. thanks...
Li Zhiyuan
5/10/2006
|
|
|
|
|
li zhiyuan wrote: any one has write() and read() specification. thanks...
MSDN has: write [^], read [^].
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
[my articles]
|
|
|
|
|
That code is exceptionally badly formatted! I can't even be bothered looking at it.
Steve
|
|
|
|
|
This is homework related stuff:
double dHomeworkArray[10][3][4];
//Meta code
for(...10)
for(...3)
for(...4)
dHomeworkArray[10][3][4]=(double)rand()/RAND_MAX;
to save write the complete array to disk
fwrite( &dHomeworkArray, sizeof( double ) * 10 *3 *4);
Greetings from Germany
|
|
|
|
|
thanks, but i tried to debug it, it can't initialize dHomeworkArray....what happened? thanks
the following is code what i have done and debugged.
double dHomeworkArray[10][3][4];
//Meta code
for(int i = 0;i<10; i++)
dHomeworkArray[10][3][4]=(double)rand()/RAND_MAX;
{
for(int j=0;j<3; j++)
{
for(int k=0;k<4;k++)
cout << " " << setprecision(6)<<dHomeworkArray[i][j][0]<<"<<" " <<dHomeworkArray[i][j][1]<<" "<<dHomeworkArray[i][j][2]<<" "<<dHomeworkArray[i][j][3]<<endl<<endl;
}
Li Zhiyuan
5/10/2006
|
|
|
|
|
HI ,
I have coded a C++ project which enhances a C makfiile project
The orignal Solution had only one project
Anyway I am using some of the symbols/sturctures of the C makefile project
So... IN the C makfile project I __declspec(dllexport) those symbols/structures and the C makfile project genarates in .obj directory obj's and .lib('s)
Under Tools->;options VC++ directories I add the .obj directory
where the LIBRARY tab containing the exported symbols
In the the second project I __declspec(dllimport) the symbols/structures
However the First project builds fine
While in the Second I get linker errors unresolved __declspec(dllimport)
Thankx
|
|
|
|
|
Does the linker find the lib? Is it in the build path?
Greetings from Germany
|
|
|
|
|
Hi friends, troubling you yet again...
I want to communicate with serial port (COM1).
I want to send/receive some data through it.
I have read earlier post, & also found a project on serial port.
But i m not able to understand anything of that.
I simply want to brief steps about
->opening a port
->reading a port
->write a port
Thanking you in advance
|
|
|
|
|
tina- wrote: ->opening a port
CreateFile or (CreateFileEx ) function, if you have a look at MSDN configuration sample [^], you can easily figure out the usage.
tina- wrote: ->reading a port
->write a port
ReadFile and WriteFile : the usage in not-overlapped mode is not such difficult, but perhaps you need to go multithreading.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
[my articles]
|
|
|
|
|
CPallini wrote: CreateFile or (CreateFileEx)
thanx friend.
the project i downloaded (abt serial comm) used CreateFile, but wat do u think will be the first parameter (lpFileName)?
will it be COM1
|
|
|
|
|
It must be _T("COM1") for serial port 1.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
[my articles]
|
|
|
|
|
How can we know if a data has been received at the serial port ?
is it required to check the port continuously, using ReadFile(), & if it return anything than 0, then we conclude that a data is received ?
|
|
|
|
|
Yes, if you use not-overlapped I/O. This way the application probably needs an additional thread always waiting for incoming data on the serial line.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
[my articles]
|
|
|
|
|
It depends of the timeouts you specified on the comm port. Basically, you can configure your COM port so that it never waits for data (ReadFile returns immediately) or that it blocks forever until some characters have been received, or something in the middle. Check the SetCommTimeouts[^] function.
Anyway working with the serial port is not something easy for a beginner. So expect some frustration because nothing works as expected. But we're to help if needed .
modified on Monday, January 21, 2008 2:37:11 AM
|
|
|
|
|
Try using CMSCommCtrl class . for this you have to add MSCOMM32.ocx control activex control to your project.
|
|
|
|