0

I'm trying to get information from an input file into an output file, which I have done, but I can't figure out how to get the numbers from the file into an array and only output the numbers divisible by 7. I've been stuck for a few hours, please help.

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

int main()
{
    string ifilename, ofilename, line;
    ifstream inFile, checkOutFile;
    ofstream outFile;
    char response;
// Input file
    cout << "Please enter the name of the file you wish to open : ";
    cin >> ifilename;
    inFile.open(ifilename.c_str());
    if (inFile.fail())
    {
        cout << "The file " << ifilename << " was not successfully  opened." << endl;
        cout << "Please check the path and name of the file. " << endl;
        exit(1);
    }
    else
    {
        cout << "The file is successfully opened." << endl;
    }
// Output file

    char array[10];
    int numberz = 0;

    cout << "Please enter the name of the file you wish to write : ";
    cin >> ofilename;
    checkOutFile.open(ofilename.c_str());
    if (!checkOutFile.fail())
    {
        cout << "A file " << ofilename << " exists.\nDo you want to continue and overwrite it? (y/n) : ";
        cin >> response;
        if (tolower(response) == 'n')
        {
            cout << "The existing file will not be overwritten. " << endl;
            exit(1);
        }
    }
    outFile.open(ofilename.c_str());
    if (outFile.fail())
    {
        cout << "The file " << ofilename << " was not successfully opened." << endl;
        cout << "Please check the path and name of the file. " << endl;
        exit(1);
    }
    else
    {
        cout << "The file is successfully opened." << endl;
    }
// Copy file contents from inFile to outFile
    while (getline(inFile, line))
    {
        fscanf(ifilename, &array[numberz]);
        cout << line << endl;
        outFile << line << endl;
    }
// Close files
    inFile.close();
    outFile.close();
} // main
user4581301
  • 33,082
  • 7
  • 33
  • 54
  • Defragmented your code and noticed a few things. Most important, it looks like you packing a `string` into `fscanf` where `fscanf` expects a pointer to a file handle. That's not going to fly. Recommend rethinking that bit because I'm not sure what you're trying to do. – user4581301 Apr 12 '17 at 00:58
  • Oh. Forgot the other thing: Not much point to calling `exit` in `main`. You can just `return ` and get all the RAII stack unrolling action working for you. – user4581301 Apr 12 '17 at 01:00
  • On your actual problem, break it down. First start by finding the numbers that are divisible by 7 and write the suckers to the screen. Once you know you have that down solid (look into using the `%` operator), then worry about writing out to the file. – user4581301 Apr 12 '17 at 01:02
  • Helpful reading: http://stackoverflow.com/questions/7868936/read-file-line-by-line – user4581301 Apr 12 '17 at 01:04
  • @user4581301 I just don't know how to get the numbers to an array, I added the 3rd line after //'copy file contents from inFile to outFile'. I'm sure i can figure out how to get only the numbers divisible by 7 to output, but I'm unable to get them to an array. – Tyler Rodriguez Apr 12 '17 at 01:29
  • What does your input data look like? – Paul Rooney Apr 12 '17 at 02:31

1 Answers1

0

I'd use boost::filesystem for that task.

#include <iostream>
#include <boost/filesystem.hpp>
#include <boost/filesystem/fstream.hpp>

namespace fs = boost::filesystem;

int main()
{
  fs::path ifilename;
  std::cout << "Input file: ";
  std::cin >> ifilename;

  fs::ifstream inFile{ifilename};
  if ( inFile.fail() )
    throw std::invalid_argument("Could not open file");

  fs::path ofilename;
  std::cout << "Output file: ";
  std::cin >> ofilename;

  if ( fs::exists(ofilename) )
  {
    std::cout << ofilename << " exists. Do you want to overwrite it? (y/n) ";
    char response;
    std::cin >> response;
    if ( ! ( response == 'y' || response == 'Y' ) )
      return 1;
  }

  fs::ofstream outFile{ofilename};
  if ( outFile.fail() )
    throw std::invalid_argument("Could not open file");

  std::vector<std::string> array;
  std::string line;
  while ( std::getline(inFile, line) )
  {
    array.push_back( line );
    std::cout << line << '\n';
    outFile << line << '\n';
  }

  inFile.close();
  outFile.close();
}
Henri Menke
  • 10,705
  • 1
  • 24
  • 42