用c++读写多个文件
read write multiple files in c++
我想从用户那里获得多个文件地址,然后在struct.txt文件和其他程序导出文件中以二进制模式读取和写入它们。请引导我。导入-合并导出-取消合并
#include <iostream>
#include <string>
#include <fstream>
#include <iterator>
#include <algorithm>
using namespace std;
int tedad_file=0; // get count of files from user //
int main()
{
int tr=0;
cout << "please enter count of files you want to merge ";
cin >> tedad_file;
cout << "n";
std::string *files[tedad_file];
int counter=0;
int temp=0;
for(;temp<tedad_file;temp++) // getting file's address from user and add them into arrays of string (files variable)
{
cout << "Lotfan address file " << temp + 1 << " vared konid: n";
cin >> *files[temp];
}
std::ofstream output_file( "D:\struct.txt", std::ios::binary ) ;
int x=0;
for(;x<tedad_file;x++) // for - read content of files to merge them into struct.txt ---- for example tedad_file is 3
{
std::ifstream first_file((char *)&files[tedad_file], std::ios::binary ) ;
output_file << first_file.rdbuf();
}
return 0;
}
您应该使用std::vector<std::string> files
和files.push_back(filename)
例如:
int main()
{
std::vector<std::string> files;
cout << "please enter count of files you want to merge ";
int file_count = 0;
cin >> file_count;
for (int temp = 0; temp<file_count; temp++)
{
cout << "Lotfan address file " << " vared konid: n";
std::string filename;
cin >> filename;
files.push_back(filename);
}
std::ofstream output_file("D:\struct.txt", std::ios::binary);
for (auto file : files)
{
std::ifstream fin(file, std::ios::binary);
output_file << fin.rdbuf();
}
return 0;
}
否则,请使用new
运算符。但new
方法容易出错。例如:
int main()
{
cout << "please enter count of files you want to merge ";
int tedad_file = 0;
cin >> tedad_file;
cout << "n";
std::string *files = new std::string[tedad_file];
for (int temp = 0; temp<tedad_file; temp++)
{
cout << "Lotfan address file " << " vared konid: n";
cin >> files[temp];
}
std::ofstream output_file("D:\struct.txt", std::ios::binary);
for (int temp = 0; temp<tedad_file; temp++)
{
std::ifstream first_file(files[temp], std::ios::binary);
output_file << first_file.rdbuf();
}
delete[]files;
return 0;
}
为什么要写这行:std::string *files[tedad_file];
?你必须先为这个指针分配内存。如果你不在这里使用指针,而是做一些类似于std::string files[tedad_file]
的事情,那会更好。您的代码没有为数组中的字符串分配内存,这就是出现segfault的原因。
通过这种方式解决
int tedad_file=0; // Daryaft tedad file jahate edgham //
int main()
{
int tr=0;
cout << "Lotfan Tedad file jahate edgham vared konid: ";
cin >> tedad_file;
cout << "n";
std::string files[tedad_file];
int counter=0;
int temp=0;
for(;temp<tedad_file;temp++)
{
cout << "Lotfan address file " << temp + 1 << " vared konid: n";
cin >> files[temp];
}
streampos size1;
std::ofstream output_file( "D:\test.txt", std::ios::binary ) ;
int x=0;
string dd;
for(;x<tedad_file;x++)
{
dd = files[x];
std::ifstream first_file(dd.c_str(), std::ios::binary ) ;
output_file << first_file.rdbuf();
size1 = first_file.tellg();
cout << size1;
}
return 0;
}
相关文章:
- 用C++快速读写文件
- C++中读/写二进制文件
- 为什么使用_access时只读测试对读写文件有效
- 谷歌测试读写同一文件失败
- UWP 是否有同步文件读/写 API
- 为什么SDL_RWFromFile(文件,"w+")不能像它应该的那样读写?它只打开文件进行写入
- 如何使用 Dlang 或 cpp 捕获和修改对文件的读写
- 如何读写结构向量到文件
- 读/写二进制文件返回0xcccccccccccc
- C++中大文件读/写的性能问题
- 读/写文件导致错误。(javaScript & C++)
- 逐行读取文件时,文件读写
- 调试读/写字符串到二进制文件
- 使用boost iostreams将数组读写到压缩文件中
- 用c++读写多个文件
- 提升内存映射文件:读写访问
- C++在g++和msvc之间不同的文件读/写时间
- 将C++文件读写转换为C#文件读写
- c++用二进制文件读/写对象
- 文件读写会产生valgrind错误