user2917900
user2917900

Reputation: 29

Streaming numbers from a file

I was hoping there was someone who could help me with the problem I am having here. My program is below, and the problem I am having is that I can't figure out how to write the process() function to take a .txt file with a bunch of random numbers, read the numbers, and output only the positive ones to a separate file. I have been stuck on this for days now and I don't know where else to turn. If anyone could provide some help of any kind I would be very appreciative, thank you.

/*  
    10/29/13
    Problem: Write a program which reads a stream of numbers from a file, and writes only the positive ones to a second file. The user enters the names of the input and output files. Use a function named process which is passed the two opened streams, and reads the numbers one at a time, writing only the positive ones to the output.

*/
#include <iostream>
#include <fstream>
using namespace std;

void process(ifstream & in, ofstream & out);

int main(){
    char inName[200], outName[200];

    cout << "Enter name including path for the input file: ";
    cin.getline(inName, 200);
    cout << "Enter name and path for output file: ";
    cin.getline(outName, 200);

    ifstream in(inName);
    in.open(inName);
    if(!in.is_open()){ //if NOT in is open, something went wrong
        cout << "ERROR: failed to open " << inName << " for input" << endl;
        exit(-1);       // stop the program since there is a problem
    }
    ofstream out(outName);
    out.open(outName);
    if(!out.is_open()){ // same error checking as above.
        cout << "ERROR: failed to open " << outName << " for outpt" << endl;
        exit(-1);
    }
    process(in, out); //call function and send filename

    in.close();
    out.close();

    return 0;
}


void process(ifstream & in, ofstream & out){
        char c;
    while (in >> noskipws >> c){ 
        if(c > 0){
            out << c;
        }
    }

    //This is what the function should be doing:
    //check if files are open
    // if false , exit
    // getline data until end of file
    // Find which numbers in the input file are positive
    //print to out file
    //exit


}

Upvotes: 1

Views: 641

Answers (1)

David G
David G

Reputation: 96810

You shouldn't be using char for the extraction. What if the value to be extracted is greater than 1 byte? Also, std::noskipws turns off the skipping of whitespace, effectively making it difficult to extract a whitespace separated list of numbers. Use only std::noskipws if a whitespace character is valid character to extract, otherwise let the file stream do its job.

If you know the Standard library well, you can use generic algorithms like std::remove_copy_if that take iterators like the ones below:

void process(std::ifstream& in, std::ofstream& out)
{
    std::remove_copy_if(std::istream_iterator<int>(in),
                        std::istream_iterator<int>(),
                        std::ostream_iterator<int>(out, " "),
                                            [] (int x) { return x % 2 != 0; });
}

This requires use of C++11. Add the -std=c++11 option to your program or upgrade your compiler.

If you can't use these methods, then at least use int during the extraction:

int i;

while (in >> i)
{
    if (i % 2 == 0)
        out << i;
}

You said in your comments that you need to use getline. This is wrong. I'm assuming here that you have multiple lines of space-separated integers. If this is the case, getline is unneeded.

Upvotes: 3

Related Questions