I created a simple program that can read a text file line by line, then reverse itss content, I want to make the same thing with other file extension like (zip,rar,jpg, doc, pdf...) My real intention is to create a file "corrupter/de-corrupter" program, by reversing the binary content of the file, so it can not be opened by acrobat or winzip etc...
Questions:
When the file is simple text, I can read and print all the lines, when it is a pdf or zip, it only prints a word or two of gibberish binary data, how can I get all the lines, and print them?
CodeBlocks gives me an error, there is a problem with converting from string to char, how to fix this problem?
This is what I have so far:
#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();
}
}
After a lot of research, I created some functions "copied some code :p" that can do: - find all the hard drives partitions. - search in a drive by file extension - open file in binary mode and reverse its content and save the reversed file.
The problem: * I want to send the result of " getDrives" to the getFilelisting" function and open the files found, reverse them and save after.
what I tried to do:
send the "getDrives" result into an array so I can use it in a for loop. send the found file into the "file.open" but It does not work. Here is the new code, I hope someone can help link the functions all together.
#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('') };
CHAR driveName[] = { TEXT('A') + i, TEXT('') };
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()
Aucun commentaire:
Enregistrer un commentaire