Write PGM function overwritting existing file. - c++

Im having problems understanding how to write a PGM file using data.
This function i have overwrites the existing txt document instead of creating a PGM file.
Im unsure whether my issue is lies within my Read_text function or somewhere else. Can anyone help me understand this issue and fix it please?
double* read_text(char *fileName, int sizeR, int sizeC)
{
double* data = new double[sizeR*sizeC];
int i = 0;
std::ifstream myfile(fileName);
if (myfile.is_open())
{
while (myfile.good())
{
if (i>sizeR*sizeC - 1) break;
myfile >> *(data + i);
std::cout << *(data+i) << ' '; //This line display the converted data on the screen, you may comment it out.
i++;
}
myfile.close();
}
else std::cout << "Unable to open file";
std::cout << i;
return data;
}
void write_pgm(char *filename, double *data, int sizeR, int sizeC, int Q)
{
int i, j;
unsigned char *image;
std::ofstream myfile;
image = (unsigned char *) new unsigned char[sizeR*sizeC];
// convert the integer values to unsigned char
for (i = 0; i<sizeR*sizeC; i++)
image[i] = (unsigned char)data[i];
myfile.open(filename, std::ios::out | std::ios::binary | std::ios::trunc);
if (!myfile) {
std::cout << "Can't open file: " << filename << std::endl;
exit(1);
}
myfile << "P5" << std::endl;
myfile << sizeC << " " << sizeR << std::endl;
myfile << Q << std::endl;
myfile.write(reinterpret_cast<char *>(image), (sizeR*sizeC)*sizeof(unsigned char));
if (myfile.fail()) {
std::cout << "Can't write image " << filename << std::endl;
exit(0);
}
myfile.close();
std::cout << "Finsihed " << std::endl;
delete[] image;
}
void main() {
double* data = read_text("Wally_grey.txt", rows(), columns());
std::cout << data << std::endl;
write_pgm("Wally_grey.txt",data, rows(), columns(), 255);
getchar();
}

Related

fstream stops to read at substitute control character

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:

file.write only writing 7 bytes of file in C++

The following code only saves the first 7 bytes to the file correctly and the remaining 3072-7=3065 bytes are incorrect. "correct" meaning the same value as stored in 'data'.
#define byte unsigned char
void bytesToImage(byte width, byte height, byte* data, size_t byte_count, char* fileNameWithoutExtension)
{
{
std::ofstream file("k3000", std::ios::binary);
file.write((char *)data, 3000);
}
}
However this code does save the first 500 bytes correctly:
#define byte unsigned char
void bytesToImage(byte width, byte height, byte* data, size_t byte_count, char* fileNameWithoutExtension)
{
{
std::ofstream file("k500", std::ios::binary);
file.write((char *)data, 500);
}
}
data has a length of 3072 and the function is called as follows:
size_t imageByteCount = 32 * 32 * 3;
byte* imageBufferOut = (byte*)malloc(sizeof(byte) * imageByteCount);
//(imageBufferOut is initialized...)
bytesToImage(32, 32, imageBufferOut, imageByteCount, "img");
Please excuse the redundant parameters, I have removed as much as possible to try find the bug.
Hex dumps:
Try adding some more instrumentation. For example:
{
cout << "before:";
for (int i = 0; i < 16; ++i)
cout << ' ' << std::hex << int(data[i]);
cout << '\n';
std::ofstream file("k3000", std::ios::binary);
if (file)
cout << "opened\n";
else
cout << "couldn't open\n";
file.write((char *)data, 3000);
file.flush();
if (file)
cout << "wrote ok\n";
else
cout << "write failed\n";
cout << "after:";
for (int i = 0; i < 16; ++i)
cout << ' ' << std::hex << int(data[i]);
cout << '\n';
}

Find specific lines in text document

