读取文件的二进制内容并在 C++ 中反转其内容
Read the binary content of a file and reverse its content in C++
我创建了一个简单的程序,可以逐行读取文本文件,然后反转其内容,我想用其他文件扩展名做同样的事情,比如 (zip,rar,jpg,doc,pdf... ) 我的真实意图是创建一个文件"corrupter/de-corrupter"程序,通过反转文件的二进制内容,使其无法被acrobat或winzip等打开...
问题:
当文件是简单的文本时,我可以读取并打印所有行,当它是pdf或zip时,它只打印一两个乱码二进制数据,我怎么能得到所有的行,并打印出来?
CodeBlocks 报错,字符串转字符有问题,如何解决?
这是我目前拥有的:
#include <iostream>
#include <fstream>
#include <string>
#include <cstring>
using namespace std;
fstream file;
string line;
void reverseLine(char message[]);
void reverseLine(char message[])
{
char reversed;
int i = strlen(message) -1;
while (i>=0)
{
reversed = reversed + message[i];
i = i -1;
}
cout<<message<<endl;
cout<<reversed<<endl;
}
int main()
{
int numLines=0;
file.open("g:/example.txt");
if (!file.is_open())
{
cout<<"error in opening the file !"<<endl;
}
else
{
cout<<"success in opening the file !"<<endl;
cout<<"--------------------------------------"<<endl;
while (!file.eof()){
// for (int i=0; i<100; i++)
{
cout<<(getline(file,line))<<endl;
cout<<line<<endl;
//reverseLine(getline(file,line)); //<==dont work :(
numLines++;
}
}
cout<<numLines<<endl;
file.close();
}
}
经过大量研究,我创建了一些函数 "copied some code :p" 可以做到:
- 找到所有的硬盘分区。
- 按文件扩展名在驱动器中搜索
- 以二进制模式打开文件并反转其内容并保存反转的文件。
问题:
* 我想将“getDrives”的结果发送到“getFilelisting”函数
并打开找到的文件,反转它们并保存。
我尝试做的事情:
将 "getDrives" 结果发送到数组中,以便我可以在 for 循环中使用它。
将找到的文件发送到 "file.open" 但它不起作用。
这是新代码,我希望有人能帮助 link 功能一起。
#include <iostream>
#include <fstream>
#include <string>
#include <cstring>
#include <windows.h>
using namespace std;
fstream file; //file to open
fstream outFile; //the reversed file
string line; // read lines
int numLines=0; //number of lines read
void reverseLine() //fonction for reversing the line
{
while (!file.eof()) // read line by line untile the end of the file
{
string reversed;
getline(file,line);
int i = line.length() -1;
while (i>=0)
{
reversed = reversed + line[i];
i = i -1;
if (i < 0)
{
outFile<<reversed<<endl;
}
}
//numLines++;
}
}
char getDrives() //find partitions.
{
DWORD drives = GetLogicalDrives();
for (int i=0; i<26; i++)
{
if( ( drives & ( 1 << i ) ) )
{
//CHAR driveName[] = { TEXT('A') + i, TEXT(':'), TEXT('\'), TEXT('[=11=]') };
CHAR driveName[] = { TEXT('A') + i, TEXT('[=11=]') };
cout<<driveName<<endl;
}
}
}
void GetFileListing(string directory, string fileFilter, bool recursively = true) // fonction that searches in a drive by one file type, I want it to search for all drives with multiple file extensions
{
if (recursively)
GetFileListing(directory, fileFilter, false);
directory = directory + "\";
WIN32_FIND_DATA FindFileData;
HANDLE hFind = INVALID_HANDLE_VALUE;
string filter = directory + (recursively ? "*" : fileFilter);
hFind = FindFirstFile(filter.c_str(), &FindFileData);
if (hFind == INVALID_HANDLE_VALUE)
{
return ;
}
else
{
if (!recursively)
{
cout << directory + string(FindFileData.cFileName) << endl;
}
while (FindNextFile(hFind, &FindFileData) != 0)
{
if (!recursively)
{
cout << directory + string(FindFileData.cFileName) << endl;
}
else
{
if ((FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)>0 && FindFileData.cFileName[0]!='.')
{
GetFileListing(directory + string(FindFileData.cFileName), fileFilter);
}
}
}
DWORD dwError = GetLastError();
FindClose(hFind);
if (dwError != ERROR_NO_MORE_FILES)
{
cout << "FindNextFile error. Error is "<< dwError << endl;
}
}
}
int main(int argc, char* argv[])
{
getDrives();
//char allDrives = getDrives(); // dont work
//cout<<"number of drives are:" <<sizeof(allDrives)<<endl;
//cout<<allDrives<<endl;
//string fileToOpen;
//GetFileListing("e:\", "*.txt "); // works but for only on drive with on extension
fstream foundFile; // file found by GetFileListing
//foundFile.open("e:/foundFiles.txt",fstream::out ); // dont work
// foundFile<<(GetFileListing( "e:\", "*.stl ")); // dont work
// file.open(GetFileListing( "e:\", "*.3ds")); //dont work , open files found by GetFileListing
//file.open(fileToOpen,ios_base::in |ios_base::binary| ios_base::out); //ne marche pas
//GetFileListing("e:\", "*.txt "); // dont work
file.open("e:/test.txt", ios_base::in |ios_base::binary| ios_base::out);
outFile.open("e:/test_m.txt", ios_base::binary | fstream::out);
if ( !file.is_open() )
{
cout<<"error in opening the file !"<<endl;
cout<<"--------------------------------------"<<endl;
}
else
{
cout<<"success in opening the file and outFile !"<<endl;
cout<<"--------------------------------------"<<endl;
while (!file.eof())
{
// for (int i=0; i<300; i++) // dont work, i want to read the 300 lines only
string reversed; // line found
getline(file,line);
int i = line.length() -1;
while (i>=0)
{
reversed = reversed + line[i];
i = i -1;
//cout<<line<<endl; // print found line
if (i < 0)
{
//cout<<reversed<<endl;
outFile<<reversed<<endl; //print the reversed line in the output file
}
} // end while
numLines++;
} // end while
cout<<"number of lines in the original file are :" << numLines <<endl;
// file.close();
// std::remove("e:/lines.txt"); //delete the original file
outFile.close();
} // end else
} //end main()
尝试以二进制方式打开,调用:
file.open("g:/example.txt", ios_base::in|ios_base::binary);
我创建了一个简单的程序,可以逐行读取文本文件,然后反转其内容,我想用其他文件扩展名做同样的事情,比如 (zip,rar,jpg,doc,pdf... ) 我的真实意图是创建一个文件"corrupter/de-corrupter"程序,通过反转文件的二进制内容,使其无法被acrobat或winzip等打开...
问题:
当文件是简单的文本时,我可以读取并打印所有行,当它是pdf或zip时,它只打印一两个乱码二进制数据,我怎么能得到所有的行,并打印出来?
CodeBlocks 报错,字符串转字符有问题,如何解决?
这是我目前拥有的:
#include <iostream>
#include <fstream>
#include <string>
#include <cstring>
using namespace std;
fstream file;
string line;
void reverseLine(char message[]);
void reverseLine(char message[])
{
char reversed;
int i = strlen(message) -1;
while (i>=0)
{
reversed = reversed + message[i];
i = i -1;
}
cout<<message<<endl;
cout<<reversed<<endl;
}
int main()
{
int numLines=0;
file.open("g:/example.txt");
if (!file.is_open())
{
cout<<"error in opening the file !"<<endl;
}
else
{
cout<<"success in opening the file !"<<endl;
cout<<"--------------------------------------"<<endl;
while (!file.eof()){
// for (int i=0; i<100; i++)
{
cout<<(getline(file,line))<<endl;
cout<<line<<endl;
//reverseLine(getline(file,line)); //<==dont work :(
numLines++;
}
}
cout<<numLines<<endl;
file.close();
}
}
经过大量研究,我创建了一些函数 "copied some code :p" 可以做到: - 找到所有的硬盘分区。 - 按文件扩展名在驱动器中搜索 - 以二进制模式打开文件并反转其内容并保存反转的文件。
问题: * 我想将“getDrives”的结果发送到“getFilelisting”函数 并打开找到的文件,反转它们并保存。
我尝试做的事情:
将 "getDrives" 结果发送到数组中,以便我可以在 for 循环中使用它。 将找到的文件发送到 "file.open" 但它不起作用。 这是新代码,我希望有人能帮助 link 功能一起。
#include <iostream>
#include <fstream>
#include <string>
#include <cstring>
#include <windows.h>
using namespace std;
fstream file; //file to open
fstream outFile; //the reversed file
string line; // read lines
int numLines=0; //number of lines read
void reverseLine() //fonction for reversing the line
{
while (!file.eof()) // read line by line untile the end of the file
{
string reversed;
getline(file,line);
int i = line.length() -1;
while (i>=0)
{
reversed = reversed + line[i];
i = i -1;
if (i < 0)
{
outFile<<reversed<<endl;
}
}
//numLines++;
}
}
char getDrives() //find partitions.
{
DWORD drives = GetLogicalDrives();
for (int i=0; i<26; i++)
{
if( ( drives & ( 1 << i ) ) )
{
//CHAR driveName[] = { TEXT('A') + i, TEXT(':'), TEXT('\'), TEXT('[=11=]') };
CHAR driveName[] = { TEXT('A') + i, TEXT('[=11=]') };
cout<<driveName<<endl;
}
}
}
void GetFileListing(string directory, string fileFilter, bool recursively = true) // fonction that searches in a drive by one file type, I want it to search for all drives with multiple file extensions
{
if (recursively)
GetFileListing(directory, fileFilter, false);
directory = directory + "\";
WIN32_FIND_DATA FindFileData;
HANDLE hFind = INVALID_HANDLE_VALUE;
string filter = directory + (recursively ? "*" : fileFilter);
hFind = FindFirstFile(filter.c_str(), &FindFileData);
if (hFind == INVALID_HANDLE_VALUE)
{
return ;
}
else
{
if (!recursively)
{
cout << directory + string(FindFileData.cFileName) << endl;
}
while (FindNextFile(hFind, &FindFileData) != 0)
{
if (!recursively)
{
cout << directory + string(FindFileData.cFileName) << endl;
}
else
{
if ((FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)>0 && FindFileData.cFileName[0]!='.')
{
GetFileListing(directory + string(FindFileData.cFileName), fileFilter);
}
}
}
DWORD dwError = GetLastError();
FindClose(hFind);
if (dwError != ERROR_NO_MORE_FILES)
{
cout << "FindNextFile error. Error is "<< dwError << endl;
}
}
}
int main(int argc, char* argv[])
{
getDrives();
//char allDrives = getDrives(); // dont work
//cout<<"number of drives are:" <<sizeof(allDrives)<<endl;
//cout<<allDrives<<endl;
//string fileToOpen;
//GetFileListing("e:\", "*.txt "); // works but for only on drive with on extension
fstream foundFile; // file found by GetFileListing
//foundFile.open("e:/foundFiles.txt",fstream::out ); // dont work
// foundFile<<(GetFileListing( "e:\", "*.stl ")); // dont work
// file.open(GetFileListing( "e:\", "*.3ds")); //dont work , open files found by GetFileListing
//file.open(fileToOpen,ios_base::in |ios_base::binary| ios_base::out); //ne marche pas
//GetFileListing("e:\", "*.txt "); // dont work
file.open("e:/test.txt", ios_base::in |ios_base::binary| ios_base::out);
outFile.open("e:/test_m.txt", ios_base::binary | fstream::out);
if ( !file.is_open() )
{
cout<<"error in opening the file !"<<endl;
cout<<"--------------------------------------"<<endl;
}
else
{
cout<<"success in opening the file and outFile !"<<endl;
cout<<"--------------------------------------"<<endl;
while (!file.eof())
{
// for (int i=0; i<300; i++) // dont work, i want to read the 300 lines only
string reversed; // line found
getline(file,line);
int i = line.length() -1;
while (i>=0)
{
reversed = reversed + line[i];
i = i -1;
//cout<<line<<endl; // print found line
if (i < 0)
{
//cout<<reversed<<endl;
outFile<<reversed<<endl; //print the reversed line in the output file
}
} // end while
numLines++;
} // end while
cout<<"number of lines in the original file are :" << numLines <<endl;
// file.close();
// std::remove("e:/lines.txt"); //delete the original file
outFile.close();
} // end else
} //end main()
尝试以二进制方式打开,调用:
file.open("g:/example.txt", ios_base::in|ios_base::binary);