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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
S

Sujan Dhakal

@Sujan Dhakal
About
Posts
9
Topics
3
Shares
0
Groups
0
Followers
0
Following
0

Posts

Recent Best Controversial

  • Writing Objects into files in binary mode
    S Sujan Dhakal

    so it was only the issue of std::string class, thanks i succeed with the following code.

    #include <iostream>
    #include <fstream>
    #include <cstring>
    using namespace std;

    class Data
    {
    public:
    char name[20];
    int id;
    public:
    Data(){}
    Data(char *n,int id){strcpy(name,n);this->id=id;}
    };

    int main() {

    Data objdata;
    //fstream file("c:\\\\test.txt",ios::out | ios::binary);
    fstream file("c:\\\\test.txt",ios::in | ios::binary);
    if(file.is\_open())
    {
    	//file.write((char\*)&objdata,sizeof(Data));
    
    	file.read((char\*)&objdata,sizeof(Data));
    	cout<<objdata.name<<objdata.id;
    	cout<<"Success";
    	file.close();
    }
    else
    {
    	cout<<"Error";
    }
    
    return 0;
    

    }

    C / C++ / MFC help ios

  • Writing Objects into files in binary mode
    S Sujan Dhakal

    i'm writing a simple program using Ansi C++. I'm not using Microsoft C++. that doesnot applies to me. what's your suggestion?

    C / C++ / MFC help ios

  • Javascript Menu inside <table> tag</table>
    S Sujan Dhakal

    your question is not so clear. provide some code so that it can be answered.

    Linux, Apache, MySQL, PHP question javascript

  • Writing Objects into files in binary mode
    S Sujan Dhakal

    Writing Object Succeeds with the following code

    #include <iostream>
    #include <fstream>
    using namespace std;

    class Data
    {
    public:
    string name;
    int id;
    public:
    Data(){}
    Data(string n,int id){name=n;this->id=id;}
    };

    int main() {

    Data objdata("sujan dhakal",1);
    fstream file("c:\\\\test.txt",ios::out | ios::binary);
    if(file.is\_open())
    {
    	file.write((char\*)&objdata,sizeof(Data));
    	cout<<"Success";
    	file.close();
    }
    else
    {
    	cout<<"Error";
    }
    
    return 0;
    

    }

    but why reading not working #include <iostream> #include <fstream> using namespace std; class Data { public: string name; int id; public: Data(){} Data(string n,int id){name=n;this->id=id;} }; int main() { Data objdata; fstream file("c:\\test.txt",ios::in | ios::binary); if(file.is_open()) { file.read((char*)&objdata,sizeof(Data)); cout<<objdata.name<<objdata.id<<endl; cout<<"Success"; file.close(); } else { cout<<"Error"; } return 0; } help me

    C / C++ / MFC help ios

  • Troublesome Handling of Files in C++
    S Sujan Dhakal

    #include <iostream> #include <fstream> using namespace std; int main() { char data[5];//={'a','b','c','f','\0'}; //fstream file("c:\\test.txt",ios::out | ios::binary); fstream file("c:\\test.txt",ios::in | ios::binary); if(file.is_open()) { //file.write(data,sizeof(data)); while(!file.eof()) { file.read(data,sizeof(data)); cout<<data; } file.close(); } else { cout<<"Error"; } return 0; } I've written "abcf" to text.txt file in binary mode. i'm reading back the data into the variable "data". why the heck the output is "abcfabcf". I should be "abcf" but what happened to this one. and another question do anybody knows how to write and read string data-type in binary mode. please help.

    C / C++ / MFC help c++ ios tutorial question

  • ios::in | ios::out error
    S Sujan Dhakal

    what's problem with this bit masking ios::in | ios::out | ios::binary since we have specified out in the mask, why the file is not being created. thanks for replying.

    C / C++ / MFC help ios

  • ios::in | ios::out error
    S Sujan Dhakal

    thanks for the answer but i dont want to use ofstream and ifstream class instead i want to use fstream class only. Is it possible? and i also need the file to be created as using ofstream.

    C / C++ / MFC help ios

  • ios::in | ios::out error
    S Sujan Dhakal

    no the file is expected to be created. in the following case also it outputs "Error" fstream file("c:\\test.txt",ios::out | ios::in); if(file.is_open()) { file<<"Hello"; file.close(); } else { cout<<"Error"; }

    C / C++ / MFC help ios

  • ios::in | ios::out error
    S Sujan Dhakal

    #include <iostream> #include <fstream> using namespace std; int main() { fstream file("c:\\test.txt",ios::in | ios::out | ios::binary); if(file.is_open()) { file<<"Hello"; file.close(); } else { cout<<"Error"; } return 0; } I wonder why this code is giving me the output "Error" unexpectedly...what went wrong in this code. I'm not able to write texts in file..please help

    C / C++ / MFC help ios
  • Login

  • Don't have an account? Register

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