Here is the minimal reproducible code:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
const string TEST_FILE_PATH = "output.txt";
void write_to_file(string path, string line);
void generate_passwords(string file);
int main()
{
generate_passwords(TEST_FILE_PATH);
return 0;
}
void generate_passwords(string file)
{
int count = 1;
for (int len = 1; len <= 100; len++)
{
for (int i = 0; i < 100; i++)
{
write_to_file(file, to_string(count) + " : ");
count++;
}
}
}
void write_to_file(string path, string line)
{
ofstream file;
file.open(path, ios::out | ios::app);
cout << "writing line : " << line << endl;
if (file.is_open())
{
cout << "file is open " << endl;
file << line << '\n';
cout << "written..." << endl;
}
else {
cout << "FILE NOT OPEN" << endl;
}
file.close();
}
This is a stripped-down version of my full code, and it reproduces the same error.
With this code, I expect exactly 10,000 lines to be written to output.txt. However, it sometimes skips a random number of lines:
See how it skips numbers 6466 to 6509.
The console prints "FILE NOT OPEN" for these numbers.
This rarely happens with fewer iterations, but occurs very often with large numbers of iterations.
What could be the issue?
I'm writing a simple encryption program in C++ to encrypt a text-based file.
It's using a simple XOR cipher algorithm, but this produces ASCII control characters in the output file. When I try to read from the newly encrypted file with std::ifstream, it stumbles upon character #26, it stops and becomes unable to read the rest of the file.
Example if I try to encrypt this text:
This is just a simple sample
text with two rows and one sentence.
It turns it to this
/[[[[[
[[[ [[[U
When I try to read that file in my program, it can't read past the character at position 15, so I get a half encrypted file.
How can I fix this?
Here's the code:
#include <iostream>
#include <Windows.h>
#include <string>
#include <fstream>
void Encrypt(char encryptionKey, std::string filename)
{
std::ifstream sourceFile(filename);
std::ofstream outputFile(filename.substr(0, filename.find_last_of("\\")) + "\\Encrypted" + filename.substr(filename.find_last_of("\\") + 1), std::ofstream::out | std::ofstream::trunc);
std::string sourceLine;
std::string outputLine;
long numLines = 0;
if (sourceFile.is_open())
{
std::cout << "Opening file: " + filename + " for encryption" << std::endl;
while (sourceFile.good()) // This iterates over the whole file, once for each line
{
sourceLine = ""; //Clearing the line for each new line
outputLine = ""; //Clearing the line for each new line
std::getline(sourceFile, sourceLine);
for (int i = 0; i < sourceLine.length(); i++) // Looping through all characters in each line
{
char focusByte = sourceLine[i] ^ encryptionKey;
std::cout << " focusByte: " << focusByte << std::endl;
outputLine.push_back(focusByte);
//std::cout << sourceLine << std::flush;
}
numLines++;
outputFile << outputLine << std::endl;
}
}
sourceFile.close();
outputFile.close();
}
void Decrypt(unsigned int encryptionKey, std::string filename)
{
std::ifstream sourceFile(filename);
std::ofstream outputFile(filename.substr(0, filename.find_last_of("\\")) + "\\Decrypted" + filename.substr(filename.find_last_of("\\") + 1), std::ofstream::out | std::ofstream::trunc);
std::string sourceLine;
std::string outputLine;
long numLines = 0;
if (sourceFile.is_open())
{
std::cout << "Opening file: " + filename + " for decryption" << std::endl;
while (sourceFile.good()) // This iterates over the whole file, once for each line
{
if (sourceFile.fail() == true)
std::cout << "eof" << std::endl;
sourceLine = ""; //Clearing the line for each new line
outputLine = ""; //Clearing the line for each new line
std::getline(sourceFile, sourceLine);
for (int i = 0; i < sourceLine.length(); i++) // Looping through all characters in each line
{
char focusByte = sourceLine[i] ^ encryptionKey;
std::cout << " focusByte: " << focusByte << std::endl;
outputLine.push_back(focusByte);
}
numLines++;
outputFile << outputLine << std::endl;
}
}
sourceFile.close();
outputFile.close();
}
int main(int argument_count,
char * argument_list[])
{
system("color a");
std::string filename;
if (argument_count < 2)
{
std::cout << "You didn't supply a filename" << std::endl;
}
else
{
filename = argument_list[1];
std::cout << "Target file: " << filename << std::endl;
std::cout << "Press e to encrypt the selected file, Press d to decrypt the file > " << std::flush;
char choice;
while (true)
{
std::cin >> choice;
if (choice == 'e')
{
Encrypt(123, filename);
break;
}
else if (choice == 'd')
{
Decrypt(123, filename);
break;
}
else
{
std::cout << "please choose option e or d for encryption respectivly decryption" << std::endl;
}
}
}
std::cout << "\nPaused, press Enter to continue > " << std::flush;
system("Pause");
return EXIT_SUCCESS;
}
In Decrypt(), after the first call to std::getline(), sourceFile.good() is false and sourceFile.fail() is true, which is why you stop reading subsequent lines from the encrypted file.
The reason is because the encrypted file has an encoded 0x1A byte in it, and depending on your platform and STL implementation, that character likely gets interpreted as an EOF condition, thus enabling the std::ifstream's eofbit state, terminating further reading.
In my compiler's STL implementation on Windows, when std::ifstream reads from a file, it ultimately calls a function named _Fgetc():
template<> inline bool _Fgetc(char& _Byte, _Filet *_File)
{ // get a char element from a C stream
int _Meta;
if ((_Meta = fgetc(_File)) == EOF) // <-- here
return (false);
else
{ // got one, convert to char
_Byte = (char)_Meta;
return (true);
}
}
When it tries to read an 0x1A character, fgetc() returns EOF, and when _Fgetc() returns false, std::getline() sets the eofbit on the std::ifstream and exits.
Check your compiler's STL for similar behavior.
This behavior is because you are opening the encrypted file in text mode. You need to open the encrypted file in binary mode instead:
std::ifstream sourceFile(..., std::ifstream::binary);
Also, you should enable binary mode on the encrypted file in Encrypt() as well:
std::ofstream outputFile(..., std::ofstream::binary | std::ofstream::trunc);
Try something more like this instead:
#include <Windows.h>
#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>
void Encrypt(char encryptionKey, const std::string &filename)
{
std::string::size_type pos = filename.find_last_of("\\");
std::string out_filename = filename.substr(0, pos+1) + "Encrypted" + filename.substr(pos + 1);
std::ifstream sourceFile(filename.c_str());
std::ofstream outputFile(out_filename.c_str(), std::ofstream::binary | std::ofstream::trunc);
if (sourceFile.is_open())
{
std::cout << "Opened file: " + filename + " for encryption" << std::endl;
std::string line;
long numLines = 0;
while (std::getline(sourceFile, line)) // This iterates over the whole file, once for each line
{
for (std::string::size_type i = 0; i < line.length(); ++i) // Looping through all characters in each line
{
char focusByte = line[i] ^ encryptionKey;
std::cout << " focusByte: " << focusByte << std::endl;
line[i] = focusByte;
//std::cout << line << std::flush;
}
outputFile << line << std::endl;
++numLines;
}
}
}
void Decrypt(char encryptionKey, const std::string &filename)
{
std::string::size_type pos = filename.find_last_of("\\");
std::string out_filename = filename.substr(0, pos+1) + "Decrypted" + filename.substr(pos + 1);
std::ifstream sourceFile(filename.c_str(), std::ifstream::binary);
std::ofstream outputFile(out_filename.c_str(), std::ofstream::trunc);
if (sourceFile.is_open())
{
std::cout << "Opened file: " + filename + " for decryption" << std::endl;
std::string line;
long numLines = 0;
while (std::getline(sourceFile, line)) // This iterates over the whole file, once for each line
{
for (std::string::size_type i = 0; i < line.length(); ++i) // Looping through all characters in each line
{
char focusByte = line[i] ^ encryptionKey;
std::cout << " focusByte: " << focusByte << std::endl;
line[i] = focusByte;
}
outputFile << line << std::endl;
++numLines;
}
std::cout << "eof" << std::endl;
}
}
int main(int argument_count, char* argument_list[])
{
std::system("color a");
std::string filename;
if (argument_count < 2)
{
std::cout << "Enter a file to process: " << std::flush;
std::getline(std::cin, filename);
}
else
{
filename = argument_list[1];
}
if (filename.empty())
{
std::cout << "You didn't supply a filename" << std::endl;
return EXIT_FAILURE;
}
std::cout << "Target file: " << filename << std::endl;
std::cout << "Press e to encrypt the file" << std::endl;
std::cout << "Press d to decrypt the file" << std::endl;
char choice;
while (true)
{
std::cout << "> " << std::flush;
std::cin >> choice;
if (choice == 'e')
{
Encrypt(123, filename);
break;
}
else if (choice == 'd')
{
Decrypt(123, filename);
break;
}
else
{
std::cout << "please choose option e or d for encryption or decryption, respectively" << std::endl;
}
}
std::cout << std::endl << "Paused, press Enter to continue" << std::flush;
std::system("pause");
return EXIT_SUCCESS;
}
That being said, keep in mind that when using XOR, some of the encrypted characters might end up being \r (0x0D) or \n (0x0A), which will interfere with std::getline() when decrypting the file later on, producing a decrypted output that does not match the original text input.
Since you should be treating the encrypted file as binary, you should not be reading/writing the file as text at all. Choose a different format for your encrypted output that does not rely on line-break semantics in text vs binary mode.
For example:
#include <Windows.h>
#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>
void Encrypt(char encryptionKey, const std::string &filename)
{
std::string::size_type pos = filename.find_last_of("\\");
std::string out_filename = filename.substr(0, pos+1) + "Encrypted" + filename.substr(pos + 1);
std::ifstream sourceFile(filename.c_str());
std::ofstream outputFile(out_filename.c_str(), std::ofstream::binary | std::ofstream::trunc);
if (sourceFile.is_open())
{
std::cout << "Opened file: " + filename + " for encryption" << std::endl;
std::string line;
std::string::size_type lineLen;
long numLines = 0;
while (std::getline(sourceFile, line)) // This iterates over the whole file, once for each line
{
lineLen = line.length();
for (std::string::size_type i = 0; i < lineLen; ++i) // Looping through all characters in each line
{
char focusByte = line[i] ^ encryptionKey;
std::cout << " focusByte: " << focusByte << std::endl;
line[i] = focusByte;
//std::cout << line << std::flush;
}
outputFile.write((char*)&lineLen, sizeof(lineLen));
outputFile.write(line.c_str(), lineLen);
++numLines;
}
}
}
void Decrypt(char encryptionKey, const std::string &filename)
{
std::string::size_type pos = filename.find_last_of("\\");
std::string out_filename = filename.substr(0, pos+1) + "Decrypted" + filename.substr(pos + 1);
std::ifstream sourceFile(filename.c_str(), std::ifstream::binary);
std::ofstream outputFile(out_filename.c_str(), std::ofstream::trunc);
if (sourceFile.is_open())
{
std::cout << "Opened file: " + filename + " for decryption" << std::endl;
std::string line;
std::string::size_type lineLen;
long numLines = 0;
while (sourceFile.read((char*)&lineLen, sizeof(lineLen))) // This iterates over the whole file, once for each line
{
line.resize(lineLen);
if (!sourceFile.read(&line[0], lineLen))
break;
for (std::string::size_type i = 0; i < lineLen; ++i) // Looping through all characters in each line
{
char focusByte = line[i] ^ encryptionKey;
std::cout << " focusByte: " << focusByte << std::endl;
line[i] = focusByte;
}
outputFile << line << std::endl;
++numLines;
}
std::cout << "eof" << std::endl;
}
}
int main(int argument_count, char* argument_list[])
{
std::system("color a");
std::string filename;
if (argument_count < 2)
{
std::cout << "Enter a file to process: " << std::flush;
std::getline(std::cin, filename);
}
else
{
filename = argument_list[1];
}
if (filename.empty())
{
std::cout << "You didn't supply a filename" << std::endl;
return EXIT_FAILURE;
}
std::cout << "Target file: " << filename << std::endl;
std::cout << "Press e to encrypt the file" << std::endl;
std::cout << "Press d to decrypt the file" << std::endl;
char choice;
while (true)
{
std::cout << "> " << std::flush;
std::cin >> choice;
if (choice == 'e')
{
Encrypt(123, filename);
break;
}
else if (choice == 'd')
{
Decrypt(123, filename);
break;
}
else
{
std::cout << "please choose option e or d for encryption or decryption, respectively" << std::endl;
}
}
std::cout << std::endl << "Paused, press Enter to continue" << std::flush;
std::system("pause");
return EXIT_SUCCESS;
}
ASCII value 26 is EOF on some operating systems.
You should probably treat your encrypted file as a byte stream rather than a text file for reading and writing. That means either using read() and write() functions of the IOStream or at the very least opening the files in binary mode.
If you're just enciphering your text instead of encrypting, maybe choose a different cipher (eg. ROT13) that is closed on the set of printable ASCII or UTF-8 characters.
I compiled your code in Linux (minus all the Windows stuff)...
I get this when encrypting your sentence with your code:
/[[[[[
[[[ [[[U
It also decrypts back to the original sentence. Without the goofy characters, it is the same as your output so your actual issue seems related to the encoding of the file and the program you are using to view the results. Stephan is correct in saying you should be reading/writing bytes instead of text. This can cause all sorts of issues with the characters you create. For example, line feeds and carriage returns since you are using getline().
Edit: Strange. After editing this answer, all the odd characters disappeared. Here is a screenshot:
I am writing a C++ code to calculate the code coverage and I want to used the OpenMP to help enhance my code by minimizing the overall run time by making the functions work in parallel so I can get less run time.
Can someone please tell me how and where to use the OpenMP?
int _tmain(int argc, _TCHAR* argv[])
{
std::clock_t start;
start = std::clock();
char inputFilename[] = "Test-Case-3.cs"; // Test Case File
char outputFilename[] = "Result.txt"; // Result File
int totalNumberOfLines = 0;
int numberOfBranches = 0;
int statementsCovered = 0;
float statementCoveragePercentage = 0;
double overallRuntime = 0;
ifstream inFile; // object for reading from a file
ofstream outFile; // object for writing to a file
inFile.open(inputFilename, ios::in);
if (!inFile) {
cerr << "Can't open input file " << inputFilename << endl;
exit(1);
}
totalNumberOfLines = NoOfLines(inFile);
inFile.clear(); // reset
inFile.seekg(0, ios::beg);
numberOfBranches = NoOfBranches(inFile);
inFile.close();
statementsCovered = totalNumberOfLines - numberOfBranches;
statementCoveragePercentage = (float)statementsCovered * 100/ totalNumberOfLines;
outFile.open(outputFilename, ios::out);
if (!outFile) {
cerr << "Can't open output file " << outputFilename << endl;
exit(1);
}
outFile << "Total Number of Lines" << " : " << totalNumberOfLines << endl;
outFile << "Number of Branches" << " : " << numberOfBranches << endl;
outFile << "Statements Covered" << " : " << statementsCovered << endl;
outFile << "Statement Coverage Percentage" << " : " << statementCoveragePercentage <<"%"<< endl;
overallRuntime = (std::clock() - start) / (double)CLOCKS_PER_SEC;
outFile << "Overall Runtime" << " : " << overallRuntime << " Seconds"<< endl;
outFile.close();
}
i want to minimize the time taken to count the number of branches by allowing multiple threads to work in parallel to calculate the number faster? how can i edit the code so that i can use the open mp and here you can find my functions:bool is_only_ascii_whitespace(const std::string& str)
{
auto it = str.begin();
do {
if (it == str.end()) return true;
} while (*it >= 0 && *it <= 0x7f && std::isspace(*(it++)));
// one of these conditions will be optimized away by the compiler,
// which one depends on whether char is signed or not
return false;
}
// Function 1
int NoOfLines(ifstream& inFile)
{
//char line[1000];
string line;
int lines = 0;
while (!inFile.eof()) {
getline(inFile, line);
//cout << line << endl;
if ((line.find("//") == std::string::npos)) // Remove Comments
{
if (!is_only_ascii_whitespace(line)) // Remove Blank
{
lines++;
}
}
//cout << line << "~" <<endl;
}
return lines;
}
// Function 2
int NoOfBranches(ifstream& inFile)
{
//char line[1000];
string line;
int branches = 0;
while (!inFile.eof()) {
getline(inFile, line);
if ((line.find("if") != std::string::npos) || (line.find("else") != std::string::npos))
{
branches++;
}
}
return branches;
}
I'm reading a 2 to 4 gb .txt file and then I manipulate some of the data and want to save my struct vector as a tab delimited .txt file. I read some of the other questions but still not clear to me how I'm going to do it in my program.
So my question is: How to save the Input vector results as a tab delimited .txt file?
Below is my code:
#include <iostream>
#include <sstream>
#include <string>
#include <fstream>
#include <vector>
#include <cstdio>
using namespace std;
struct Input_Spec {
std::string Data;
std::string Ativo;
int Buy_Sell;
double Sequencia;
double Id;
int Event;
std::string Hr_Priority;
double Priority;
double Price;
double Qtd_Total;
double Qtd_Traded;
std::string Data_Order;
std::string Data_Time_Order;
std::string State_Of_Order;
std::string Condition_Of_Order;
double Broker;
};
void split(const std::string &s, char delim, std::string elems[])
{
std::stringstream ss(s);
std::string item;
long i = 0;
while (std::getline(ss, item, delim))
{
elems[i++] = item;
}
}
int main()
{
ifstream infile1("C:\\Teste\\Teste.txt");
ofstream output("output.txt");
string word;
string columns[16];
string line;
int row=0;
long c=0;
long filescol=0;
for (int i = 0; std::getline(infile1, word); ++i)
{
row++;
}
//cout<<row;
infile1.close();
ifstream infile("C:\\Teste\\Teste.txt");
vector<Input_Spec> Input(row);
while( getline(infile, line))
{
split(line,';', columns);
if (columns[0]!="")
{
Input[filescol].Data =columns[0];
Input[filescol].Ativo =columns[1];
Input[filescol].Buy_Sell = stoi(columns[2]);
Input[filescol].Sequencia = stod(columns[3]);
Input[filescol].Id = stod(columns[4]);
Input[filescol].Event = stoi(columns[5]);
Input[filescol].Hr_Priority = columns[6];
Input[filescol].Priority = stod(columns[7]);
Input[filescol].Price = stod(columns[8]);
Input[filescol].Qtd_Total = stod(columns[9]);
Input[filescol].Qtd_Traded = stod(columns[10]);
Input[filescol].Data_Order = columns[11];
Input[filescol].Data_Time_Order = columns[12];
Input[filescol].State_Of_Order = columns[13];
Input[filescol].Condition_Of_Order = columns[14];
Input[filescol].Broker = stod(columns[15]);
filescol++;
c++;
}
if (c>(999))
{
break;
infile.close();
return 0;
}
}
infile.close();
return 0;
}
Here's a fragment:
ofstream output ("output.txt");
output << Input[filescol].Data << '\t';
output << Input[filescol].Ativo << '\t';
output << Input[filescol].Buy_Sell << '\t';
//...
output << Input[filescol].Broker << '\n';
Is this what you are talking about?
At first, please let me give you some hints on your code: You don't need to count all lines of the input file and you don't need to reserve memory for all items in your vector beforehand. You can use the push_back() function to add items to the vector. This saves you one iteration on the input file.
I put this improvement into the main() function below. I also expanded a little bit on the answer by #ThomasMatthews, so that you can see how to loop over the vector in order to save the data in it:
int main()
{
ifstream infile("C:\\Teste\\Teste.txt");
ofstream output("C:\\Teste\\output.txt");
string line;
string columns[16];
vector<Input_Spec> Input;
Input_Spec oneInput;
while (getline(infile, line))
{
split(line, ';', columns);
if (!columns[0].empty())
{
oneInput.Data = columns[0];
oneInput.Ativo = columns[1];
oneInput.Buy_Sell = stoi(columns[2]);
oneInput.Sequencia = stod(columns[3]);
oneInput.Id = stod(columns[4]);
oneInput.Event = stoi(columns[5]);
oneInput.Hr_Priority = columns[6];
oneInput.Priority = stod(columns[7]);
oneInput.Price = stod(columns[8]);
oneInput.Qtd_Total = stod(columns[9]);
oneInput.Qtd_Traded = stod(columns[10]);
oneInput.Data_Order = columns[11];
oneInput.Data_Time_Order = columns[12];
oneInput.State_Of_Order = columns[13];
oneInput.Condition_Of_Order = columns[14];
oneInput.Broker = stod(columns[15]);
Input.push_back(oneInput);
}
}
// ----------------------
// Modify your data here.
// ----------------------
for(vector<Input_Spec>::const_iterator it = Input.begin(); it != Input.end(); it++)
{
output << it->Data << '\t';
output << it->Ativo << '\t';
output << it->Buy_Sell << '\t';
output << it->Sequencia << '\t';
output << it->Id << '\t';
output << it->Event << '\t';
output << it->Hr_Priority << '\t';
output << it->Priority << '\t';
output << it->Price << '\t';
output << it->Qtd_Total << '\t';
output << it->Qtd_Traded << '\t';
output << it->Data_Order << '\t';
output << it->Data_Time_Order << '\t';
output << it->State_Of_Order << '\t';
output << it->Condition_Of_Order << '\t';
output << it->Broker << '\n';
}
output.close();
infile.close();
return 0;
}
I guess that more improvements could be done to your code, but this should give you a good start.
My goal is to take the text from "filename" that I have in my main directory, take the odd lines and send them over the filenameA, and the even lines to filenameB. From there, I want to splice them back into a new file. How can I create a while loop to do so?
// This program splits a file into two files from main directory
// and then splices the original file with a new name.
#include<iostream>
#include<fstream>
using namespace std;
void pause();
int main()
{
char filename[] = "Lab2Test.txt";
char filenameA[] = "LabTest-FA.txt";
char filenameB[] = "LabTest-FB.txt";
ifstream origin(filename);
ofstream fA(filenameA);
ofstream fB(filenameB);
if (! origin)
{
cout << filename << " could not be opened." << endl;
return -1;
}
string s;
int i=0;
while(getline(origin, s))
{
if(i % 2 == 1) //odd - write to LabTest-FA
fA << s << endl;
else
fB << s << endl;
i++;
}
}
void pause()
{
cin.sync();
cout << "Press any key to continue..." << endl;
cin.ignore();
}
First pick a book on C++ and learn a bit more about the language, you have lots of mistakes in the code.
Here is a working program, the best I can come up with.
// This program splits a file into two files from main directory
// and then splices the original file with a new name.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
void pause()
{
cin.sync();
cout << "Press any key to continue..." << endl;
cin.ignore();
}
int main()
{
char filename[] = "Lab2Test.txt";
char filenameA[] = "LabTest-FA.txt";
char filenameB[] = "LabTest-FB.txt";
char filenew[] = "Lab2Test2.txt";
ifstream origin(filename);
fstream fA(filenameA, std::fstream::in | std::fstream::out | std::fstream::trunc);
fstream fB(filenameB, std::fstream::in | std::fstream::out | std::fstream::trunc);
ofstream fnew(filenew);
if (!origin)
{
cout << filename << " could not be opened." << endl;
return -1;
}
string s;
int i = 0;
while( getline(origin, s) )
{
if(i % 2 == 1) // odd - write to LabTest-FA
fA << s << endl;
else // even - write to LabTest-FB
fB << s << endl;
i++;
}
fA.flush(); // write to disk
fB.flush();
fA.seekg(0, ios::beg); // rewind the files to the beginning
fB.seekg(0, ios::beg);
string s1, s2;
while( getline(fB,s1) )
{
fnew << s1 << endl;
if(getline(fA,s2))
fnew << s2 << endl;
}
pause();
}