【发布时间】:2019-11-06 06:28:20
【问题描述】:
我正在尝试将一个文件的内容复制到 linux 中的另一个文件。 我认为我的逻辑是正确的,但我不明白错误是什么。
我的函数有 3 个参数。第三个参数是一个字符串,它是应该从中读取内容的文件名。
#include<iostream>
#include <curses.h>
#include<fstream>
#include<stdio.h>
#include<stdlib.h>
#include<string>
void process(int cvar, int cclause, string fnm)
{
ifstream fs;
ofstream ft;
fs.open("contents.txt");
if(!fs)
{
cout<<"Error in opening source file..!!";
}
ft.open(fnm,ios::app);
if(!ft)
{
cout<<"Error in opening target file..!!";
fs.close();
}
char str[255];
while(fs.getline(str,255))
{
ft<<str;
}
cout<<"File copied successfully..!!";
fs.close();
ft.close();
getch();
}
这是我得到的错误:
g++ mainProj.cpp -lz3
/tmp/ccLBpiRs.o: In function `process(int, int, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)':
mainProj.cpp:(.text+0x172): undefined reference to `stdscr'
mainProj.cpp:(.text+0x17a): undefined reference to `wgetch'
collect2: error: ld returned 1 exit status
【问题讨论】:
-
顺便说一句,你说你认为你的逻辑是正确的,但你也说第三个参数是要读取的文件名(据说),而你的代码显然是附加到它的。跨度>
-
顺便说一句,复制文件的一种更快、更有效的方法是以二进制模式打开它们并使用
istream::read和ostream::write和uint8_t数组。复制文件的最佳技术是让操作系统为您复制文件。 -
你有使用 istream::read 和 ostream::write 的示例程序吗?