C++로 파일을 읽어서 row, column 갯수를 알아내는 것에 관한 질문입니다.

aeronova의 이미지

안녕하세요,

제가 하려는 것은 간단한 matrix 형식의 파일을 읽어서 전체 행과 열의 갯수를 구하려고 하는 것입니다.
행과 열의 갯수를 알려고 하니 입력 파일을 2번 읽어야했고, 그래서 파일 포인터 위치를 각각의 경우 파일의 맨 처음으로 돌려서 처리했습니다. 또 clear()를 사용하여 파일 상태 정보도 모두 제거해서 혹시 모를 파일 읽기와 관한 오류를 없애려고 했습니다. 우여곡절끝에 대강 만든 후 다시 abstract class를 이용해서 다시 디자인하는 도중 이전에 되었던 행과 열을 구하는 부분이 작동하지 않음을 알았습니다. Derived Class에서 단순히 파일을 open만 했는데도 open 이후에 파일 포인터 위치를 찍어보니 -1이더군요. (이건 end of file을 가르키지 않나요?)
일단 문제되는 부분만 따로 정리해서 올렸습니다. 고수님들 조언 부탁드리겠습니다.

p.s.
zip 파일이 올라가지 않아서 확장자를 임의로 png로 바꾸었습니다. 확장자만 다시 zip으로 바꾸셔서 풀면 됩니다.

File attachments: 
첨부파일 크기
Image icon testcode.png3.81 KB
doldori의 이미지

음... 간단한 일 같은데 좀 어렵게 푸시는 듯합니다.
파일의 내용을 행렬 형태로 저장하고 싶다면 vector의 vector를 쓰면 됩니다.

ifstream in("all.dat");
if (!in) exit(1);
 
typedef vector<string> Row;
typedef vector<Row>    Matrix;
 
Matrix matrix;
string line;
while (getline(in, line))
{
    istringstream ss(line);
    matrix.push_back(Row(istream_iterator<string>(ss),
                         istream_iterator<string>()));
}

행/열의 개수는 vector가 갖고 있으니 따로 _numRow, _numCol 같은 멤버는 필요가 없고요.

Quote:
Derived Class에서 단순히 파일을 open만 했는데도 open 이후에 파일 포인터 위치를 찍어보니 -1이더군요. (이건 end of file을 가르키지 않나요?)

AbstractClass::AbstractClass() : 
    _delimiters(" \f\n\r\t\v"), _inFile(NULL), _numRow(0), _numCol(0) {
}

문제는 _inFile(NULL)입니다. 파일 이름에 널 포인터를 주는 것은 fopen(NULL, NULL)과
마찬가지로 그 결과가 정의되지 않습니다. 맞게 고치려면 초기화 리스트에서 빼든지 초기치를
명시하고 싶다면 _inFile()과 같이 default construction을 하십시오.
aeronova의 이미지

도움 감사합니다. istringstream라는 것이 있었네요. 덕분에 좋은 것을 배웠습니다. :)

It's better to burn out than to fade away. -- Kurt Cobain.

댓글 달기

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