C++ 如何在Ubuntu中将内容从一个文件传输到另一个文件。?
我正在尝试在linux中将一个文件的内容复制到另一个文件。 我认为我的逻辑是正确的,但我不明白错误是什么 我的函数有3个参数。第三个参数是一个字符串,它是应该从中读取内容的文件名C++ 如何在Ubuntu中将内容从一个文件传输到另一个文件。?,c++,file,C++,File,我正在尝试在linux中将一个文件的内容复制到另一个文件。 我认为我的逻辑是正确的,但我不明白错误是什么 我的函数有3个参数。第三个参数是一个字符串,它是应该从中读取内容的文件名 #include<iostream> #include <curses.h> #include<fstream> #include<stdio.h> #include<stdlib.h> #include<string> void process(
#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();
}
#包括
#包括
#包括
#包括
#包括
#包括
无效流程(内部cvar、内部cclause、字符串fnm)
{
IFFS;
流ft;
fs.open(“contents.txt”);
如果(!fs)
{
不能包含
并链接到-lncurse
更多
如何在Ubuntu中将内容从一个文件传输到另一个文件
您可以使用输入流读取文件,使用输出流写入文件
您包含了一个标题
,并使用了其中声明的函数,但未能链接到定义这些函数的库
如何在Ubuntu中将内容从一个文件传输到另一个文件
下面是一个简单高效的代码段。有更高效的方法:
#include <iostream>
#include <fstream>
#include <string>
void copy_file(const std::string& source_filename, const std::string& destination_filename)
{
std::ifstream input(source_filename.c_str(), "b");
std::ofstream output(destination_filename.c_str(), "b");
const size_t BUFFER_SIZE = 1024 * 16;
static uint8_t buffer[BUFFER_SIZE];
while (input.read(buffer, BUFFER_SIZE))
{
const size_t bytes_read = input.gcount();
output.write(buffer, bytes_read);
}
}
#包括
#包括
#包括
无效复制文件(常量标准::字符串和源文件名,常量标准::字符串和目标文件名)
{
std::ifstream输入(source_filename.c_str(),“b”);
std::of流输出(destination_filename.c_str(),“b”);
const size\u t BUFFER\u size=1024*16;
静态uint8_t缓冲区[缓冲区大小];
while(input.read(buffer,buffer_SIZE))
{
const size_t bytes_read=input.gcount();
输出。写入(缓冲区、字节和读取);
}
}
上面的代码使用大缓冲区。文件内容被读取(使用二进制模式)到缓冲区,然后使用块I/O写入另一个文件。文件是流设备,在传输{large}时效率最高数据块。顺便说一句,您说您认为您的逻辑是正确的,但您也说第三个参数是要读取的文件名(假定)顺便说一句,复制文件的一种更快、更有效的方法是以二进制模式打开文件,并使用
istream::read
和ostream::write
和uint8_t
数组。复制文件的最佳方法是让操作系统为您复制文件。您有没有复制文件的示例使用istream::read和ostream::write的图形?
mainProj.cpp:(.text+0x172): undefined reference to `stdscr'
mainProj.cpp:(.text+0x17a): undefined reference to `wgetch'
#include <iostream>
#include <fstream>
#include <string>
void copy_file(const std::string& source_filename, const std::string& destination_filename)
{
std::ifstream input(source_filename.c_str(), "b");
std::ofstream output(destination_filename.c_str(), "b");
const size_t BUFFER_SIZE = 1024 * 16;
static uint8_t buffer[BUFFER_SIZE];
while (input.read(buffer, BUFFER_SIZE))
{
const size_t bytes_read = input.gcount();
output.write(buffer, bytes_read);
}
}