读取文件的二进制内容并在C ++中反转其内容

时间:2016-04-28 12:44:08

标签: c++ file binary reverse

我创建了一个简单的程序,可以逐行读取文本文件,然后反转itss内容,我想与其他文件扩展名相同的东西,如(zip,rar,jpg,doc,pdf ...)我的真正意图是通过反转文件的二进制内容来创建文件“corrupter / de-corrupter”程序,因此无法通过acrobat或winzip等打开...

问题:

  • 当文件是简单文本时,我可以读取和打印所有行,当它是pdf或zip时,它只打印一两个乱码二进制数据,我怎样才能获得所有行,并打印出来?

  • CodeBlocks给我一个错误,从string转换为char有问题,如何解决这个问题?

这是我到目前为止所做的:

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

1 个答案:

答案 0 :(得分:1)

尝试以二进制模式打开它,所以请致电:

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

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