Problem in reading stream value from a file

#include<fstream.h>
#include<conio.h>
#include<string>

class CLS
{
public : string name;
}c;


void main()
{
fstream f;
f.open("D:\\Text.txt", ios::out);

cin>>c.name;
cout<<"\n\nEntered Name: "<<c.name;
cout<<"\nSize : "<<c.name.size();
f.write((char*)&c, sizeof(CLS));
f.close();
getch();
}


I have used the above code to write the class instanc to a file "Text.txt"...
But it seems that "f.write((char*)&c, sizeof(CLS));" is not working properly with string. The data can easily be written using stream object!! Please someone tell me what's the problem and how can I correct it 'coz I require this type of codes for my Project also!!


#include<fstream.h>
#include<conio.h>
#include<string>

class CLS
{
public : string name;
}c;

void main()
{
string name;
fstream f;
f.open("D:\\Text.txt", ios::in);
f.read((char*)&c, sizeof(CLS));
cout<<"Name: "<<c.name.c_str();
cout<<"\nSize: "<<c.name.size();
f.close();
getch();
}
When I tried to read the data on the file...., it gave an error "Thread stopped...violation.."
Please somebody help me out!!!
std::string is not a POD; we can't treat it as a trivially copyable sequence of bytes
We can't use write() / read() / std::memcpy() etc. on it.

Use std::getline() if you are reading in a full name containing white spaces
(instead of just the first or last name; ie a string with no embedded white space).
http://www.cplusplus.com/reference/string/string/getline/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
// #include<fstream.h>
#include <fstream>
// #include<conio.h>
#include <iostream>
#include<string>


class CLS
{
    //public : string name;
    public: std::string name ;
}c;


//void main()
int main()
{
    // fstream f;
    // f.open("D:\\Text.txt", ios::out);
    std::ofstream f( "D:\\Text.txt" ) ;

    // cin>>c.name;
    std::cin >> c.name ;

    // cout<<"\n\nEntered Name: "<<c.name;
    // cout<<"\nSize : "<<c.name.size();
    std::cout << "\n\nEntered Name: " << c.name
               << "\nSize : " << c.name.size() << '\n' ;

    // f.write((char*)&c, sizeof(CLS));
    f << c.name << '\n' ;

    // f.close();
    // getch();

    char c ;
    std::cin >> c ;
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
// #include<fstream.h>
#include <fstream>
// #include<conio.h>
#include <iostream>
#include<string>

class CLS
{
    //public : string name;
    public: std::string name ;
}c;


//void main()
int main()
{
    // string name;
    // fstream f;
    // f.open("D:\\Text.txt", ios::in);

    std::ifstream f( "D:\\Text.txt" ) ;

    // f.read((char*)&c, sizeof(CLS));
    f >> c.name ;

    // cout<<"\n\nEntered Name: "<<c.name;
    // cout<<"\nSize : "<<c.name.size();
    std::cout << "\n\nEntered Name: " << c.name
               << "\nSize : " << c.name.size() << '\n' ;

    // f.close();
    // getch();

    char c ;
    std::cin >> c ;
}
Thanks....
But What if I have a class having variables of other data types also... and I want to write them all on the file in one go!! Aren't you telling me to write and read them all one by one in the program code, are you??
> But What if I have a class having variables of other data types also...
> and I want to write them all on the file in one go!!

Overload the << operator for the class.
http://msdn.microsoft.com/en-us/library/1z2f6c2k.aspx
Thanks for the help!! :D
I've got a new topic to learn.... Operator Overloading!!
Hey, is this possible to convert
int 45 to char "45"
or char "100" to int!!! (I think that's not possible)

Just asking for a knowledge..... I know about the conversions by subtracting 48..... but that works only for single digit only. Suppose if your conversion further(after converting to nos.) requires normal arithmetic operations!!
> I know about the conversions by subtracting 48..... but that works only for single digit only.

It only works if in the encoding used for the execution character set, character '0' has a value of 48. It is at best a non-portable construct that would work for the culture that you belong to, with its most commonly used implementations.



> is this possible to convert int 45 to char "45" or char "100" to int!!!

With a conforming implementation, we could use std::to_string() and std::stoi().
http://en.cppreference.com/w/cpp/string/basic_string/to_string
http://en.cppreference.com/w/cpp/string/basic_string/stol

Or else, we could use a stringstream to perform the conversion for us.
http://www.artima.com/cppsource/streamstrings.html

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
#include <string>
#include <sstream>

int main()
{
    // convert int to string
    {
        const int i = 12345 ;

        std::ostringstream stm ;
        stm << i ;

        const std::string str = stm.str() ;
        const char* cstr = str.c_str() ;
        std::cout << i << ' ' << str << ' ' << cstr << '\n' ;
    }

    // convert string to int
    {
        const std::string str = "-45678" ;
        std::istringstream stm(str) ;
        int i ;
        if( stm >> i ) std::cout << str << ' ' << i << '\n' ;
    }
}

http://ideone.com/ddxNXe
Topic archived. No new replies allowed.