Writing Data to Files
-
I changed it so that it reads: hours<
-
I changed it so that it reads: hours<
And of which type is your
day
entity ? Is it just anint
?
We can do no great things, only small things with great love. - Mother Theresa
-
I changed it so that it reads: hours<
Can you compile this ?
// --------
// Main.cpp
// --------
/**
* @file
* @brief Re: main()
* @version 0.1
*/// --------
// Includes
// --------
#include <iostream>
#include <fstream>// ---------------
// Used namespaces
// ---------------
using namespace std;// -------------------------------
// Definition of the MyClass class
// -------------------------------
/**
* Foo.
*/
class MyClass
{public:
// ------------ // Construction // ------------ /// standard-constructor MyClass(); // ------------- // Serialization // ------------- /// writes the object to the passed stream ostream& write(ostream& Stream) const;
private:
// ---------- // Attributes // ---------- /// the first member int m\_first; /// the second member int m\_second; /// the third member int m\_third;
};
// -------
// MyClass
// -------
/**
* The standard-constructor.
*/
MyClass::MyClass()
{
m_first = 1;
m_second = 2;
m_third = 3;
}// -----
// write
// -----
/**
* Writes the object to the passed stream.
*/
ostream& MyClass::write(ostream& Stream) const
{
return Stream << "first : " << m_first << endl
<< "second : " << m_second << endl
<< "third : " << m_third;
}// ----------
// operator<<
// ----------
/**
* ostream << MyClass
*/
ostream& operator<<(ostream& Stream, const MyClass& Object)
{
return Object.write(Stream);
}// ----
// main
// ----
/**
* The application starts here.
*
* @param argc number of arguments
* @param argv list of arguments
*
* @return 0 if finished successfully
*/
int main(int argc, char** argv)
{
// ofstream cout("cout.txt");cout << MyClass() << endl; return 0;
}
We can do no great things, only small things with great love. - Mother Theresa
-
yes, hours is an ofstream.
-
And of which type is your
day
entity ? Is it just anint
?
We can do no great things, only small things with great love. - Mother Theresa
yes, my "day" is an int
-
Can you compile this ?
// --------
// Main.cpp
// --------
/**
* @file
* @brief Re: main()
* @version 0.1
*/// --------
// Includes
// --------
#include <iostream>
#include <fstream>// ---------------
// Used namespaces
// ---------------
using namespace std;// -------------------------------
// Definition of the MyClass class
// -------------------------------
/**
* Foo.
*/
class MyClass
{public:
// ------------ // Construction // ------------ /// standard-constructor MyClass(); // ------------- // Serialization // ------------- /// writes the object to the passed stream ostream& write(ostream& Stream) const;
private:
// ---------- // Attributes // ---------- /// the first member int m\_first; /// the second member int m\_second; /// the third member int m\_third;
};
// -------
// MyClass
// -------
/**
* The standard-constructor.
*/
MyClass::MyClass()
{
m_first = 1;
m_second = 2;
m_third = 3;
}// -----
// write
// -----
/**
* Writes the object to the passed stream.
*/
ostream& MyClass::write(ostream& Stream) const
{
return Stream << "first : " << m_first << endl
<< "second : " << m_second << endl
<< "third : " << m_third;
}// ----------
// operator<<
// ----------
/**
* ostream << MyClass
*/
ostream& operator<<(ostream& Stream, const MyClass& Object)
{
return Object.write(Stream);
}// ----
// main
// ----
/**
* The application starts here.
*
* @param argc number of arguments
* @param argv list of arguments
*
* @return 0 if finished successfully
*/
int main(int argc, char** argv)
{
// ofstream cout("cout.txt");cout << MyClass() << endl; return 0;
}
We can do no great things, only small things with great love. - Mother Theresa
yes, i can
-
yes, i can
Maybe the compiler gets confused by your included header files. Try to use: <string> instead of <string.h> <fstream> instead of <fstream.h> <iostream> instead of <iostream.h> ... And avoid mixing them up.
We can do no great things, only small things with great love. - Mother Theresa
-
I am trying to write data to a file so that I can open it later. I am trying to save the values of variable to the file. After the file has been opened, I input: "hours << day << endl;" "hours" is the outfile and "day" is a variable that I am trying to write into it. If I remove the "<
Something else is at play here. This works fine for me:
#include <fstream.h>
void main( void )
{
ofstream hours("c:\\file.txt");
int day = 20;
hours << day << endl;
}
"Take only what you need and leave the land as you found it." - Native American Proverb
-
Something else is at play here. This works fine for me:
#include <fstream.h>
void main( void )
{
ofstream hours("c:\\file.txt");
int day = 20;
hours << day << endl;
}
"Take only what you need and leave the land as you found it." - Native American Proverb
Yes, this problem is really a bit strange... On my system the following code causes a C2679 error:
#include <string>
#include <fstream.h>int main(int argc, char** argv)
{
ofstream hours("Hours.txt");hours << "Hallo" << std::endl; return 0;
}
We can do no great things, only small things with great love. - Mother Theresa
-
Yes, this problem is really a bit strange... On my system the following code causes a C2679 error:
#include <string>
#include <fstream.h>int main(int argc, char** argv)
{
ofstream hours("Hours.txt");hours << "Hallo" << std::endl; return 0;
}
We can do no great things, only small things with great love. - Mother Theresa
Writing
string
is different than writingint
(your original problem). Theofstream
class does not support writingstring
data. You have to provide that yourself:#include <fstream>
std::ofstream &operator<<( std::ofstream &os, const std::string &str )
{
os << str.c_str();
return os;
}
"Take only what you need and leave the land as you found it." - Native American Proverb