使用二进制读取和写入连接文件
Concatenate files using the binary read and write
我编写了一个程序,该程序需要n个文件,然后用户将输入他们的文件名,然后我的程序会将所有这些文件连接成1个由换行符分隔的文件,这是我的程序(工作正常):
#include <iostream>
#include <string>
#include <fstream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
using namespace std;
int main()
{
int n;
cin>>n;
ifstream read;
ofstream finalout;
int i;
finalout.open("concatn.txt");
if(!finalout)
{
cout << "Oops something went wrong, SORRY DUDE" << endl;
}
char a[n][50];
char help[50];
for(i=0; i<n; i++)
{
scanf("%s", help);
strcpy(a[i], help);
}
string STRING;
for(i=0; i<n; i++)
{
read.open(a[i]);
if(read == NULL)
{
cout << "Could not open the file, SORRY DUDE" << endl;
}
while(read.eof() == 0)
{
getline(read, STRING);
finalout << STRING;
}
finalout << "n";
read.close();
read.clear();
}
finalout.close();
return 0;
}
现在我必须做同样的事情,但使用"二进制读写",但我的输出文件应该仍然是 txt 文件,我该怎么做,有人可以解释这到底是什么意思,因为我有点困惑!?
二进制读写只是为了支持plateform-independent
读写支持。例如,在Windows平台上New Line
是rn
,但对于基于UNIX的平台,它只是n
。因此,当我们以二进制模式读取时,字符将按原样读取。但是,如果您在非二进制模式下读取它,那么如果需要,字符将被修改。
要在二进制模式下打开文件,请使用ios::binary
标志。
ofstream myfile;
myfile.open ("example.bin", ios::out | ios::app | ios::binary);
更多信息请看这里
相关文章:
- 在 c++ 中连接字符串和整数,以便在 C++ 11 不支持计算机的情况下读取多个文件
- 连接资源文件中的字符串,并将其传递给菜单项字符串 (winapi) c++
- 将文本和二进制数据连接到一个文件中
- 头文件和库文件连接
- 连接两个文件时如何避免冗余尾随换行符?
- GLM如何在不声明内联函数并在另一个(未连接的?)文件中将其定义的函数逃脱
- 似乎无法将我的标题文件与我的CPP文件连接到链接列表中
- 使用二进制读取和写入连接文件
- 连接QT中的多个.UI文件
- 我如何连接文件路径
- 头文件连接
- COM 客户端连接 COM 服务器所需的文件是什么(进程外方案)
- C++应用程序连接到网站服务器并根据密码检索文件
- 头文件如何连接到相应的.cpp文件?
- 通过终端接收 3 个文件,并使用 fstream 将 file2 和 file3 的内容连接到文件 1
- Linux复制文件连接到节点JS / Python / C / C 之后
- 如何连接两个文件夹/项目的生成文件,这两个文件夹/项目必须一起运行且独立运行
- C++返回对成员的连接文件,因此可以由同一类的其他方法使用
- 在 Mac 上使用 boost::文件系统连接文件路径时程序崩溃
- 连接C ++文件和Gnuplot并在执行后自动绘图