So I am trying to read values from a .dat file into a 2D vector (plus 3-1D vectors each representing a RGB value), then output the values into a .PPM file that my code will create with a specific format. I've got most of the code complete, but for some reason it's just stopping after getting all of the user input it needs, then doesn't return anything. If you could suggest anything that would be more efficient, that would be great. However, my main problem is just getting the code to work. Any help would be greatly appreciated!
Cheers
#include <iostream>
#include <algorithm>
#include <ctime>
#include <cmath>
#include <iomanip>
#include <vector>
#include <fstream>
#include <string>
#include <unistd.h>
#include <sys/stat.h>
using namespace std;
// Function Declaration
inline bool exists (const string& name);
int main(int argc, const char * argv[]) {
// Start main code
// Variable Declaration
double rows;
double columns;
string fileName;
int min = 0;
int max = 0;
vector<int> red;
vector<int> green;
vector<int> blue;
// Request/get rows and validate
do {
cin.clear();
cout << "Enter number of rows: ";
cin >> rows;
if (cin.fail() || rows - static_cast<int>(rows) != 0) {
cout << "Invalid number of rows. Please enter a positive integer." << endl;
}
}
while (cin.fail() || rows - static_cast<int>(rows) != 0);
cin.clear();
// Request/get columns and validate
do {
cout << endl << "Enter number of columns: ";
cin >> columns;
if (cin.fail() || columns - static_cast<int>(columns) != 0) {
cout << "Invalid number of columns. Please enter a positive integer." << endl;
}
}
while (cin.fail() || columns - static_cast<int>(columns) != 0);
// Request/get file name and validate both if input is good and if file exists
do {
cin.clear();
cout << "Enter file name: ";
cin >> fileName;
if (cin.fail()) {
cout << "Invalid file name. Enter a file name with extension \".dat.\"" << endl;
}
if (!exists(fileName)) {
cout << "File does not exist." << endl;
}
}
while (cin.fail() || !exists(fileName));
// Open file to be read
ifstream readFile;
readFile.open(fileName);
// Declare primary vector
vector<vector<int>> dataVector(rows, vector<int>(columns));
// Calculate minimum and maximum values in file
while (!readFile.eof()) {
int tempComp;
readFile >> tempComp;
if (tempComp > max) {
max = tempComp;
}
else if (tempComp < min) {
min = tempComp;
}
}
readFile.clear();
readFile.seekg(0, ios::beg);
cout << max;
// Input data from file to vector
while (!readFile.eof() && readFile.is_open()) {
int num;
vector<int> tempVector;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
readFile >> num;
tempVector.push_back(num);
red.push_back(static_cast<int>((static_cast<double>(num) / static_cast<double>(max)) * 255));
green.push_back(static_cast<int>((static_cast<double>(num) / static_cast<double>(max)) * 255));
blue.push_back(static_cast<int>((static_cast<double>(num) / static_cast<double>(max)) * 255));
}
dataVector.push_back(tempVector);
}
}
ofstream outputFS(fileName += ".ppm");
outputFS << "P3" << endl;
outputFS << columns << " " << rows << endl;
outputFS << "255" << endl;
for (int j = 0; j < max; j++) {
outputFS << red.at(j) << " " << green.at(j) << " " << blue.at(j);
if (j % static_cast<int>(columns) == 0) {
cout << endl;
}
}
ofstream out(fileName += ".ppm");
outputFS.close();
return 0;
}
// Definition of Functions
// Checks if file exists
inline bool exists (const string& name) {
struct stat buffer;
return (stat (name.c_str(), &buffer) == 0);
}