바이너리로 읽고 쓰기

freezm7의 이미지

아직도 초보를 못 벗어나네요. 간단히 파일 복사 프로그램을 C++ 로 짜려고 하는데요, 컴파일은 되는데, 실행하면 원본 파일과 복제된 파일의 크기와 내용이 다릅니다. 무엇을 놓치고 있는 것일까요?

텍스트 파일을 이 프로그램으로 복사해보니 \n 문자가 모두 빠진채, 한줄로 적히네요.

cygwin + gcc 입니다.

#include <iostream>
#include <fstream>

using namespace std;

int main(int argc, char** argv)
{
	for (int i = 1; i < argc; i++)	
	{
		char* filename = argv[i];

		ifstream ifile(filename, ios_base::binary);

		char* ofilename = strcat(filename, ".bit");
		ofstream ofile(ofilename, ios_base::binary);

		char byte;
		while (ifile >> byte)
			ofile << byte;
	}
}
freezm7의 이미지

The C++ Standard Library : A Tutorial and Reference 711 페이지에서 찾았습니다.

istream >> noskipws;

가 필요하더군요.

한가지 이해가 가지 않는 것은,
ifstream ifile(filename, ios_base::binary);
에서 보다시피, 생성자에서 binary 임을 알려주었는데,
>> 연산자로 white space skip 을 하지말라고 해줘야 한다는게 이해가 가지 않는군요.

그렇담, ios_base::binary 가 무슨 차이를 만들어내는지...

잘 아시는 분 알려주시기 바랍니다.

즐겁게 살아 볼까나~*

doldori의 이미지

텍스트 모드와 이진 모드의 차이는 개행 문자를 나타내는 '\n'을 어떻게 처리할
것인지에 따라 구분됩니다. Windows에서 개행 문자는 0x0D + 0x0A이며
UNIX에서는 0x0A, Mac에서는 0x0D 등으로 시스템마다 나타내는 방법이
다른데, '\n'은 시스템에 관계 없이 개행 문자를 추상적으로 처리할 수 있도록
합니다. 예를 들어 Windows의 경우 텍스트 모드에서 파일에 0x0D + 0x0A가
있으면 이를 하나의 '\n'으로 변환하게 되는 것이죠. 파일에 쓸 때에도 '\n'을
쓰라고 하면 실제로 0x0D + 0x0A로 바꾸어 쓰게 됩니다.
반면 이진 모드에서는 이러한 변환이 없습니다. 이진 모드에서는 추상적인 '\n'이
더 이상 의미가 없으며 개행 문자는 0x0D와 0x0A의 값(문자가 아님)으로만
취급됩니다. 이는 이진 입출력이 데이터 호환성을 보장받지 못하게 하는
한 가지 원인이 됩니다.

파일을 복사하는 용도라면 '\n'의 변환이 일어나지 않는 이진 모드가 적당하며
<<나 >> 등의 formatted 입출력보다는 istream::read나 ostream::write의
unformatted 입출력이 많이 사용됩니다.

댓글 달기

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