ofstream error in c++ - c++

I am getting an ofstream error in C++, here is my code
int main () {
ofstream myfile;
myfile.open ("example.txt");
myfile << "Writing this to a file.\n";
myfile.close();
return 0;
}
error from Dev-C++ 10
C:\devp\main.cpp aggregate
`std::ofstream OutStream' has
incomplete type and cannot be defined
Thanks in advance

You can try this:
#include <fstream>
int main () {
std::ofstream myfile;
myfile.open ("example.txt");
myfile << "Writing this to a file.\n";
myfile.close();
return 0;
}

The file streams are actually defined in <fstream>.

You may not be including the appropiate header file.
adding #include <fstream> at the beggining of your source file should fix the error.

Probably, you are including the wrong header file. There is a header <iosfwd> that is used for header files that need to reference types from the STL without needing a full declaration of the type. You still are required to include the proper header <iostream> in order to use the types in question.

Include fstream header file that's it. Because ofstream & ifstream are included in fstream.

Add #include <fstream>.
Code:
#include <iostream>
#include <fstream>
using namespace std;
int main() {
std::ofstream myfile;
myfile.open("example.txt", std::ios::out);
myfile << "Writing this to a file\n";
myfile.close();
return 0;
}

I faced the same error because I forgot to use using namespace std;
after including it, the problem was solved.

Related

Trying creating and writing into a txt file in C++

Basically, I'm following a simple tutorial about files handling in C++.
I've been trying to create and write into a txt file at the same time, but any of the methods I've tried won't actually create a txt file in my executable location.
I should also say that, I print myfile.is_open() just to know if the file truly created and opened, but I get 0 everytime with every method.
What am I doing wrong ?
I mainly tried to create and write to a txt file like this:
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
fstream myfile;
myfile.open("example.txt", ios::out);
cout << myfile.is_open() << endl;
myfile << "Writing this to a file.\n";
myfile.close();
}
First, I bet you're using an IDE like Visual Studio. Most IDEs set your working directory somewhere other than your project directory. I don't use Visual Studio, but many of them put them in ../.
So your file is being produced, but not where you think you should find it.
If you compile and run this program without an IDE, you'll get your file where you expect it.
You may also be able to tell your IDE that the working directory should be your project directory.
Now, to keep you from making a few bad habits, I'm going to tell you two more things.
It's considered a mistake to do using namespace std. Instead, I do using statements only on those things I am going to use frequently. In your short code, I wouldn't have done any.
Next, if you're going to write out a file, it's better to use std::ofstream. It's otherwise the same code. But it's a bit clearer that you're only using the file for output.
So my version of your code:
#include <iostream>
#include <fstream>
int main()
{
std::ofstream myfile;
myfile.open("example.txt");
std::cout << myfile.is_open() << std::endl;
myfile << "Writing this to a file.\n";
myfile.close();
}
Yeah, those std:: everywhere can be annoying, so you could do this:
#include <iostream>
#include <fstream>
using std::ofstream;
using std::cout;
using std::endl;
int main()
{
ofstream myfile;
myfile.open("example.txt");
cout << myfile.is_open() << endl;
myfile << "Writing this to a file.\n";
myfile.close();
}
I actually have an include of CommonUsing.h that I put a few things I do almost everywhere.
#pragma once
#include <chrono>
#include <iostream>
#include <date/date.h>
//======================================================================
// The most common using statements I do in most of my code.
//======================================================================
using std::cout;
using std::cerr;
using std::endl;
using std::string;
using namespace std::chrono_literals;
using date::operator<<;

Open() is used for creating a file and opening a existing file.Could someone tell the difference?

I have created a file name c.txt with open() and if I want to make changes in the file then I am going to open with open() then what is the difference?
Could someone please help me out.
#include "stdafx.h"
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
ofstream myfile;
myfile.open("c.txt");
myfile << "gm all";
myfile.close();
}
It's very simple!
ofstream: Output stream.
ifstream: Input stream.
Create the file with ofstream, and open it with ifstream.

writing in a user-defined .txt file

The basic syntax I use to write to a .txt file is this:
ofstream myfile;
myfile.open ("data.txt", ios::trunc);
outfile<<"writing";
Now, supposing i were to let the user decide which file should be accessed, could it be done through a string?
#include <iostream>
#include <fstream>
using namespace std;
int main (){
ofstream myfile;
string filename;
char x;
cin>>x;
if (x==1)
filename="data2.txt";
myfile.open (filename, ios::trunc);
myfile<<"writing";
return 0;
}
I already tested this and it didn't work. My question is: is it possible to do such a thing? If yes, how? When i compile it, the error i get is the following:
undefined reference to 'std::basicofstream<char, std::char_traits<char> >::open(std::string const&, std::_Ios_Openmode)'
I can't understand what it is that's causing it.
Your error says it can't find open method that uses std::string.
Try this:
myfile.open(filename.c_str(), ios::trunc);
Some versions of C++ do not allow the std::string for open method, so you will have to use the c_str() method of std::string.
You should add an else branch since if x isn't equal to 1 open has nothing to open.
Also you forgot to declare ofstream myfile; in your second snippet. Maybe that's the reason this doesn't work (it shouldn't even compile).

Very basic C++ error

Hey guys, I'm writing the simplest thing ever, just creating an ifstream to read in a text file and I have a weird error. Here is the code (note : the '<' missing for iostream and fstream are well written in my code but I couldn't write them here)
#include "genlib.h"
#include "simpio.h"
#include "random.h"
#include "vector.h"
#include "map.h"
#include <iostream>
#include <fstream>
int main() {
ifstream in;
in.open("Hamlet.txt");
if (in.fail()) Error("Could not open file");
return 0;
}
I get the following error after the ifstream in; line : "error : expected unqualified-id before '=' token"
Any idea what's going wrong ?
Thanks
The only thing unusual past ifstream in; is the Error call. My wild guess is that it's a poorly written macro. Try this instead:
int main() {
ifstream in;
in.open("Hamlet.txt");
if (in.fail()) { Error("Could not open file"); }
return 0;
}
Note the new braces around Error.
You need to add
using namespace std;
to use arbitrary names from the library without qualification. Otherwise, the declaration must be
std::ifstream in;
There is also the option
using std::ifstream;
but I wouldn't recommend it, since you probably won't be writing out std::ifstream all that often.
My guess is that in one of your own include files ("genlib.h" and "simpio.h" seem non-Standard), that you're #defined "in"
Try opening the file directly in the constructor:
ifstream inf ( "Hamlet.txt" , ifstream::in );
use std::ifstream
(and provide compilable code; those are not all standard headers, and what is Error()?)

Strange fstream problem

I have really strange problem. In Visual C++ express, I have very simple code, just:
#include <fstream>
using namespace std;
int main()
{
fstream file;
file.open("test.txt");
file<<"Hello";
file.close();
}
This same code works OK in my one project, but when I create now project and use this same lines of code, no file test.txt is created. Please, what is wrong?ยจ
EDIT: I expect to see test.txt in VS2008/project_name/debug - just like the first functional project does.
Canonical code to write to a file:
#include <fstream>
#include <iostream>
using namespace std;
int main() {
ofstream file;
file.open("test.txt");
if ( ! file.is_open() ) {
cerr << "open error\n";
}
if ( ! ( file << "Hello" ) ) {
cerr << "write error\n";
}
file.close();
}
Whenever you perform file I/O you must test every single operation, with the possible exception of closing a file, which it is not usually possible to recover from.
As for the file being created somewhere else - simply give it a weird name like mxyzptlk.txt and then search for it using Windows explorer.
Perhaps the executable is run in a different directory than it was before, making test.txt appear somewhere else. Try using an absolute path, such as "C:\\Users\\NoName\\Desktop\\test.txt" (The double backslashes are needed as escape characters in C strings).
fstream::open() takes two arguments: filename and mode. Since you are not providing the second, you may wish to check what the default argument in fstream is or provide ios_base::out yourself.
Furthermore, you may wish to check whether the file is open. It is possible that you do not have write permissions in the current working directory (where 'test.txt' will be written since you don't provide an absolute path). fstream provides the is_open() method as one way of checking this.
Lastly, think about indenting your code. While you only have a few lines there, code can soon become difficult to read without proper indentation. Sample code:
#include <fstream>
using namespace std;
int main()
{
fstream file;
file.open("test.txt", ios_base::out);
if (not file.is_open())
{
// Your error-handling code here
}
file << "Hello";
file.close();
}
You can use Process Monitor and filter on file access and your process to determine whether the open/write is succeeding and where on disk it's happening.
Theres two ways to fix this. Either do:
file.open("test.txt", ios::out)
#include <fstream>
using namespace std;
int main()
{
fstream file;
file.open("test.txt", ios::out);
file<<"Hello";
file.close();
}
Or you can create an ofstream instead of fstream.
#include <fstream>
using namespace std;
int main()
{
ofstream file;
file.open("test.txt");
file<<"Hello";
file.close();
}