【问题标题】:Read the binary content of a file and reverse its content in C++读取文件的二进制内容并在 C++ 中反转其内容
【发布时间】:2016-04-28 12:44:08
【问题描述】:

我创建了一个简单的程序,可以逐行读取文本文件,然后反转其内容,我想用其他文件扩展名(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();
    }
 }

经过大量研究,我创建了一些函数“复制了一些代码:p”,可以执行以下操作: - 找到所有的硬盘分区。 - 按文件扩展名在驱动器中搜索 - 以二进制模式打开文件并反转其内容并保存反转的文件。

问题: * 我想将“getDrives”的结果发送到getFilelisting”函数 并打开找到的文件,将它们反转并保存。

我想做什么:

将“getDrives”结果发送到一个数组中,以便我可以在 for 循环中使用它。 将找到的文件发送到“file.open”,但它不起作用。 这是新代码,我希望有人可以帮助将所有功能链接在一起。

#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('\0') };

                           CHAR driveName[] = { TEXT('A') + i, TEXT('\0')  };

                           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()

【问题讨论】:

  • 你能发布你得到的错误吗?

标签: c++ file binary reverse


【解决方案1】:

尝试以二进制模式打开它,调用:

file.open("g:/example.txt", ios_base::in|ios_base::binary);

http://en.cppreference.com/w/cpp/io/basic_fstream/open

【讨论】:

  • 感谢解答,还是无法显示二进制数据:(
  • 你能给我一个实际的示例代码,可以读取加密的二进制文件吗?
  • OP 使用文本 io 方法,即使文件以二进制模式打开,这也不是可行的方法......
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-05-14
  • 2013-12-01
  • 2013-09-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多