IndianCoder12345
IndianCoder12345

Reputation: 3

multiple functions for stream C++

#include <fstream>
#include <iostream>
#include <cstdlib>
#include <cctype>

using namespace std;

void whitespace_replace(ifstream& in_stream, ofstream& out_stream);
void digit_replace(ifstream& in_stream, ofstream& out_stream);

int main()
{
    ifstream fin;
    ofstream fout;

    cout << "Begin editing files." << endl;
    fin.open("example.dat");

    if (fin.fail())
    {
        cout << "Input file opening failed.\n";
        exit(1);
    }

    fout.open("example2.dat");

    if (fout.fail())
    {
        cout << "Output file opening failed.\n";
        exit(1);
    }

    whitespace_replace(fin, fout);
    digit_replace(fin, fout);
    fin.close();
    fout.close();
    cout << "End of editing files.\n";
    return 0;
}

Function to replace white spaces with a hyphen.

void whitespace_replace(ifstream& in_stream, ofstream& out_stream)
{
    char next;

    do
    {
        in_stream.get(next);

        if (isdigit(next))
            out_stream << '#';
        else
            out_stream << next;
    } while (next != '.');
}

Function to replace digits with a '#':

void digit_replace(ifstream& in_stream, ofstream& out_stream)
{
    char voip;
    do
    {
        in_stream.get(voip);

        if (isspace(voip))
            out_stream << "-";
        else
            out_stream << "-";
    } while (voip != '.');
}

It doesn't let me run both the functions to change the numbers in my .dat file to '#' and replace all blank spaces with a '-'. What do I have to do to make the functions work?

Upvotes: 0

Views: 363

Answers (2)

Biruk Abebe
Biruk Abebe

Reputation: 2233

After returning from the first function the state of fin and fout will not be the same when you pass them to the second function, and in your case seeking the streams back to the beginning,before calling the second function, will not accomplish what you are trying to do because it will undo what you have written to the output. You may need to combine the two function together if you want to read from one file,process the data and output the result to another file directly as you are doing. Otherwise you need to first read the data from the input file, put it in a variable then call the individual functions to process that data and write the processed data to the output file.

One way similar to your might be to combine the logic of the two functions in one function call this function:

char next;
in_stream.get(next);
if (isspace(next))
    out_stream << "-";//if space replace with '-'
else if (isdigit(next))
    out_stream << "#";//if digit replace with '#'..
else
    out_stream << next;

Upvotes: 0

πάντα ῥεῖ
πάντα ῥεῖ

Reputation: 1

The subsequent calls of

whitespace_replace(fin, fout);
digit_replace(fin, fout);

certainly don't see the same stream state for fin or fout.

You have to combine these operations into a single parser/decision hierarchy what to translate from the input in a particular state.

Upvotes: 1

Related Questions