Printing output to a text file

Oct 4, 2017 at 4:28pm
After spending time trying to find out how to direct output to a printer (I use a wireless printer) without success, I decided to look at printing to a text file instead. I found this code on cplusplus by googling and this works.

1
2
3
4
5
6
7
8
9
10
11
12
// basic file operations
#include <iostream>
#include <fstream>
using namespace std;

int main () {
  ofstream myfile;
  myfile.open ("example.txt");
  myfile << "Writing this to a file.\n";
  myfile.close();
  return 0;
}


I decided to play around with it, to see if control characters such as \n and \t worked, which they do. Obviously saving to the same filename every time is not desirable, so I thought I'd try to include code to ask for a filename and allocate it to a variable and replace the filename in the code with the variable. Unfortunately, it doesn't work.

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

using namespace std;

int main () {
  string a, b, fileName;
  
  cout << "ENTER FILE NAME (******.txt) ";
  cin fileName;
  ofstream txtOut;
  //txtOut.open ("Example.txt");
  txtOut.open (fileName);
   txtOut << "Writing this to a file.\n";
   txtOut << "\n";
   a = "Line of text. \n";
   b = "This is a ";
   txtOut << b+a;  // links b and a into a single string
   
   // this demonstrates the use of tabs
   txtOut << b << "\t\t" << a;
  txtOut.close();
  return 0;
}


It works with CodeBlocks but not with wxDev-C++ nor with Falcon IDE. What is wrong and what needs to be done to correct it?
Oct 4, 2017 at 4:36pm
It works with CodeBlocks but not with wxDev-C++ nor with Falcon IDE. What is wrong and what needs to be done to correct it?

Since this is a C++11 feature you need to insure you're compiling code using a C++11 or higher compatible compiler. You could also try using a C-string when opening the file (ofstream txtOut(fileName.c_str);)

Oct 4, 2017 at 4:41pm
What doesn't work about it? Do you get compile errors? Do you not get the expected output?

Line 3: You're missing the #include <string> header.

Line 10: You're missing the >> operator.



Oct 4, 2017 at 5:28pm
@jlb
They are all using Gcc but wxDev-C++ and Falcon IDE also use MingW. They are all recent installs, so I assume all are C++11 or higher. Using your suggestion results in error for all IDEs.

@AbstractionAnon
The original code works Ok and the later code works in Codeblocks. It comes up with errors for line 13 and doesn't compile.

Codeblocks worked without the #include <string> header. It made no difference to the other two IDEs.
I've no idea why the >> operator was missing in line 10. It's there in my code and I did a copy and paste. Obviously, it won't work without it.

is it likely to be anything to do with MingW? I mainly use Codeblocks, but I would like it to work in the other two.

Thanks very much for your help.
Oct 4, 2017 at 5:49pm
Most IDEs don't do much configuration of the compiler by default, so you probably need to have the IDE tell the compiler to use C++11 or higher, depending on the version of the compiler. Until fairly recently g++ defaulted to gnu98 "standard", so you may need to add a compile switch (-std=c++11) to insure C++11 compiling. You'll need to read your IDE documentation to see how to accomplish this.


Last edited on Oct 4, 2017 at 5:50pm
Topic archived. No new replies allowed.