C++使用头文件复制文本文件程序
你好,我正在制作一个应该复制文本文件的程序。文本文件目前只有 3 行文本。 textFileCopy 函数应该读取由 filenamein 数组给出的文本文件,然后输出由 filenameout 数组给出的文本文件的副本。
这是我的 main.cpp 文件。在此文件中,程序要求用户发送输入文件名和输出文件名作为参数,这是我刚刚使用 Visual Studio 中的命令参数框完成的,命令参数框包含“input.txt output.txt”,所以这意味着argv[1] 包含输入文件,argv[2] 包含要创建的输出文件
#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;
}
这是声明 textFileCopy 函数的 FileUtilities.h 文件
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
#pragma once
class FileUtilities
{
public:
bool textFileCopy(char filenamein[], char filenameout[]);
};
这是匹配的 FileUtilities.cpp 文件,包括
#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;
}
我正在使用的 textFileCopy 函数在 .cpp 文件中创建 .h 文件中定义的函数时遇到问题,并且我从 .cpp 文件中收到此行 FileUtilities::textFileCopy(char filenamein[], char filenameout[])
的错误。我知道函数中的实际代码有效,它只是第一行
UPDATE
好的,所以我在函数之前添加了 bool 。
现在程序已编译,我在对话框中收到如下错误
“Microsoft Visual C++ 调试库
调试断言失败!
程序:.....Parser.exe 文件 f:\dd\vctools\crt_bld\Self_x86\crt\src\fopen.c 第 53 行
表达式:(file!=NULL)”
然后在 Visual Studio 中打开文件“dbghook.c”
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要将返回类型(bool)放在函数名称前面。
You need to put the return type (bool) in front of the function name.
当您尝试使用 NULL 文件名打开
ifstream
时,textFileCopy(false, false)
被隐式转换为textFilecopy(NULL, NULL)
,您得到了您所看到的断言。textFileCopy(false, false)
is being implicitly converted totextFilecopy(NULL, NULL)
and when you attempt to open anifstream
with a NULL filename, you get the assertion you're seeing.