C++, vector를 파일에 저장 질문드립니다

minyoung347의 이미지

안녕하세요 C++에서 벡터를 파일에 저장하려고 합니다

,로 구분되어진 N x 3 행렬 형식의 파일을 불러와서 벡터에 저장하고
이 벡터를 string1에 따라 sort한 후에 다른 파일에 저장하려고 합니다
N x 3 vector를 아래와 같은 형식으로 save.csv 파일에 저장하려면 어떻게 해야하나요?
a1, a2, a3
b1, b2, b3
c1, c2, c3
...

주석 처리한 부분과 같이 ofstream fout, ostream, copy를 사용해서 저장하려고 했는데 잘 되지않아서 질문드립니다,
답변 주시면 감사하겠습니다!

#include <iostream>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <vector>
#include <string.h>
#include <algorithm>
#include <iterator>
using namespace std;
 
class line
{
public:
line(const string& init_string1, const string& init_string2, const string& init_string3);
friend int Compare_By_string1(const line& lhs, const line& rhs);
 
private:
string string1, string2, string3;
};
 
line::line(const string& init_string1, const string& init_string2, const string& init_string3)
:string1(init_string1), string2(init_string2), string3(init_string3){}
 
int Compare_By_string1(const line& lhs, const line& rhs)
{
return (lhs.string1<rhs.string1);
}
 
int main()
{
ifstream file("example.csv");
vector<line> vector_line;
string string1, string2, string3;
 
while(getline(file,string1,',')
&&getline(file,string2,',')
&&getline(file,string3,'\n'))
{
vector_line.push_back(line(string1, string2, string3));
}
file.close();
 
sort(vector_line.begin(), vector_line.end(), Compare_By_string1);
 
/*
ofstream fout("save.csv");
ostream_iterator<vector_line> output_iterator(fout,"\n");
copy(vector_line.begin(), vector_line.end(), output_iterator);
*/
 
return 0;
}

example.csv 파일은
a3, b3, c3
a2, b2, c2
a1, b1, c1
a4, b4, c4
와 같이 구성되어 있고
이 파일을 sort 한 후에
a1, b1, c1
a2, b2, c2
a3, b3, c3
a4, b4, c4
를 save.csv파일에 저장하려고 합니다

hano881112의 이미지

copy에서 내부적으로 ostream_iterator의 operator

일단 ostream_iterator 템플릿에 전달해주는 건 line 클래스가 되어야겠구요.

ostream_iterator&ltline> output_iterator(fout,"\n");

ostream_iterator의 operator를 직접 재정의해줄 순 없으니, 전역 operator를 friend로 지정하여 line 내부에서 선언하도록 하겠습니다.

class line
{
public:
	line(const string& init_string1, const string& init_string2, const string& init_string3);
	friend int Compare_By_string1(const line& lhs, const line& rhs);

	friend ostream& operator

그럼 copy 동작이 무리없이 잘 이루어질 것입니다.

minyoung347의 이미지

hano님이 알려주신대로 바꾸니까 잘 돌아갑니다
공부를 더 해야겠군요.. 정말 감사합니다!!

익명 사용자의 이미지

파일 입출력이나,, 네트워크 송신할때는 데이터를 직렬화 해야해요.

데이터들을 직렬화 하는걸 찾아보세용

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.