#3
by かずま » 8年前
読み込みのことを考えるとバイナリファイルではなく、
テキストファイルにしたほうが良いのではありませんか?
コード:
#include <iostream> // cout
#include <fstream> // ofstream, ifstream
#include <sstream> // istringstream
#include <vector> //
#include <string> // getline
using namespace std;
void set(vector<vector<string> >& vvs, string *s, int n)
{
vector<string> vs;
for (int i = 0; i < n; i++)
vs.push_back(s[i]);
vvs.push_back(vs);
}
void print(vector<vector<string> >& vvs)
{
for (int i = 0; i < vvs.size(); i++) {
for (int j = 0; j < vvs[i].size(); j++) {
if (j != 0) cout << ", ";
cout << vvs[i][j];
}
cout << '\n';
}
}
int output(const char *name, vector<vector<string> >& vvs)
{
ofstream ofs(name);
if (!ofs) return 1;
for (int i = 0; i < vvs.size(); i++) {
for (int j = 0; j < vvs[i].size(); j++) {
if (j != 0) ofs << '\t';
ofs << vvs[i][j];
}
ofs << '\n';
}
return 0;
}
int input(const char *name, vector<vector<string> >& vvs)
{
ifstream ifs(name);
if (!ifs) return 1;
string line, field;
while (getline(ifs, line)) {
istringstream iss(line);
vector<string> vs;
while (getline(iss, field, '\t'))
vs.push_back(field);
vvs.push_back(vs);
}
return 0;
}
int main()
{
string a[3][4] = {
{ "abc", "def", "ghi", "jkl" },
{ "mno", "pqr" },
{ "123", "456", "789" },
};
vector<vector<string> > vvs;
set(vvs, a[0], 4);
set(vvs, a[1], 2);
set(vvs, a[2], 3);
cout << "\n--- vss ---\n";
print(vvs);
if (output("test.dat", vvs)) return 1;
vector<vector<string> > vvs2;
if (input("test.dat", vvs2)) return 1;
cout << "\n--- vss2 ---\n";
print(vvs2);
return 0;
}
参考になりますか?
読み込みのことを考えるとバイナリファイルではなく、
テキストファイルにしたほうが良いのではありませんか?
[code]
#include <iostream> // cout
#include <fstream> // ofstream, ifstream
#include <sstream> // istringstream
#include <vector> //
#include <string> // getline
using namespace std;
void set(vector<vector<string> >& vvs, string *s, int n)
{
vector<string> vs;
for (int i = 0; i < n; i++)
vs.push_back(s[i]);
vvs.push_back(vs);
}
void print(vector<vector<string> >& vvs)
{
for (int i = 0; i < vvs.size(); i++) {
for (int j = 0; j < vvs[i].size(); j++) {
if (j != 0) cout << ", ";
cout << vvs[i][j];
}
cout << '\n';
}
}
int output(const char *name, vector<vector<string> >& vvs)
{
ofstream ofs(name);
if (!ofs) return 1;
for (int i = 0; i < vvs.size(); i++) {
for (int j = 0; j < vvs[i].size(); j++) {
if (j != 0) ofs << '\t';
ofs << vvs[i][j];
}
ofs << '\n';
}
return 0;
}
int input(const char *name, vector<vector<string> >& vvs)
{
ifstream ifs(name);
if (!ifs) return 1;
string line, field;
while (getline(ifs, line)) {
istringstream iss(line);
vector<string> vs;
while (getline(iss, field, '\t'))
vs.push_back(field);
vvs.push_back(vs);
}
return 0;
}
int main()
{
string a[3][4] = {
{ "abc", "def", "ghi", "jkl" },
{ "mno", "pqr" },
{ "123", "456", "789" },
};
vector<vector<string> > vvs;
set(vvs, a[0], 4);
set(vvs, a[1], 2);
set(vvs, a[2], 3);
cout << "\n--- vss ---\n";
print(vvs);
if (output("test.dat", vvs)) return 1;
vector<vector<string> > vvs2;
if (input("test.dat", vvs2)) return 1;
cout << "\n--- vss2 ---\n";
print(vvs2);
return 0;
}
[/code]
参考になりますか?