Using freopen() to print to file and screen - c++

I am trying to use freopen() to print to a text file and the screen, but I am only achieving the printing to a file.
I was wondering if there was an easy to save the programs output to a file and print it to the screen? Because I had this working another way, but I ended up having to print out every statement twice. One being for the file the other just for the output.
Note: I am new to C++ and I am trying to learn it for a class next semester so direct answer are needed as I have already look online and couldn't find any simple answers to this solution besides.
Here is what I have so far:
#include<iostream>
#include<time.h>
#include<stdlib.h>
#include<fstream>
using namespace std;
void menu(){
cout << "\t********************************************************\n"
<< "\t* Welcome to slot machine. *\n"
<< "\t* Would you like to play? (1 to play, 2 not to play) *\n"
<< "\t********************************************************\n\n";
return;
}
void update(int arr[], int &token) {
if (arr[0]==arr[1] && arr[1]==arr[2]) {
token+=4;
cout << "You win\n\n";
} else if (arr[0]==arr[1] || arr[1]==arr[2] || arr[0]==arr[2]) {
token+=1;
cout << "You got two out of three\n\n";
} else {
token-=1;
cout << "You lose\n\n";
}
}
int main() {
freopen("file.txt", "w", stdout);
int x, arr[3], token=4;
srand(time(0));
menu();
cin >> x;
while(token!=0) {
cout << "You have " << token << " tokens\n\n"
<< "Pull? (1 to pull, 2 not to pull)\n\n";
cin>>x;
if(x==1) {
for(int i=0; i<3; i++) {
arr[i]=1+rand()%10;
}
cout << "\t\t";
for(int j=0; j<3; j++) {
cout << arr[j] << " ";
}
cout << "\n\n";
update(arr,token);
}
else{
cout << "OK\n";
}
}
cin.get();
return 0;
}

I don't know a simple way to achieve that, but I've managed to solve this somehow.
Using fstreams you can output to file the same way you can write to console.
#include <fstream>
int main()
{
std::ofstream f("file.txt");
f << "something";
}
Now there's a point we can start: is there a way we can output to the console and file simultaneously?
I've recently written stream demultiplexer to address that problem:
#include <vector>
#include <ostream>
class stream_demultiplexer
{
private:
typedef std::vector<std::ostream*> str_cont;
str_cont d;
public:
stream_demultiplexer& put(std::ostream::char_type ch)
{
for(str_cont::iterator it = d.begin(); it != d.end(); ++it)
(*it)->put(ch);
return *this;
}
stream_demultiplexer& write(const std::ostream::char_type* s, std::streamsize count)
{
for(str_cont::iterator it = d.begin(); it != d.end(); ++it)
(*it)->write(s, count);
return *this;
}
stream_demultiplexer& flush()
{
for(str_cont::iterator it = d.begin(); it != d.end(); ++it)
(*it)->flush();
return *this;
}
template<typename T>
stream_demultiplexer& operator<<( const T& obj )
{
for(str_cont::iterator it = d.begin(); it != d.end(); ++it)
(**it) << obj;
return *this;
}
stream_demultiplexer& operator<<(std::ios_base& (*func)(std::ios_base&))
{
for(str_cont::iterator it = d.begin(); it != d.end(); ++it)
(**it) << func;
return *this;
}
template<typename CharT, typename Traits>
stream_demultiplexer& operator<<(std::basic_ios<CharT,Traits>& (*func)(std::basic_ios<CharT,Traits>&) )
{
for(str_cont::iterator it = d.begin(); it != d.end(); ++it)
(**it) << func;
return *this;
}
stream_demultiplexer& operator<<(std::ostream& (*func)(std::ostream&) )
{
for(str_cont::iterator it = d.begin(); it != d.end(); ++it)
(**it) << func;
return *this;
}
void add_stream(std::ostream& ss)
{
d.push_back(&ss);
}
};
You can use it like this:
stream_demultiplexer spl;
std::ofstream f("file.txt");
spl.add_stream(f);
spl.add_stream(std::cout);
spl << 55 << " HELLO WORLD";
My approach has advantage that manipulators and unformatted output works correctly:
spl << 76 << " " << std::hex << 76 << std::endl;
spl.put('a');
spl.write("ABCDE", 5);

