Benefit
Benefit

Reputation: 19

Create a output file from file stream

I am fairly new to c++ and yes this is a homework assignment. I am considering a switch statement instead of the if else statements in my function.

I am trying to write information read from an input filestream, to an output file after the data has been manipulated.

The prgram is supposed to read information from a file, process it and then display the data on the console and also write the result to an output file, the program should ask the user to enter the filename for both input and output files.

I cannot get my program to create a file. It works with files that already exist though.

Please help me with getting my program to create a file if it does not exist.

Oh and this is in c++

Any help will be greatly appreciated.

MY CODE :

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
#include <sstream>

using namespace std;

char getNumber(char l);

int main ()
{
     string s1 = "D:\\Unisa\\Assignment_stuffs\\COS1512\\Assignment\\";
     string inFile, outFile;

     cout << " Please enter the input filename: ";
     cin >> inFile;
     cout << "\nPlease enter the output filename: ";
     cin >> outFile;

     string inFileAdd = s1 + inFile;
     string inFileAdd2 = s1 + outFile;

     ifstream in_stream;
     ofstream out_stream;
     in_stream.open(inFileAdd.c_str(), ios::in);
     if (in_stream.fail())
     {
        cout << "Error!! Input file opening failed.";
        exit(1);
     }
     out_stream.open(inFileAdd2.c_str(), ios::out);
     if (out_stream.fail())
     {
        cout << "Error!! Output file opening failed.";
        exit(1);
     }   

     char next = ' ';
     string letter;

          while (!in_stream.eof()) 
          {
                in_stream.get(next);
                while (next != '\n')
                {
                        cout << next;
                        out_stream.put(next);
                        letter = letter + getNumber(next);
                        in_stream.get(next); 
                }
               cout << " " + letter;
               out_stream << " " + letter << endl;

               letter = "";
               cout << endl;
          }


     in_stream.close();

    return 0;
}

char getNumber(char l)
{
     if ((l == 'A') || (l == 'a') || (l == 'B') || (l == 'b') || (l == 'C') || (l == 'c'))
     {
            return '2';
     }
     else if ((l == 'D') || (l == 'd') || (l == 'E') || (l == 'e') || (l == 'F') || (l == 'f'))
     {
          return '3';
     }
     else if ((l == 'G') || (l == 'g') || (l == 'H') || (l == 'h') || (l == 'I') || (l == 'i'))
     {
          return '4';
     }
     else if ((l == 'J') || (l == 'j') || (l == 'K') || (l == 'k') || (l == 'L') || (l == 'l'))
     {
          return '5';
     }
     else if ((l == 'M') || (l == 'm') || (l == 'N') || (l == 'n') || (l == 'O') || (l == 'o'))
     {
          return '6';
     }
     else if ((l == 'P') || (l == 'p') || (l == 'Q') || (l == 'q') || (l == 'R') || (l == 'r') || (l == 'S') || (l == 's'))
     {
          return '7';
     }
     else if ((l == 'T') || (l == 't') || (l == 'U') || (l == 'u') || (l == 'V') || (l == 'v'))
     {
          return '8';
     }
     else if ((l == 'W') || (l == 'w') || (l == 'X') || (l == 'x') || (l == 'Y') || (l == 'y') || (l == 'Z') || (l == 'z'))
     {
          return '9';
     }
}

Upvotes: 1

Views: 4194

Answers (2)

Abdullah Danial
Abdullah Danial

Reputation: 1

Create a class called "FileMonitor" that has a private member variable for the filename and a public method "monitorFile" that continuously checks if the contents of the file have been modified and performs some action if they have

Upvotes: 0

nckturner
nckturner

Reputation: 1266

Do what Beta said and create a minimal file opening and writing program first. Get that working. Then build the rest of the needed functionality around that, compiling and fixing errors every step of the way. If you can get this working, add a little bit of your desired functionality to it. Look into permissions on the directory you are writing to if this doesn't work. This compiles in visual studio, you may need to include other libraries on linux/unix/mac:

#include <fstream>
int main()
{
    std::ofstream file;

    file.open("file.txt");      //open a file

    file<<"Hello file\n";       //write to it

    file.close();           //close it
}

Upvotes: 1

Related Questions