0
votes

Hello i am making a program that is supposed to copy a text file. The text file is just 3 lines of text at the moment. The textFileCopy function is supposed to read in a text file given by the filenamein array and then output a copy of the text file given by the filenameout array.

This is my main.cpp file. In this file the program requires the user to send an input filename and an output filename as arguments which i have just done using the command argument box in visual studio, the command argument box contains "input.txt output.txt" so this means that argv[1] contains the input file and argv[2] contains the output file which is to be created

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

#include "FileUtilities.h"


int main(int argc, char **argv) {
    FileUtilities fileUtil;
    fileUtil.textFileCopy(false, false);
    if (argc !=3) {
        cerr << "Usage: " << argv[0] << " <input filename> <output filename>" << endl;
        int keypress; cin >> keypress;
        return -1;
    }

    fileUtil.textFileCopy(argv[1], argv[2]);

    int keypress; cin >> keypress;
}

And this is the FileUtilities.h file which declares the textFileCopy function

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

class FileUtilities
{
public:
    bool textFileCopy(char filenamein[], char filenameout[]);
}; 

And here is the matching FileUtilities.cpp file including textFileCopy function

#include <iostream>
#include <fstream>
#include <string>

#include "FileUtilities.h"

bool FileUtilities::textFileCopy(char filenamein[], char filenameout[])
{
    ifstream fin(filenamein);
    if(fin.is_open())
    {
        ofstream fout(filenameout);

        char c;
        while(fin.good())
        {
            fin.get(c);
            fout << c;
        }

        fout.close();
        fin.close();

        return true;
    }
    return false;
}

I am having trouble creating the function defined in the .h file in the .cpp file and i get errors with this line FileUtilities::textFileCopy(char filenamein[], char filenameout[]) from the .cpp file. I know the actual code in the function works its just that first line

UPDATE

Ok so i have put bool before the function.

Now the program compiles and i get an error in a dialog box as follows

"Microsoft Visual C++ Debug Library

Debug Assertion Failed!

Program: .....Parser.exe file f:\dd\vctools\crt_bld\Self_x86\crt\src\fopen.c Line 53

Expression: (file!=NULL)"

then it opens a file "dbghook.c" in visual studio

2
You could post the error you're getting and it would be easier to diagnose your problem. One obvious problem is that you're missing the bool return type. - Pablo
Please don't put using namespace in a header, it makes your code less reusable. - Fred Foo
You have another question. Put it in another post. - Benjamin Lindley

2 Answers

3
votes

You need to put the return type (bool) in front of the function name.

bool FileUtilities::textFileCopy(char filenamein[], char filenameout[])
0
votes

textFileCopy(false, false) is being implicitly converted to textFilecopy(NULL, NULL) and when you attempt to open an ifstream with a NULL filename, you get the assertion you're seeing.