-2

I am trying to create a program that asks the user for the name of a file, then opens the file, adds the sum of all the integers listed on the file, then writes that sum on an output file.

After writing my code and saving the testfile1.txt into the same folder as the program, the program keeps giving me the: "could not access testfile1" (message I output to notify myself that it is unable to open the testfile1.txt).

Here is what I have so far (skipped the lines with description blocks):

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main(){
    ifstream inputFile;
    ofstream outputFile;

    string testfile1;
    string sum;

    int total = 0;
    int num;

    cout << "Please input name of file." << endl;
    cin >> testfile1;
    cin.get();

    inputFile.open(testfile1.c_str());

    if (inputFile.fail()) {
        inputFile.clear();
        cout << "could not access testfile1" << endl;
        return(1);
    }

    while (!inputFile.eof()) {
        inputFile >> num;
        total = total + num;
        inputFile.close();
    }

    outputFile.open(sum.c_str());
    outputFile << total << endl;
    outputFile.close();

    if (outputFile.fail()) {
        cout << "could not access file." << endl;
        return 1;
    }

    return 0;
}

Question:

How can I make this program find and open the testfile1.txt?

Note:

I am pretty sure that when prompted for the file name, I did not misspell.

Ziezi
  • 6,375
  • 3
  • 39
  • 49
Jamie_Q
  • 21
  • 2
  • 11
  • When I get this error I first print the filename actually entered marked with "|" characters next to it. Many cases it is either a space or a newline in front of, or after, the name passed to open. – dascandy Oct 10 '15 at 11:50

2 Answers2

0

use getline (std::cin,name); for input name and use proper function of ostream for reading and writing.

you'r getting input wrong at line 21 and line 22

0

Here are few remarks that will help you figure out the possible problem:

1.You could reduce some lines of code by attaching your streams to a file during definition, instead of defining them and then use open, like so:

ifstream inputFile(testfile1.c_str());

2.To check if a file is open (and handle if it couldn't):

 if (!inputFile) error ("Can't open input file: ", testfile1);

and:

 if (!outputFile) error ("Can't open output file: ", sum);

right after the definition.

3.All open files are implicitly closed at the end of the program (or a function that contains them), so there is no need to explicitly close() them.

4.To read the contents of the input file and sum them:

int sum = 0;
string line;
// read a line
while (getline(inputFile, line)) {
    stringstream ss(line);
    // assuming you are reading integers separated by white space
    int num  = 0;
    // extract each number on the line
    while (ss >> num) total += num;
    // reset line
    line.erase();
}

Note: test and modify your code according to your specific needs. A side note: you could probably omit: cin.get(); in your code.

Ziezi
  • 6,375
  • 3
  • 39
  • 49