在 C++ 中读取非文本文件
reading non-text file in c++
我用notepad++(打开方式)错误地打开了mp3文件,然后在记事本中以文本形式显示了整个文件,真是太酷了。
因为我又在学习 C++,所以我告诉自己让我们编写一个程序来打开控制台中的任何文件并在控制台上显示它们的内容,所以我开始这样写代码:
int readAndWrite() {
string filename(R"(path\to\a\file)");
ifstream file(filename);
string line;
if (!file.is_open()) {
cerr << "Could not open the file - '"
<< filename << "'" << endl;
return EXIT_FAILURE;
}
while (getline(file, line)){
cout << line;
}
return EXIT_SUCCESS;
}
但它只显示文件的 3 或 4 行,然后退出程序我再次检查我的记事本++,发现里面有大约 700,000 行。
我告诉自己文件中可能有一个字符,所以我开始编写上面的代码并进行以下更改。让我们在文本文件中写入而不是显示文件。
int readAndWrite() {
string filename(R"(path\to\a\file)");
string filename2(R"(path\to\a\file\copy)");
ifstream file(filename);
ofstream copy(filename2);
string line;
if (!file.is_open()) {
cerr << "Could not open the file - '"
<< filename << "'" << endl;
return EXIT_FAILURE;
}
while (getline(file, line)){
copy << line;
}
return EXIT_SUCCESS;
}
同样的结果。下次尝试我放弃逐行读取文件,所以我开始使用此功能进行复制。
void copyStringNewFile(ifstream& file, ofstream& copy)
{
copy << file.rdbuf();
}
他们的成绩没有一点变化。
在这一点上,我告诉自己问题可能出在文件中,这有点是因为当我使用一个简单的文本文件时,上述所有代码都有效。
与所有其他非文本文件一样,mp3
文件不包含 行 ,因此您不应使用 std::getline
。使用 istream::read
and ostream::write
. You can use istream::gcount
检查实际读取了多少个字符。
由于您处理的是非文本文件,因此也可以在 binary
模式下打开文件。
您还应该测试打开两个文件是否有效 - 即输入文件和输出文件。
示例:
#include <cerrno>
#include <cstring>
#include <fstream>
#include <iostream>
int readAndWrite() {
std::string filename(R"(path\to\a\file)");
std::string filename2(R"(path\to\a\file_copy)");
std::ifstream file(filename, std::ios::binary);
if(!file) {
std::cerr << '\'' << filename << "': " << std::strerror(errno) << '\n';
return EXIT_FAILURE;
}
std::ofstream copy(filename2, std::ios::binary);
if(!copy) {
std::cerr << '\'' << filename2 << "': " << std::strerror(errno) << '\n';
return EXIT_FAILURE;
}
char buf[1024];
while(file) {
file.read(buf, sizeof(buf));
// write as many characters as was read above
if(!copy.write(buf, file.gcount())) {
// write failed, perhaps filesystem is full?
std::cerr << '\'' << filename2 << "': " << std::strerror(errno) << '\n';
return EXIT_FAILURE;
}
}
return EXIT_SUCCESS;
}
int main() {
return readAndWrite();
}
我用notepad++(打开方式)错误地打开了mp3文件,然后在记事本中以文本形式显示了整个文件,真是太酷了。 因为我又在学习 C++,所以我告诉自己让我们编写一个程序来打开控制台中的任何文件并在控制台上显示它们的内容,所以我开始这样写代码:
int readAndWrite() {
string filename(R"(path\to\a\file)");
ifstream file(filename);
string line;
if (!file.is_open()) {
cerr << "Could not open the file - '"
<< filename << "'" << endl;
return EXIT_FAILURE;
}
while (getline(file, line)){
cout << line;
}
return EXIT_SUCCESS;
}
但它只显示文件的 3 或 4 行,然后退出程序我再次检查我的记事本++,发现里面有大约 700,000 行。 我告诉自己文件中可能有一个字符,所以我开始编写上面的代码并进行以下更改。让我们在文本文件中写入而不是显示文件。
int readAndWrite() {
string filename(R"(path\to\a\file)");
string filename2(R"(path\to\a\file\copy)");
ifstream file(filename);
ofstream copy(filename2);
string line;
if (!file.is_open()) {
cerr << "Could not open the file - '"
<< filename << "'" << endl;
return EXIT_FAILURE;
}
while (getline(file, line)){
copy << line;
}
return EXIT_SUCCESS;
}
同样的结果。下次尝试我放弃逐行读取文件,所以我开始使用此功能进行复制。
void copyStringNewFile(ifstream& file, ofstream& copy)
{
copy << file.rdbuf();
}
他们的成绩没有一点变化。 在这一点上,我告诉自己问题可能出在文件中,这有点是因为当我使用一个简单的文本文件时,上述所有代码都有效。
与所有其他非文本文件一样,mp3
文件不包含 行 ,因此您不应使用 std::getline
。使用 istream::read
and ostream::write
. You can use istream::gcount
检查实际读取了多少个字符。
由于您处理的是非文本文件,因此也可以在 binary
模式下打开文件。
您还应该测试打开两个文件是否有效 - 即输入文件和输出文件。
示例:
#include <cerrno>
#include <cstring>
#include <fstream>
#include <iostream>
int readAndWrite() {
std::string filename(R"(path\to\a\file)");
std::string filename2(R"(path\to\a\file_copy)");
std::ifstream file(filename, std::ios::binary);
if(!file) {
std::cerr << '\'' << filename << "': " << std::strerror(errno) << '\n';
return EXIT_FAILURE;
}
std::ofstream copy(filename2, std::ios::binary);
if(!copy) {
std::cerr << '\'' << filename2 << "': " << std::strerror(errno) << '\n';
return EXIT_FAILURE;
}
char buf[1024];
while(file) {
file.read(buf, sizeof(buf));
// write as many characters as was read above
if(!copy.write(buf, file.gcount())) {
// write failed, perhaps filesystem is full?
std::cerr << '\'' << filename2 << "': " << std::strerror(errno) << '\n';
return EXIT_FAILURE;
}
}
return EXIT_SUCCESS;
}
int main() {
return readAndWrite();
}