The easy way in a UNIX-like environment is to use the shell command tee:
$ my-program | tee output.txt
will copy stdout to the terminal, and also to the file output.txt.
If you have to do it in code, you could use your own output stream instead of cout, which forwards every operator<< to two (or more) ostreams. This feels nicer (to me) than mucking around with the C output file underlying the C++ ostream cout.
#include <ostream>
class Tee {
std::ostream &first, &second;
template<typename T> friend Tee& operator<< (Tee&, T);
public:
Tee(std::ostream &f, std::ostream &s) : first(f), second(s) {}
};
template <typename T>
Tee& operator<< (Tee &t, T val)
{
t.first << val;
t.second << val;
return t;
}
Then, if you replace your freopen line with:
std::ofstream outfile("file.txt");
Tee tee(std::cout, outfile);
you can just use tee << instead of cout <<.
Note that you'll either need to pass tee into your functions, or make it a global for that to work.

Related

creating a class vector that does not delete it's content

I am a beginner , so i wanted to ask , can we create a class object vector/array , that does not delete it's content when i close the program like , so like I want a customer record , but whenever if we try to restart the program we need to enter the customer details again and again ...
how to prevent that from happening
#include <iostream>
#include <vector>
using namespace std;
class customer{
public:
int balance;
string name;
int password;
};
int main(){
vector <customer> cus;
...
if(choice == 1){
cout << cus[i].balance
}
return 0;
}
As a complement to Adam's answer, it is possible to encapsulate the serialization in the container class itself. Here is an simplified example:
The header file defining a persistent_vector class that saves its content to a file:
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <initializer_list>
namespace {
// Utility functions able to store one element of a trivially copyable type
template <class T>
std::ostream& store1(std::ostream& out, const T& val) {
out.write(reinterpret_cast<const char*>(&val), sizeof(val));
return out;
}
template <class T>
std::istream& load1(std::istream& in, T& val) {
in.read(reinterpret_cast<char*>(&val), sizeof(val));
return in;
}
// Specialization for the std::string type
template <>
std::ostream& store1<std::string>(std::ostream& out, const std::string& val) {
store1<size_t>(out, val.size());
if (out) out.write(val.data(), val.size());
return out;
}
template <>
std::istream& load1<std::string>(std::istream& in, std::string& val) {
size_t len;
load1<size_t>(in, len);
if (in) {
char* data = new char[len];
in.read(data, len);
if (in) val.assign(data, len);
delete[] data;
}
return in;
}
}
template <class T>
class persistent_vector {
const std::string path;
std::vector<T> vec;
// load the vector from a file
void load() {
std::ifstream in(path);
if (in) {
for (;;) {
T elt;
load1(in, elt);
if (!in) break;
vec.push_back(elt);
}
if (!in.eof()) {
throw std::istream::failure("Read error");
}
in.close();
}
}
// store the vector to a file
void store() {
std::ofstream out(path);
size_t n = 0;
if (out) {
for (const T& elt : vec) {
store1(out, elt);
if (!out) break;
++n;
}
}
if (!out) {
std::cerr << "Write error after " << n << " elements on " << vec.size() << '\n';
}
}
public:
// a bunch of constructors, first ones load data from the file
persistent_vector(const std::string& path) : path(path) {
load();
}
persistent_vector(const std::string& path, size_t sz) :
path(path), vec(sz) {
load();
};
// last 2 constructors ignore the file because they do receive data
persistent_vector(const std::string& path, size_t sz, const T& val) :
path(path), vec(sz, val) {
};
persistent_vector(const std::string& path, std::initializer_list<T> ini) :
path(path), vec(ini) {
}
// destructor strores the data to the file before actually destroying it
~persistent_vector() {
store();
}
// direct access to the vector (const and non const versions)
std::vector<T>& data() {
return vec;
}
const std::vector<T>& data() const {
return vec;
}
};
It can, out of the box, handle any trivially copyable type and std::string. User has to provide specializations of store1 and load1 for custom types.
Here is a trivial program using it:
#include <iostream>
#include <string>
#include "persistent_vector.h"
int main() {
std::cout << "Create new vector (0) or read an existing one (1): ";
int cr;
std::cin >> cr;
if (!std::cin || (cr != 0 && cr != 1)) {
std::cout << "Incorrect input\n";
return 1;
}
if (cr == 0) {
persistent_vector<std::string> v("foo.data", 0, "");
// skip to the end of line...
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
for (;;) {
std::string line;
std::cout << "Enter a string to add to the vector (empty string to end program)\n";
std::getline(std::cin, line);
if (line.empty()) break;
v.data().push_back(line);
}
}
else {
persistent_vector<std::string> v("foo.data");
for (const std::string& i : v.data()) {
std::cout << i << '\n';
}
}
return 0;
}
When a programmer creates a vector class, he must ensure that the resources acquired for that vector are released when they are no longer needed. (See RAII)
C++ Reference : https://en.cppreference.com/w/cpp/language/raii
Wikipedia : https://en.wikipedia.org/wiki/Resource_acquisition_is_initialization
Stack Overflow : What is meant by Resource Acquisition is Initialization (RAII)?
Microsoft : https://learn.microsoft.com/en-us/cpp/cpp/object-lifetime-and-resource-management-modern-cpp?view=msvc-170
Before the program closes, all resources must be released.
(No leaking resources, memory included)
It is not possible to create a vector class that does not delete its contents after closing a program. Secure operating systems will release program resources when the program is closed.
If you want the program not to lose customer information after closing, you need to save the information in persistent (non-volatile) storage device, such as a disk.
As CinCout, 김선달, Serge Ballesta say, you have to save the customer information to a file, and write the program so that you can read that file during the start of the program.
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
struct customer {
std::string name;
int balance;
int password;
};
int main() {
std::vector <customer> customers;
std::ifstream ifs("info.txt");
{
customer customer{};
while (ifs >> customer.name >> customer.balance >> customer.password)
customers.push_back(customer);
}
for (const auto& [name, balance, password] : customers) {
std::cout <<
"\nName : " << name <<
"\nBalance : " << balance <<
"\nPassword : " << password <<
'\n';
}
std::cout << "\n\nWelcome\n\n";
std::ofstream ofs("info.txt", std::ios_base::app);
char cont{};
do {
customer customer{};
std::cout << "Name : ";
std::cin >> customer.name;
std::cout << "Balance : ";
std::cin >> customer.balance;
std::cout << "Password : ";
std::cin >> customer.password;
ofs << customer.name << ' ' << customer.balance << ' ' << customer.password << '\n';
std::cout << "Add another customer? (Y/N) : ";
std::cin >> cont;
} while (cont == 'Y');
for (const auto& [name, balance, password] : customers) {
std::cout <<
"\nName : " << name <<
"\nBalance : " << balance <<
"\nPassword : " << password <<
'\n';
}
}
CPlusPlus : https://www.cplusplus.com/doc/tutorial/files/
LearnCpp : https://www.learncpp.com/cpp-tutorial/basic-file-io/
(About File I/O)
This program is a prototype, I left some things incomplete (like check readings, user-defined I/O operators, duplicate code, formatting, reallocations of customers, ifs is not required after range-for + structured binding,...).
I suggest you read the book "Programming: Principles and Practice Using C+", I’m reading it and it helped me a lot.
(I’m also a beginner)
Edit: I also suggest you use "using namespace std;" only for small projects, examples or simple exercises.
Do not use "using namespace std;" for real projects, large projects or projects that may include other dependencies because the use of "using namespace std;" could lead to a possible naming collisions between names within std and the names of other codes and libraries.
It’s not good practice to use it all the time.

