|
You may use:
(1) a text format for a human readable file.
(2) a binary format for later retrieval of array values.
Regarding the API , you have a lot of options: the std::ofstream class methods or standard C functions (fopen, fwrite, fclose ), the Win32 API (like CreateFile, WriteFile, CloseHandle functions) or the MFC classes (like CFile , CStdioFile ), ...
Moreover, I think it is time (considering all your previous posts) to have a look at MFC serialization [^] topic.
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
how to understand the following code:
ofs.write( (char*)&count, sizeof(count));that means char*;
how to save using std::ofstream? thanks a lot,...urgent..
second question is that can you provide me the stuff: for example: %d,if%, and so forth..i read them before, but so far i couldn't find these matierials, thanks a lot.
Li Zhiyuan
5/10/2006
|
|
|
|
|
To write some data to the file, the ostream object needs to know
(1) the address of the data.
(2) the size of the data.
(1) Well, &count means exactly the address of the variable count (as int pointer) and (char *) is a cast of the int pointer to a char one (ostream works on byte basis and it requires a char pointer, a char is 1-byte long, after all...).
(2) sizeof(count) is the size of count , i.e. the size of an integer variable (i.e. 4 bytes).
li zhiyuan wrote: second question is that can you provide me the stuff: for example: %d,if%, and so forth
%d, %f are format specifiers for the printf (as well for fprintf , fscanf functions): whenever you need to print a particular type using such function(s) then you have to use the proper format specifier, for instance
int i= 100;
float f = 3.14159;
printf("An integer, i=%d, a float f=%f\n", i, f);
Full documentation can be found here http://msdn2.microsoft.com/en-us/library/hf4y5e3w(VS.71).aspx[^]
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
thank u very much.
how to create 10 3x4 arrays, late when i wanna use anyone of them, i can invoke it, i tried two days already, could not solve this problem,..really urgent now...thanks a lot..
how can i find full explanation of format specifier: &d,%1f........i need to read recall them..forgot it already..thanks a lot
Li Zhiyuan
5/10/2006
|
|
|
|
|
Actually, in a previous reply, I showed exactly all the necessary steps, using ofstream and ifstream , to perform the task on a simple array. I think you can easily figure out how to extend the posted code to fulfill your needs.
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
where can i find ofs.write() and ofs.read() function explanation? thanks
Li Zhiyuan
5/10/2006
|
|
|
|
|
|
dear brother
i tried many times i still couldn't finish this point, could you help me to provide detail of this. how to write 10 3x4 random value arrays, thanks a lot..i am really worried now.....i couldn't extend from your previious code
Li Zhiyuan
5/10/2006
|
|
|
|
|
But do you need to write in a human readable format (i.e. such that you could later open the file with the Notepad and see the numbers) or you need just to store the numbers for later retrieval (and re-construction of the array)?
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]
|
|
|
|
|
no need, just create 10 3x4 arrays, and in later my program, i need to use anyone of them...that is all, thanks
Li Zhiyuan
5/10/2006
|
|
|
|
|
If I remember well, you have arrays of doubles.
I give you a really crude sample code working.
(1) Create 10 3x4 arrays, initialise them with random values, i.e.
int i, j, k;
double a[10][3][4];
for (i=0; i<10; i++)
for (j=0; j<3; j++)
for (k=0; k<4; k++)
a[i][j][k] = (double)rand()/RAND_MAX;
(2) Store all of the elements of all the arrays inside a file, explicitely
exploiting our knowledge of array size (really poorly coding, indeed).
FILE * fp = fopen("arrays.bin","wb");
fwrite(a, sizeof(double), 10 * 3 * 4, fp);
fclose(fp);
(3) Retrieve all of the array values from file content to buil the new 10 3x4 arrays.
double b[10][3][4];
FILE * fp = fopen("arrays.bin","rb");
fread(b, sizeof(double), 10 * 3 * 4, fp);
fclose(fp);
(3) bis, retrieve the 7-th array from the file content
double c[3][4];
FILE * fp = fopen("arrays.bin","rb");
fseek(fp, sizeof(double) * 6 * 4 * 3, SEEK_SET);
fread(c, sizeof(double), 3 * 4, fp);
fclose(fp);
hope that helps.
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]
|
|
|
|
|
thanks a lot brother, how can i see a[5][3][4] array?
int i, j, k;
double a[10][3][4];
for (i=0; i<10; i++)
{
for (j=0; j<3; j++)
{
for (k=0; k<4; k++)
a[i][j][k] = (double)rand()/RAND_MAX;
cout <<" "<<a[i][j][0]<<" "<<a[i][j][1]<<" "<<a[i][j][2]<<" "<<a[i][j][3];
cout << endl;
}
}
but however i couldn't get solution, for example,
a[5]
display 3x4 array
like this.
Li Zhiyuan
5/10/2006
|
|
|
|
|
li zhiyuan wrote: how can i see a[5][3][4] array?
please elaborate.
li zhiyuan wrote: int i, j, k;
double a[10][3][4];
for (i=0; i<10; i++)
{
for (j=0; j<3; j++)
{
for (k=0; k<4; k++)
a[i][j][k] = (double)rand()/RAND_MAX;
cout <<" "<<a[i][j][0]<<" "<<a[i][j][1]<<" "<<a[i][j][2]<<" "<<a[i][j][3];
cout << endl;
}
}
but however i couldn't get solution, for example,
a[5]
display 3x4 array
Your code displays all of the a elements. What do you need instead?
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]
|
|
|
|
|
li zhiyuan wrote: how to create 10 3x4 arrays...
Creating arrays and printing them are two different things. Which one is troubling you?
li zhiyuan wrote: how can i find full explanation of format specifier: &d,%1f........i need to read recall them..forgot it already..thanks a lot
CPallini already provided you with a link. Did you not consult it?
"Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
With std::transform , how to transform the stream buffer of an std::fstream object to lower case? Thanks!
The below code gets error C2248.
std::fstream file("data.txt", std::ios::in);
std::istream_iterator<std::fstream> p(file);
std::istream_iterator<std::fstream> pE;
std::transform(p, pE, p, ::tolower);
Maxwell Chen
|
|
|
|
|
I found the solution!
std::fstream file("data.txt", std::ios::in);
std::string s(std::istreambuf_iterator<char>(file.rdbuf()), std::istreambuf_iterator<char>());
std::transform(s.begin(), s.end(), s.begin(), ::tolower);
Maxwell Chen
|
|
|
|
|
This worked for me:
#include "stdafx.h"
#include <iostream>
#include <iterator>
#include <algorithm>
int main(int argc, char* argv[])
{
using namespace std;
cin >> noskipws;
istream_iterator<char> isi(cin);
istream_iterator<char> isi_end;
ostream_iterator<char> osi(cout);
transform(isi, isi_end, osi, ::tolower);
return 0;
}
Steve
|
|
|
|
|
Stephen Hewitt wrote:
cin >> noskipws;
istream_iterator<char> isi(cin);
istream_iterator<char> isi_end;
ostream_iterator<char> osi(cout);
transform(isi, isi_end, osi, ::tolower);
But when I replace cin and cout with a single fstream object, it won't work.
istream_iterator<char> isi(file);
istream_iterator<char> isi_end;
transform(isi, isi_end, isi, ::tolower);
Maxwell Chen
|
|
|
|
|
The third argument should be osi , not isi : isi is an input iterator.
Steve
|
|
|
|
|
Stephen Hewitt wrote: The third argument should be osi, not isi: isi is an input iterator.
But then it is not working. I am not sure if any mistake below...
ABC DEF
using namespace std;
fstream file("data.txt", ios::in);
file >> noskipws;
istream_iterator<char> isi(file);
istream_iterator<char> isi_end;
ostream_iterator<char> osi(file);
transform(isi, isi_end, osi, ::tolower);
file.clear();
file.seekg(0, file.beg);
string s;
while(!file.eof()) {
getline(file, s);
cout << s << end;
}
Output does not have any change though.
ABC DEF
Maxwell Chen
|
|
|
|
|
Try something like this then:
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
#include <iterator>
#include <algorithm>
int main(int argc, char* argv[])
{
using namespace std;
ifstream ifs("C:\\Data.txt");
if (!ifs)
{
cerr << "Failed to open data file!" << endl;
return 1;
}
istream_iterator<char> isi(ifs);
istream_iterator<char> isi_end;
string s;
transform(isi, isi_end, back_inserter(s), ::tolower);
cout << s << endl;
return 0;
}
NOTE: I couldn't test this as MSVC6's std::string class is missing the push_back member function. Section 21.3.5 of the C++ standard says it should exist.
Steve
|
|
|
|
|
Stephen Hewitt wrote: transform(isi, isi_end, back_inserter(s), ::tolower);
Then this solution is very much similar to my previous one (URL[^]). Both of them copy the file buffer to a string. The only difference is that your solution requires to do file >> noskipws; in addition because file.rdbuf() keeps the spaces and the newlines whereas another way does not.
Maxwell Chen
|
|
|
|
|
Hi all,
May this question can be odd to you, if so I'm really sorry.
I've write a simple server application which is used to make a chat window. Actually there is two chat windows on the same HTML page. Whatever I type on one window is displayed on the other. Now I want to automatically run the server, without pressing F5 and start, at predefined time. I think it is clear to you.
One of my friend says that, using global mutex, I think it is the right word I can do that. But I don't know anything about it. Can you guys give me a help on it.
Thanks a lot...
I appreciate your help all the time...
Eranga
|
|
|
|
|
I have an std::map and I want to use std::for_each and boost::bind to call a method on the 'second' object in each map node. Anyone know the correct syntax?
class MyType
{
public:
void Method()
{
}
};
std::map<int, MyType> aMap;
this is what I can get right...
std::for_each(aMap.begin(), aMap.end(), boost::bind(&MyType::Method))
|
|
|
|
|
This works for me:
#include "stdafx.h"
#include <iostream>
#include <string>
#include <map>
#include <algorithm>
#include <functional>
#include <boost/bind.hpp>
using namespace std;
class MyType
{
public:
MyType(const string &name) : m_name(name) {}
void Method()
{
cout << m_name << endl;
}
private:
string m_name;
};
typedef std::map<int, MyType> Map_t;
int main(int argc, char* argv[])
{
Map_t m;
m.insert(Map_t::value_type(1, MyType("One")));
m.insert(Map_t::value_type(2, MyType("Two")));
m.insert(Map_t::value_type(3, MyType("Three")));
m.insert(Map_t::value_type(4, MyType("Four")));
m.insert(Map_t::value_type(5, MyType("Five")));
for_each(m.begin(), m.end(), boost::bind(&MyType::Method, boost::bind(&Map_t::value_type::second, _1)));
return 0;
}
Steve
|
|
|
|
|