0

so i have written a chunk of code to take in data from a txt file then perform some calculations on it. However, i am trying to print the original contents of the txt file to the screen before the the rest of the function executes and I am running into some problems. it either doesnt print to the screen or it makes my output file empty or only displays one line. any help about what to do would be greatly appreciated!

#include fstream
#include iostream
#include iomanip
#include string
using namespace std;


int main()
{

    // defines the input/out streams for the data file
    ifstream dataIn;
    ofstream dataOut;

    // Contains amount item purchased
    int a, b, t, noOfDishes;
    int res, res1;
    string inputfile;


    cout << "Please enter the name of the file you want to open:" << endl;
    getline(cin, inputfile);

    // Opening the input file
    dataIn.open(inputfile);
        cout << infile1.rdbuf();





    // checking whether the file name is valid or not
    if (dataIn.fail())
    {
        cout << "** File Not Found **";
        return 1;
    }
    else
    {
        // creating and Opening the output file
        dataOut.open("output.txt");


        while (dataIn >> a >> b >> t)
        {
            res = 0;
            noOfDishes = 0;
            dataOut << a << "\t" << b << "\t" << t << "\t";
            res1 = a;
            res = a;

            while (true)
            {

                if (res <= t)
                {
                    noOfDishes++;
                    res1 = (res1 + b);
                    res += res1;

                }
                else
                    break;
            }
            dataOut << noOfDishes << endl;

        }
        // Closing the input file
        ;
        dataIn.close();


        cout << " Data Written to output.txt " << endl;
        // Closing the output file.
        dataOut.close();
    }

    return 0;
}
Some programmer dude
  • 363,249
  • 31
  • 351
  • 550
  • 1
    `dataIn` or `infile1`? Please try to create a [mcve] which doesn't contain other unrelated errors. Preferably we should be able to copy-paste it and replicate your problems. – Some programmer dude May 05 '19 at 22:47
  • 1
    As for the "makes my output file empty", that's very likely unrelated to the issue of the input file not being printed on standard output. You should try to separate these two issues into two questions (and please read about [how to ask good questions](http://stackoverflow.com/help/how-to-ask), as well as [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/)). I also recommend that you [learn how to debug your programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). – Some programmer dude May 05 '19 at 22:57
  • 1
    Sorry, looking back over it I kind of mangled my input as I copied an older version instead of my code instead of the most current. dataIn IS infile1. I was able to fix my issue shortly after posting this by including dataIn.clear() and dataIn.seekg(0) and using an if else loop to display the contents initially. Excuse my sloppiness, its been a hectic day. – Preston Dotsey May 05 '19 at 23:32
  • Research c++ "slurp()". – 2785528 May 06 '19 at 02:36

1 Answers1

0

Before dataOut.open(), add these lines that display the contents of the file:

char ch = dataIn.get(); 
while(ch != EOF){
     cout << ch; //Output character
      ch = dataIn.get();
}
dataIn.clear(); //Remove the dirty bit EOF

This reads the characters of the file one by one and outputs it to the console till it reaches end of file Edit: As pointed out by @Some Programmer Dude , if your compiler treats char as an unsigned type, instead of: while(ch != EOF) Use:

while(!dataIn.eof())

Amal K
  • 2,135
  • 9
  • 27
  • `char ch` and `ch != EOF` will not work very well if `char` happens to be an unsigned type (which is up to the implementation (compiler) to decide). There's a reason the [`get`](https://en.cppreference.com/w/cpp/io/basic_istream/get) function returns some other "integer type". – Some programmer dude May 05 '19 at 23:01
  • There is another way to test the condition: ```while(!dataIn.eof())``` Thanks for pointing that out :) – Amal K May 05 '19 at 23:04
  • 1
    Not really a good solution either, see [Why is iostream::eof inside a loop condition (i.e. `while (!stream.eof())`) considered wrong?](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons) – Some programmer dude May 05 '19 at 23:09
  • 1
    I was actually able to figure it out by including dataIn.clear() and dataIn.seekg() and using an If Else loop to print the contents before. Thank you for your help! – Preston Dotsey May 05 '19 at 23:37