c++을 이용하여 csv 파일 다루기.. 완전 초보입니다.

eco3165의 이미지

제가 이번에 c++을 이용해서 csv 파일을 읽어들이고, 원하는 부분만 출력하는 코딩을 짜고 있는데요..
여기 질문 중에 파일을 읽는 코드를 누가 올려놨더라구요. 근데 제가 아직은 c++에 대한 숙련도가 낮고 이 코딩은 빠른 시일내에 해야 해서
코드에 대한 간단한(?) 설명을 조금 듣고 싶습니다. 여기에 더해 아래 코드를 돌리게 되면 첫번째 열의 숫자값을 비교하여 정렬을 하게 되는데
여기서 문제(1과 10, 2와 20, 3과 30을 같은 값으로 본다는 문제가 생깁니다.) Compare 함수 부분을 수정하면 될거 같긴한데..

무튼 아래 코드에 대한 주석이나 설명을 해주실 수 있다면 귀찮겠지만 한번만 해주시면 너무 감사하겠습니다ㅠㅠ
답답하시겠지만 기초적인것도 짧게라도 설명 해주시면 감사하겠습니다.

마지막으로 out.csv파일에 정렬해서 저장한 값을 행렬로 표현한다면, 그 행렬의 3열의 라인(예를 들면 csv 데이터 중 원하는 라인 값을 불러오는)
것도 행렬의 개념으로 코딩을 하면 뽑아낼 수 있는지 궁금합니다.

답변 달리기 전까지 저도 공부 열심히 하고 있을테니까 지나가는 선배님들 도움을 좀 주십쇼.

———————————————————————————————————————————————

#include
#include
#include
#include
#include
#include
using namespace std;

// class for sort
class sort_class
{
private:
string string1, string2;

public:
sort_class(const string& init_string1, const string& init_string2);

friend int Compare_By_string1(const sort_class& lhs, const sort_class& rhs);
friend ostream& operator<<(ostream& os, const sort_class& obj)
{ os<<obj.string1<<’,’<<obj.string2; return os;
}
};

sort_class::sort_class(const string& init_string1, const string& init_string2)
:string1(init_string1), string2(init_string2){}

// function that sorts by string1
int Compare_By_string1(const sort_class& lhs, const sort_class& rhs)
{ return (lhs.string1<rhs.string1);
}

int main()
{
// read test.csv file and put them into the test_vector
ifstream file(“ATFSwathData_1[AOITrack].csv”);
vector test_vector;

string string1, string2;
while(getline(file, string1,’,’)
&&getline(file,string2,’\n’))
{ test_vector.push_back(sort_class(string1,string2));
}
file.close();

// sort the test_vector by string1
sort(test_vector.begin(), test_vector.end(),Compare_By_string1);

// write the sorted test_vector on out.csv file
ofstream fout(“out.csv”);
std::ostream_iterator output_iterator(fout,”\n”);
std::copy(test_vector.begin(),test_vector.end(),output_iterator);
std::copy(test_vector.begin(), test_vector.end(), std::ostream_iterator(std::cout, “\n”));
fout.close();

// read out.csv and then print out.csv on the screen
string line;
ifstream out (“out.csv”);
if(out.is_open())
{ while(getline(out,line)) cout<<line<<endl;
}
out.close();
return 0;
}

Anti-Lock의 이미지

비교 함수에서 문자열을 비교하고 있는데,
숫자로 변환해서 비교하면 원하시는 대로 될것 같습니다.
원하는 행의 출력은,
각 행을 벡터나 리스트에 넣고 나면 원하는 것을 출력할 수 있을것 같네요.

댓글 달기

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
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.