I want to create small program to understand things I need better.
This code can write words to text document, new line under previous in sequential order and keep lines there after starting program again.
Now before adding a new word or phrase to the file, I want to find if the word already exists in my document, if exist, don't add it, but get exist equal one on output, read it from file, and main thing here is somehow also find line under or above current exist line. For example: if exist line index is 3, I want to see +1 line 4 or -1 line 2. If new word doesn't exist in text document just add it.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
std::ofstream outfile("doc.txt", std::ios_base::app);
int main()
{
std::string t;
for (int i = 0; i < 10; i++)
{
cout << "Add new phrase: " << endl;
std::getline(std::cin, t);
cout << t << endl;
outfile << t << std::endl;
}
_getch();
return 0;
}
EDIT:
using namespace std;
std::ofstream outfile("doc.txt", std::ios_base::app);
int main()
{
int length = 100;
std::ifstream infile("doc.txt", std::ifstream::in);
infile.seekg(0, infile.end);
size_t len = infile.tellg();
infile.seekg(0, infile.beg);
char *buf = new char[len];
infile.read(buf, length);
infile.close();
std::string writtenStr(reinterpret_cast<const char *>(buf), len);
std::string t;
for (int i = 0; i < 10; i++)
{
std::getline(std::cin, t);
if (writtenStr.find(t) != std::string::npos)
{
cout << "Line [" << t << "] exist." << endl;
}
else
{
cout << "Line [" << t << "] saved." << endl;
writtenStr += t;
outfile << t << std::endl;
}
}
_getch();
return 0;
}
I'd read the file into a string when the program starts. Then, check the string for the phrase each time i want to add a new word. If the string doesn't contain the phrase, add it to the string and the file, and a delimiter of your choice if desired. For example:
int main()
{
// Read existing file into a string
std::ifstream infile("doc.txt", std::ifstream::in);
infile.seekg(0, infile.end);
size_t len = infile.tellg();
infile.seekg(0, infile.beg);
char *buf = new char[len];
infile.read(buf,length);
infile.close();
std::string writtenStr(reinterpret_cast<const char *>(buf), len);
// Open file for output
std::ofstream outfile("doc.txt", std::ios_base::app);
std::string t;
for (int i = 0; i < 10; i++)
{
// Get new phrase
std::getline(std::cin, t);
// Check if phrase is already in file;
if (writtenStr.find(t) == std::string::npos)
{
cout << "Could not add new phrase: " << endl;
cout << t << endl;
cout << "Phrase already exists in file." << endl;
}
else
{
cout << "Add new phrase: " << endl;
cout << t << endl;
writtenStr += t;
outfile << t << std::endl;
}
}
_getch();
return 0;
}

Where can I use OpenMP in my C++ code

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;
}

Getting Garbage Values while reading struct data from a binary file

