[英]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;
}
现在,我必须做同样的事情,但要使用“ BINARY READ AND WRITE”,但我的输出文件仍应是txt文件,我该怎么做,有人可以解释一下这是什么意思,因为我有点困惑!
二进制读写只是为了支持plateform-independent
的读写支持。 例如,在Windows平台上, New Line
为\\r\\n
但在基于UNIX的平台上,它仅为\\n
。 因此,当我们以二进制模式读取字符时,将按原样读取字符。 但是,如果您以非二进制模式读取它,那么将根据需要修改字符。
要以二进制模式打开文件,请使用ios::binary
标志。
ofstream myfile;
myfile.open ("example.bin", ios::out | ios::app | ios::binary);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.