C++ copy text file program using header file
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 arg开发者_JS百科ument 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
You need to put the return type (bool) in front of the function name.
bool FileUtilities::textFileCopy(char filenamein[], char filenameout[])
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.
精彩评论