Hi guys in my previous question, I was able to get the data of a struct to be loaded on a file, but now the issue is I'm getting garbage value while retrieving it.
File Contents: settings.bin
110#NormalCompression Level210#NormalCompression Level310#NormalCompression Level410#NormalCompression Level510#NormalCompression Level
Code
#include<cstdlib>
#include<iostream>
#include<string>
#include<iomanip>
#include<fstream.h>
using namespace std;
const char* ErrorLogFilePath = "resources\\error.txt";
const char* SettingsFilePath = "resources\\settings.bin";
const int NoOfSettings = 5;
struct Setting {
int SettingID;
int SettingINTValue;
double SettingDOUBLEValue;
char SettingCHARValue;
string SettingSTRINGValue;
string SettingName;
};
istream& operator>>(istream& _is, Setting& _s) {
_is>>_s.SettingID;
_is>>_s.SettingINTValue;
_is>>_s.SettingDOUBLEValue;
_is>>_s.SettingCHARValue;
_is>>_s.SettingSTRINGValue;
_is>>_s.SettingName;
}
ostream& operator<<(ostream& _os, const Setting& _s) {
_os<<_s.SettingID;
_os<<_s.SettingINTValue;
_os<<_s.SettingDOUBLEValue;
_os<<_s.SettingCHARValue;
_os<<_s.SettingSTRINGValue;
_os<<_s.SettingName;
}
class ErrorReport {
public:
fstream ErrorFile;
void PostError(string Title,string Data,int ErrorID) {
ErrorFile.open(ErrorLogFilePath,ios::out);
ErrorFile.close();
}
} Error;
class SettingsClass {
public:
Setting setting[NoOfSettings];
void ResetSettings() {
fstream SettingFile;
Setting defaultsetting[NoOfSettings];
for(int i=1; i<=NoOfSettings; i++) {
defaultsetting[i-1].SettingID = i;
defaultsetting[i-1].SettingINTValue = 0;
defaultsetting[i-1].SettingDOUBLEValue = 0;
defaultsetting[i-1].SettingCHARValue = '#';
defaultsetting[i-1].SettingSTRINGValue = "null";
switch(i) {
default:
defaultsetting[i-1].SettingName = "Compression Level";
defaultsetting[i-1].SettingSTRINGValue = "Normal";
defaultsetting[i-1].SettingINTValue = 1;
break;
}
}
SettingFile.open(SettingsFilePath,ios::binary|ios::out);
if(SettingFile.is_open()) {
for(size_t i=0; i<NoOfSettings; ++i) {
SettingFile<<defaultsetting[i];
}
} else {
cout<<"Error!";
}
SettingFile.close();
}
void _SettingsClass() {
fstream SettingFile;
SettingFile.open(SettingsFilePath,ios::binary|ios::in);
Setting TempSettings[NoOfSettings];
if(SettingFile.is_open()) {
for(size_t i=0; i<NoOfSettings; ++i) {
SettingFile>>TempSettings[i];
}
} else {
cout<<"Error...";
}
SettingFile.close();
for(int i=0; i<NoOfSettings; i++) {
cout<<TempSettings[i].SettingINTValue<<"\n";
}
}
} Settings;
int main(int argc, char *argv[])
{
Settings._SettingsClass();
// cout<<Settings.GetSetting(1).SettingName;
system("PAUSE");
return EXIT_SUCCESS;
}
Output
4473076
1
3
0
2686384
Now Why I'm getting those garbage values? Can anyone help me, cause I think it should not turn up like this(?). And I should be getting 1 for each such element of that struct array.
Thanks in Advance!
Formatted input requires delimiters so it knows when to stop reading for a particular value. Even though you're opening the file in binary mode what you're writing is in essence a text file with no delimiters so you can't read it back.
If you must have a binary file then this is the way to read/write one using your structure:
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
#include <vector>
const char* SettingsFilePath = "settings.bin";
struct Setting
{
int SettingID;
int SettingINTValue;
double SettingDOUBLEValue;
char SettingCHARValue;
std::string SettingSTRINGValue;
std::string SettingName;
Setting()
: SettingID(0)
, SettingINTValue(0)
, SettingDOUBLEValue(0)
, SettingCHARValue(0)
{
}
void Write(std::fstream& out)
{
out.write(reinterpret_cast<const char*>(&SettingID), sizeof(SettingID));
out.write(reinterpret_cast<const char*>(&SettingINTValue), sizeof(SettingINTValue));
out.write(reinterpret_cast<const char*>(&SettingDOUBLEValue), sizeof(SettingDOUBLEValue));
out.write(reinterpret_cast<const char*>(&SettingCHARValue), sizeof(SettingCHARValue));
size_t str_size = SettingSTRINGValue.size();
out.write(reinterpret_cast<const char*>(&str_size), sizeof(str_size));
out.write(SettingSTRINGValue.c_str(), SettingSTRINGValue.size());
str_size = SettingName.size();
out.write(reinterpret_cast<const char*>(&str_size), sizeof(str_size));
out.write(SettingName.c_str(), SettingName.size());
}
void Read(std::fstream& in)
{
in.read(reinterpret_cast<char*>(&SettingID), sizeof(SettingID));
in.read(reinterpret_cast<char*>(&SettingINTValue), sizeof(SettingINTValue));
in.read(reinterpret_cast<char*>(&SettingDOUBLEValue), sizeof(SettingDOUBLEValue));
in.read(reinterpret_cast<char*>(&SettingCHARValue), sizeof(SettingCHARValue));
size_t str_size;
std::vector<char> str_data;
in.read(reinterpret_cast<char*>(&str_size), sizeof(str_size));
str_data.resize(str_size);
in.read(&str_data[0], str_size);
SettingSTRINGValue.assign(str_data.begin(), str_data.end());
in.read(reinterpret_cast<char*>(&str_size), sizeof(str_size));
str_data.resize(str_size);
in.read(&str_data[0], str_size);
SettingName.assign(str_data.begin(), str_data.end());
}
void Print(const std::string& title)
{
std::cout << title << "\n";
std::cout << std::string(title.size(), '-') << "\n";
const size_t w = 22;
std::cout << std::setw(w) << std::right << "SettingID : " << SettingID << "\n";
std::cout << std::setw(w) << std::right << "SettingINTValue : " << SettingINTValue << "\n";
std::cout << std::setw(w) << std::right << "SettingDOUBLEValue : " << SettingDOUBLEValue << "\n";
std::cout << std::setw(w) << std::right << "SettingCHARValue : " << SettingCHARValue << "\n";
std::cout << std::setw(w) << std::right << "SettingSTRINGValue : " << SettingSTRINGValue << "\n";
std::cout << std::setw(w) << std::right << "SettingName : " << SettingName << "\n";
std::cout << "\n";
}
};
int main()
{
{
Setting s;
s.Print("Default before Write");
s.SettingID = 1;
s.SettingINTValue = 2;
s.SettingDOUBLEValue = 3.5;
s.SettingCHARValue = 'Z';
s.SettingSTRINGValue = "Blah Blah";
s.SettingName = "Some Settings";
std::fstream f(SettingsFilePath, std::ios::out | std::ios::binary);
s.Write(f);
s.Print("Values written to file");
}
{
Setting s;
s.Print("Default before read");
std::fstream f(SettingsFilePath, std::ios::in | std::ios::binary);
s.Read(f);
s.Print("Values after read");
}
return EXIT_SUCCESS;
}
It seems you forgot to load your setting into file.
int main(int argc, char *argv[])
{
Settings.ResetSettings(); // insert code here
Settings._SettingsClass();
// cout<<Settings.GetSetting(1).SettingName;
system("PAUSE");
return EXIT_SUCCESS;
}