Read data from a file into struct and add struct to a vector (to create a vector of structs)

This question has been asked before but the other question/answers used concepts I'm not yet familiar with in C++.
I need to read data from a file into a vector of structs.
I have the following code but I'm confused about what to put in (....), that is if I have the correct logic.
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
struct Parts{
std::string partNum;
char partClass;
int stock;
float cost;
};
bool readFile(std::vector <Parts>&);
int displayMenu();
int main(){
std::vector <Parts> pVector;
readFile(pVector);
if (!readFile(pVector)){
std::cout << "Error reading file!" << std::endl;
}
else{
displayMenu();
}
return 0;
}
bool readFile(std::vector <Parts> &pVector){
std::ifstream inputFile("parts.txt");
if (inputFile.fail()){
std::cout << "Error reading file!" << std::endl;
return false;
}
else{
while (....){
pVector.emplace_back(....);
}
return true;
}
}
Sample lines from the file:
P-42936 A 18 129.79
P-43179 A 47 35.60
P-43264 B 31 103.81
P-43367 B 5 32.39
P-43378 A 46 6.38
P-43622 A 10 155.36
You want that :
bool readFile(std::vector <Parts> &pVector){
std::ifstream inputFile("parts.txt");
if (inputFile.fail()){
std::cout << "Error reading file!" << std::endl;
return false;
}
else {
Parts part;
while (inputFile >> part.partNum >> part.partClass >> part.stock >> part.cost)
pVector.emplace_back(part);
return true;
}
}
In main you call two time the read function :
readFile(pVector);
if (!readFile(pVector)){
very probably the first call must be removed
It can be also interesting to define the operator >> for Parts rather than to have the code doing that in readFile
So :
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
struct Parts{
std::string partNum;
char partClass;
int stock;
float cost;
};
std::istream & operator >>(std::istream & is, Parts & part) {
if (is >> part.partNum >> part.partClass >> part.stock)
is >> part.cost;
return is;
}
bool readFile(std::vector <Parts>&);
//int displayMenu();
int main(){
std::vector <Parts> pVector;
if (!readFile(pVector)){
std::cout << "Error reading file!" << std::endl;
}
else{
//displayMenu();
// to check, of course operator << can be defined too
for (auto p : pVector)
std::cout << p.partNum << '/' << p.partClass << '/' << p.stock << '/' << p.cost << std::endl;
}
return 0;
}
bool readFile(std::vector <Parts> &pVector){
std::ifstream inputFile("parts.txt");
if (inputFile.fail()){
std::cout << "Error reading file!" << std::endl;
return false;
}
else {
Parts part;
while (inputFile >> part)
pVector.emplace_back(part);
return true;
}
}
Compilation and execution:
pi#raspberrypi:/tmp $ g++ -Wall r.cc
pi#raspberrypi:/tmp $ cat parts.txt
P-42936 A 18 129.79
P-43179 A 47 35.60
P-43264 B 31 103.81
P-43367 B 5 32.39
P-43378 A 46 6.38
P-43622 A 10 155.36
pi#raspberrypi:/tmp $ ./a.out
P-42936/A/18/129.79
P-43179/A/47/35.6
P-43264/B/31/103.81
P-43367/B/5/32.39
P-43378/A/46/6.38
P-43622/A/10/155.36
pi#raspberrypi:/tmp $
I suggest adding overloads for operator>> and operator<<:
#include <iostream>
#include <string>
#include <vector>
struct Part { // I renamed it because it only holds info about one part
std::string partNum;
char partClass;
int stock;
float cost;
};
std::istream& operator>>(std::istream& is, Part& p) {
return is >> p.partNum >> p.partClass >> p.stock >> p.cost;
}
std::ostream& operator<<(std::ostream& os, const Part& p) {
return os << p.partNum << ' ' << p.partClass << ' ' << p.stock << ' ' << p.cost;
}
This makes extracting or printing one Part easy:
bool readFile(std::vector<Part>& pVector){
std::ifstream inputFile("parts.txt");
if(inputFile) {
Part tmp;
while(inputFile >> tmp) // extract one Part at a time using operator>>
pVector.emplace_back(tmp);
return true;
} else {
std::cout << "Error reading file!" << std::endl;
return false;
}
}

How to add integers and strings in the same vector?

I need help for my university homework. i'm still new to this.
Basically i am doing a run-length encoding and i don't know how to add the letter after the counter:
#include <iostream>
#include <string>
#include <vector>
#include <stdexcept>
void error(std::string str)
{
throw std::runtime_error(str);
}
int main()
{ int counter = 1;
std::string id;
std::vector<int> v;
std::cout << "Enter the data to be compressed: ";
std::cin >> id;
try
{ for(int i = 0; i < id.size(); i++)
{
if(std::isdigit(id[i]))
error("invalid input");
}
std::cout << "The compressed data is: ";
for(int i = 0; i < id.size(); i++)
{
if(id[i] == id[i+1])
{
counter++;
}
else if(id[i]!= id[i+1])
{
v.push_back(counter);
v.push_back(id[i]);
counter=1;
}
}
for(int j = 0; j < v.size(); j++)
std::cout << v[j];
}
catch(std::runtime_error& str)
{
std::cerr << "error: " << str.what() << std::endl;
return 1;
}
return 0;
}
For example if i input aaabbb, the probram should output 3a3b. The problem is that it outputs 397398 97 and 98 being the ascii code for a and b.
i don't know how to put the letter after the counter and for them to be in the same vector.
If you want to serialize as a string try this :
#include <iostream>
#include <string>
#include <vector>
#include <stdexcept>
#include <sstream>
void error(std::string str) {
throw std::runtime_error(str);
}
int main() {
std::ostringstream stream;
int counter = 1;
std::string id;
std::cout << "Enter the data to be compressed: ";
std::cin >> id;
try {
for (int i = 0; i < id.size(); i++) {
if (std::isdigit(id[i]))
error("invalid input");
}
std::cout << "The compressed data is: ";
for (int i = 0; i < id.size(); i++) {
if (id[i] == id[i + 1]) {
counter++;
} else if (id[i] != id[i + 1]) {
stream << counter;
stream << (char) id[i];
counter = 1;
}
}
std::cout << stream.str() << std::endl;
} catch (std::runtime_error& str) {
std::cerr << "error: " << str.what() << std::endl;
return 1;
}
}
v[j] from std::cout << v[j] is of type int and that is why std::cout writes a number. To write it as a character, you should cast v[j] to char as follows: std::cout << (char)v[j]. In this way, std::cout will use the char specialization, not the int one.
While the other answers might give you the output you need, I believe the idiomatic way to solve this is using a class to hold both the character and its count. There are two obvious choices.
std::pair
Could also be std::tuple if you prefer it for consistency or whatever reason. Save your results in a std::vector<std::pair<char, int>. This saves the information, but to print it you would need to define an appropriate function. Add elements via
v.emplace_back(character, count);
Wrapper Class
If you want to offer some functionality without outside helper classes, define a custom wrapper class such as the following.
class CharacterCount {
private:
char character;
int count;
public:
CharacterCount(char character, int count):
character(character), count(count) {}
explicit operator std::string() const { return std::to_string(count) + character;
// Other helper functions or constructors you require
}
This simplifies printing
for (auto& character_count : v)
std::cout << static_cast<std::string>(character_count);
I believe because std::ostream::operator<< is templated, you cannot get an implicit conversion to std::string to work. I would advise against implicit conversion anyway.
You can use the same emplace_back syntax as before because we offer an appropriate constructor.
So you take your input in a string and ultimately just need to stream this information out, ultimately meaning there's really no reason to store the information in a vector, just output it! You can use find_if with a lambda to find the non-consecutive character (or find_if_not if you prefer.)
for(string::const_iterator finish, start = cbegin(id); start != cend(id); start = finish) {
finish = find_if(start, cend(id), [value = *start](const auto i) { return i != value; } );
cout << distance(start, finish) << *start;
}
Live Example

Link List - no operator '<<' matches

My c++ is really poor. Anyhow with the code snippet bellow why do I get a error on the << in the do while loop when outside of it I get no error. The error is: no operator "<<" matches these operands. However the string w picks up the word fine. I read somewhere I may have to overload it but why? And how would I over load it for a link list.
Thanks in advance.
void print()
{
HashTable *marker = headOne;
HashTable *inList;
for( int i = 0; i < tableSize; i++ )
{
cout << i << ": " << marker->number << endl;
if(marker->child != NULL)
{
inList = marker;
do
{
string w = inList->word;
cout << w << endl;
inList = inList->child;
}
while(inList != NULL);
}
marker = marker->next;
}//end for loop
}
In order to be able to cout a std::string you have to include:
#include <string>
#include <iostream>
This works:
// Missing includes and using
#include <string>
#include <iostream>
using namespace std;
// missing struct
struct HashTable {
HashTable* next;
HashTable* child;
string word;
int number;
};
// missing vars
HashTable ht;
HashTable* headOne = &ht;
int tableSize = 5;
// Unchanged
void print()
{
HashTable *marker = headOne;
HashTable *inList;
for( int i = 0; i < tableSize; i++ )
{
cout << i << ": " << marker -> number << endl;
if(marker->child != NULL)
{
inList = marker;
do
{
string w = inList -> word;
cout << w << endl;
inList = inList -> child;
}
while(inList != NULL);
}
marker = marker -> next;
}//end for loop
}
I read somewhere I may have to overload it but why?
Because there's no overload that matches your need.
And how would I over load it for a link list.
You can do this outside of your class or struct:
(where T is the type of the object that you want to print)
std::ostream& operator<<(std::ostream& os, const T& obj)
{
/* write obj to stream */
return os;
}
This is just an example that prints a vector:
std::ostream& operator<<(std::ostream& os, vector<int>& obj)
{
for (auto &i : obj)
os << i << " ";
return os;
}
Then I would be able to simply do this cout << n.vec; where n is the class object and vec the name of a vector of ints.

C++ fstream in >> variableName execution

Here is my code, not sure why no error but return nothing. it is OK if there is no getline function in ReadFile?? And when debugging, another potential problem is whether the map is needed to define the size when map is declared. I am a beginner. any help is appreciated.
#include <iostream>
#include <fstream>
#include<map>
#include<set>
#include<string>
void swap(char &ch1, char &ch2){ //swap the content
char tmp=ch1;
ch1=ch2;
ch2=tmp;
}
std::string ToLower(std::string s){
for(int i=0;i < s.length();i++)
{
if(s[i]<='Z' && s[i]>='A')
{
s[i]-='A'-'a';
}
}
return s;
}
std::string signature(std::string s)
{
s=ToLower(s);
for(int i=0;i<s.length();i++)
{ int minIndex=i;
for(int j=i+1;j<s.length();j++)
if(s[j]<s[minIndex]) minIndex=j;
swap(s[minIndex],s[i]);
}
return s;
}
void ReadFile(std::ifstream &in, std::map<std::string,std::set<std::string>> &m)
{
while(true)
{
std::string word;
in>>word;
if(!in.good())break;
m[signature(word)].insert(word);
}
}
typedef std::map<std::string, std::set<std::string>>::const_iterator MapIterator;
typedef std::set<std::string>::const_iterator SetIterator;
int main(){
std::ifstream in("ospd.txt");
std::map<std::string, std::set<std::string>> m;
ReadFile(in,m);
for (MapIterator iter = m.begin(); iter != m.end(); iter++)
{
std::cout << "Key: " << iter->first << std::endl << "Values:" << std::endl;
for (SetIterator set_iter = iter->second.begin(); set_iter != iter->second.end(); set_iter++)
std::cout << " " << *set_iter <<std:: endl;
}
system("pause");
return 0;}
Updated: the programming is working. Thanks for everybody!!!
Can you try this.
#include <iterator>
using namespace std;
void ReadFile(std::ifstream &in, std::map<std::string,std::set<std::string>> &m)
istream_iterator<string> itr(in);
istream_iterator<string> end;
while(itr != end)
{
string work = *itr;
m[signature(word)].insert(word);
++itr;
}