write함수에 관해서 질문드립니다.

blugon97의 이미지

예를 들어
typedef struct
{
unsigned short a;
unsigned int b;
char c[100];
} msg_t;

msg_t msg = {1, 2, "3"};

이런 구조체를 write함수를 써서 파일에 쓴다음 read를 써서 파일에서 읽어드
려 msg_t 타입의 구조체에 저장하는게 가능한가요?? 어떤 프로토콜에 쓰이는 메시지를 정의하기 위해서 필요하거든요. 다른 방법이 없어서 임시적으로 sprintf를 써서 char 배열에 저장한뒤 write, read 배열을 확인해보니 "123" 이라는 값이 들어가 있습니다. 저는 메모리에 short 2바이트 int 4바이트 char ?바이트 이런식으로 잡히길 원했는데 그냥 char단위로 "123" 3바이트가 잡혔습니다. 이런 결과를 원한게 아니었거든요. 조언 부탁합니다.

fliers의 이미지

왜 sprintf 를 사용하죠?

fwrite ((void*)&msg, sizeof(msg_t), 1, fp); fread ((void*)&msg, sizeof(msg_t), 1, fp);

음, 물론 그냥 write, read 로도 가능은 하고요..

그리고 구조체가 메모리에 잡히는게
4바이트(32비트겠죠?) 단위에서 끊어지던데
실제 구조체 멤버별 크기 말고 sizeof로 통째로 묶은 크기가 좀 더 큰..

무슨 옵션을 써서 낭비없이 사용하던 것 같은데 그거까진 모르겠네요..

lunarainbow의 이미지

fliers wrote:
그리고 구조체가 메모리에 잡히는게
4바이트(32비트겠죠?) 단위에서 끊어지던데
실제 구조체 멤버별 크기 말고 sizeof로 통째로 묶은 크기가 좀 더 큰..

무슨 옵션을 써서 낭비없이 사용하던 것 같은데 그거까진 모르겠네요..

어? 정말 이런 옵션이 있나요?

저는 단지 프로그래머가 알아서 '잘' 잡는 것으로만 해결 가능하다고 알고 있었는데..

예를 들어,

struct aaa{
char a;
int b;
char c;
};

이런 코드의 경우,

struct aaa{
char a;
char c;
int b;
};

이렇게 해서 말입니다.

혹시 fliers 님이 말씀하신 옵션... 아시는분 계시나요?

stoneshim의 이미지

우리 모두 리얼리스트가 되자. 그러나 가슴에 이룰 수 없는 꿈을 가지자

댓글 달기

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