Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Why does reading a string cause errors?

Why does reading a string cause errors?

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestioniostutorial
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    tpndtbk
    wrote on last edited by
    #1

    Hi all, I have the following read/write program. typedef struct { string s1; string s2; } Names; int main { fstream fout("data.dat", ios::in | ios::out | ios::binary); Names name; name.s1.assign("abc"); name.s2.assign("xyz"); fout.write((char*)name, sizeof(Names)); fout.close } And then, I create another program to read this file int main { fstream fin("data.dat", ios::in | ios::binary); Names *name = new Names(); fin.read((char*)name, sizeof(Names)); // --> Till here, it's Ok. But if I want to access the struct. For example: cout << name->s1.c_str() << endl; fout.close } The strange thing is that, if I combine write and read file in one program, then no problem. But if I creat2 2 differenct programs, there's problems. Can anyone help me out? THank you in advance

    B 1 Reply Last reply
    0
    • T tpndtbk

      Hi all, I have the following read/write program. typedef struct { string s1; string s2; } Names; int main { fstream fout("data.dat", ios::in | ios::out | ios::binary); Names name; name.s1.assign("abc"); name.s2.assign("xyz"); fout.write((char*)name, sizeof(Names)); fout.close } And then, I create another program to read this file int main { fstream fin("data.dat", ios::in | ios::binary); Names *name = new Names(); fin.read((char*)name, sizeof(Names)); // --> Till here, it's Ok. But if I want to access the struct. For example: cout << name->s1.c_str() << endl; fout.close } The strange thing is that, if I combine write and read file in one program, then no problem. But if I creat2 2 differenct programs, there's problems. Can anyone help me out? THank you in advance

      B Offline
      B Offline
      Budric B
      wrote on last edited by
      #2

      int main { fstream fout("data.dat", ios::in | ios::out | ios::binary); Names name; <--- not a pointer name.s1.assign("abc"); name.s2.assign("xyz"); fout.write((char*)name, sizeof(Names)); <--- never thought you can cast fout.close a structure to a pointer to } char. Either this works for weird reasons, or you made a mistake copying (char *)&name.

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups