Read in a file for word search game C++ - c++

I am attempting read in a file and create a 2D vector to store the game board for a word search game but I can't seem to get it to read more than the first line. I'm sure it is something small but I put in a test display for the 'board' vector and all that shows up is the first line of the text file.
Here is some of my code:
'Board.cpp'
#include "stdafx.h"
#include "Board.h"
#include <fstream>
#include <iostream>
using namespace std;
Board::Board(void)
: rows(0)
{
}
Board::~Board(void)
{
}
void Board::readInFile(void)
{
ifstream indata;
indata.open("Puzzle.txt");
indata >> rows;
for(int i = 0; i < rows; i++){
char tmpChar;
for(int j = 0; j < rows; j++){
indata >> tmpChar;
row.push_back(tmpChar);
}
board.push_back(row);
}
indata.close();
}
'Board.h'
#pragma once
#include <vector>
using namespace std;
class Board
{
public:
Board(void);
~Board(void);
void readInFile(void);
protected:
vector<vector<char>> board;
vector<char>row;
protected:
int rows;
};
Here is how the text file is set up:
16
BDXWASEESPHWEBGB
SIGJVAWDFLCTZIAM
ENKVESMARNAEBRRI
IKOEOPZLUKMVJDDL
KLIRELOBSNPOFWEC
SBOHKLLRHSIFPANA
RSKWMEEEPEITPTPE
EZPIELLLYMOOQCDH
TAWDLGGLZBUNDHOJ
ASIOJNFOPKAJAPBP
WLRCIILROZXLSCID
SKATEBOARDGCLCIA
LLABESABIVOTNVVE
VOFRISBEETMIEVZG
BWADEVAUCYCSWING
XNJFJPZHBTBFTSAW

There are much better ways to handle input files. Firstly, rather than using a vector<char>, just use a std::string, and rather than using a vector<vector<char> >, just use a vector<string>. I'm not sure I understand why row is a member of the class if it is only being used to fill the board. Anytime you need the last item in board you can just do board.back() or *board.rbegin().
void Board::readInFile(void)
{
ifstream indata("Puzzle.txt", ios::in);
if(!ifstream.is_open())
return;
indata >> rows; //you don't really need to do this if you take
//the row count out of the file, but if you can't
//change the file, leave it in so as not to corrupt
//the loop that follows
string line;
while(getline(indata, line)){
board.push_back(line);
}
indata.close();
}
Really, you don't need to have the file store the rows variable.
Your function would also be more reusable if readInFile took the filename as a parameter so it could open anything instead of only one specific filename.

Related

C++ ifstream problem.I want to read "coordinates" from a .csv file but somehow the code reads the file twice and it puts wierd numbers

I have a problem trying to read coordinates from a .csv file to a 2d array , to use it as input to a linear regression model .I know how to read from a file to an array but i needed the informations of the file to be double and not string so I thought i should see what the output whould be. I can't get it right.The problem is that the file's lines are being read 30 times each one of them and i can't figure it out.
The code is here:
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <cmath>
#include <limits>
#include <iomanip>
#include <vector>
using namespace std;
int main()
{
cout<<setprecision(10);
//vector<vector<double> > observ_matr;
ifstream myfile("Salary_Data.csv");
vector<vector<double> > vec;
string line;
while(getline(myfile,line))
{
stringstream lineStream(line);
string cell;
vector<double> temp_vec;
while(getline(lineStream, cell, ','))
{
temp_vec.push_back(atof(cell.c_str()));
}
vec.push_back(temp_vec);
}
for(int i=0;i<vec.size();i++)
{
for(int j=0;j<vec.size();j++)
{
cout<<vec[i][j]<<"\t\t";
}
cout<<endl;
}
//cout<<vec[1].size();
return 0;
}
"myfile" is this , this .csv file
The output i am getting after running the code is this:
The problem is here...
for (int j = 0; j < vec[i].size(); j++) // vec[i].size() instead of vec.size()

How to set up function parameters to get back a 2 dimensional vector filled with values read from a file?

I am struggling to write parameters for a function 'readFile' which parameters are - a file name and a 2D vector to which information is copied from the file. The program doesn't throw ANY errors, but it does return just an empty vector. If i copy the code from 'readFile' function to main() function, it does work, but I want to make a seperate function so I can fill vectors with information from different files. Also I will need to pass these arguments for different type of functions with similar structure where I read from a file and copy to a 2D vector, so I really have to figure this out.
'PrintVector' function works fine. Could someone help me?
#include <string>
#include <sstream>
#include <iostream>
#include <vector>
#include <fstream>
using namespace std;
void printVector(vector< vector <float> > v)
{
for (size_t i=0; i<v.size(); ++i)
{
for (size_t j=0; j<v[i].size(); ++j)
{
cout << v[i][j] << "\t";
}
cout << "\n";
}
}
void readFile(char* filename, vector< vector<float> > &rowvector)
{
ifstream myfile(filename, ios::in);
string line;
string field;
vector<float> v; // 1 row
float result; // converted string value is saved in 'result' as a float type
if(myfile.is_open()){
while ( getline(myfile,line) ) // get next line in file
{
v.clear();
stringstream ss(line);
while (getline(ss,field,',')) // comma seperates elements
{
istringstream convert(field);
if ( !(convert >> result) )
result = 0;
v.push_back(result); // add each field to the 1D array
}
rowvector.push_back(v); // add the 1D array to the 2D array
}
}
myfile.close();
}
int main()
{
vector< vector<float> > myvector; // new 2D vector
readFile("test.txt", myvector);
printVector(myvector);
return 0;
}
Any help is much appreciated!

C++ Problem with space detection in an Array String

I'm currently writting a program where I try to filter extra spaces so if there are more than 1 spaces in a row, I discard the rest leaving only one
But this is only the first step because the aim of the program is to parse a txt file with mips assembly instructions.
So far I've opened the file, stored the content in a vector and then stored the vector content in an array. Then I check, if you find a char 2 times in a row shift the array to the left.
The problem is that the code works well for any other letter, except for the space character. (On the code below I test it with the 'D' character and it works)
#include <iostream>
#include <cmath>
#include <fstream>
#include <cstdlib>
#include <vector>
#include <algorithm>
using namespace std;
class myFile {
vector<string> myVector;
public:
void FileOpening();
void file_filter();
};
void myFile::FileOpening() {
string getcontent;
ifstream openfile; //creating an object so we can open files
char filename[50];
int i = 0;
cout << "Enter the name of the file you wish to open: ";
cin.getline(filename, 50); //whatever name file the user enters, it's going to be stored in filename
openfile.open(filename); //opening the file with the object I created
if (!openfile.is_open()) //if the file is not opened, exit the program
{
cout << "File is not opened! Exiting the program.";
exit(EXIT_FAILURE);
};
while (!openfile.eof()) //as long as it's not the end of the file do..
{
getline(openfile, getcontent); //get the whole text line and store it in the getcontent variable
myVector.push_back(getcontent);
i++;
}
}
void myFile::file_filter() {
unsigned int i = 0, j = 0, flag = 0, NewLineSize, k, r;
string Arr[myVector.size()];
for (i = 0; i < myVector.size(); i++) {
Arr[i] = myVector[i];
}
//removing extra spaces,extra line change
for (i = 0; i < myVector.size(); i++) {
cout << "LINE SIZE" << myVector[i].size() << endl;
for (j = 0; j < myVector[i].size(); j++) {
//If I try with this character for example,
//it works (Meaning that it successfully discards extra 'Ds' leaving only one.
// But if I replace it with ' ', it won't work. It gets out of the loop as soon
//as it detects 2 consecutive spaces.
if ((Arr[i][j] == 'D') && (Arr[i][j + 1] == 'D')) {
for (k = j; k < myVector[i].size(); k++) {
Arr[i][k] = Arr[i][k + 1];
flag = 0;
j--;
}
}
}
}
for (i = 0; i < myVector.size(); i++) {
for (j = 0; j < myVector[i].size(); j++) //edw diapernw tin kathe entoli
{
cout << Arr[i][j];
}
}
}
int main() {
myFile myfile;
myfile.FileOpening();
myfile.file_filter();
}
My question is, why does it work with all the characters except the space one, and how do I fix this?
Thanks in advace.
Wow. Many lines of code. I can only recomend to learn more about the STL and algorithms.
You can read the complete file into a vector using the vectors "range"-constructor and std::istream_iterator. Then you can replace one or more spaces in a string by using a std::regex. This is really not complicated.
In the below example, I do all the work, with 2 lines of code in function main. Please have a look:
#include <iostream>
#include <vector>
#include <iterator>
#include <algorithm>
#include <string>
#include <fstream>
#include <regex>
using LineBasedTextFile = std::vector<std::string>;
class CompleteLine { // Proxy for the input Iterator
public:
// Overload extractor. Read a complete line
friend std::istream& operator>>(std::istream& is, CompleteLine& cl) { std::getline(is, cl.completeLine); return is; }
// Cast the type 'CompleteLine' to std::string
operator std::string() const { return completeLine; }
protected:
// Temporary to hold the read string
std::string completeLine{};
};
int main()
{
// Open the input file
std::ifstream inputFile("r:\\input.txt");
if (inputFile)
{
// This vector will hold all lines of the file. Read the complete file into the vector through its range constructor
LineBasedTextFile text{ std::istream_iterator<CompleteLine>(inputFile), std::istream_iterator<CompleteLine>() };
// Replace all "more-than-one" spaces by one space
std::for_each(text.begin(), text.end(), [](std::string& s) { s = std::regex_replace(s, std::regex("[\\ ]+"), " "); });
// For Debug purposes. Print Result to std::out
std::copy(text.begin(), text.end(), std::ostream_iterator<std::string>(std::cout, "\n"));
}
return 0;
}
I hope, I could give you some idea on how to proceed.

Creating 2D String Vector from text file

I'm having slight trouble creating a 2D Vector of String that's created by reading values from a text file. I initially thought I needed to use an array. however I've come to realise that a vector would be much more suited to what I'm trying to achieve.
Here's my code so far:
I've initialised the vector globally, but haven't given it the number of rows or columns because I want that to be determined when we read the file:
vector<vector<string>> data;
Test data in the file called "test" currently looks like this:
test1 test2 test3
blue1 blue2 blue3
frog1 frog2 frog3
I then have a function that opens the file and attempts to copy over the strings from text.txt to the vector.
void createVector()
{
ifstream myReadFile;
myReadFile.open("text.txt");
while (!myReadFile.eof()) {
for (int i = 0; i < 5; i++){
vector<string> tmpVec;
string tmpString;
for (int j = 0; j < 3; j++){
myReadFile >> tmpString;
tmpVec.push_back(tmpString);
}
data.push_back(tmpVec);
}
}
}
However, when I attempt to check the size of my vector in my main function, it returns the value '0'.
int main()
{
cout << data.size();
}
I think I just need a pair of fresh eyes to tell me where I'm going wrong. I feel like the issues lies within the createVector function, although I'm not 100% sure.
Thank you!
You should use std::getline to get the line of data first, then extract each string from the line and add to your vector. This avoids the while -- eof() issue that was pointed out in the comments.
Here is an example:
#include <string>
#include <iostream>
#include <vector>
#include <sstream>
typedef std::vector<std::string> StringArray;
std::vector<StringArray> data;
void createVector()
{
//...
std::string line, tempStr;
while (std::getline(myReadFile, line))
{
// add empty vector
data.push_back(StringArray());
// now parse the line
std::istringstream strm(line);
while (strm >> tempStr)
// add string to the last added vector
data.back().push_back(tempStr);
}
}
int main()
{
createVector();
std::cout << data.size();
}
Live Example

(C++) Creating two dimensional array from .dat file

I am trying to define a class CDTable, which has a variable double table[4][4000] (4 by 4000 array of doubles) and some functions that carry out calculations based on the table.
My CDTable.h looks something like:
#define CDTABLE_H
class CDTable {
public:
double table[4][4000];
CDTable(string fileName);
double dLookUp(double z);
};
and a part of my CDTable.cpp looks like:
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include <ctime>
#include <stdlib.h>
#include "CDTable.h"
using namespace std;
CDTable::CDTable(string fileName) {
fstream file;
file.open(fileName.c_str(), ios::in);
string line;
int col = 0;
int row = 0;
getline(file, line); //skip the title line
while (getline(file, line, '\t')) { //for each line
istringstream ss(line);
double val;
while (ss >> val) { //read the value
table[row][col] = val; //assign the value
++col; //next column in the table
}
++row; //next line in the table
col = 0; //reset column
}
file.close();
}
int main() {
CDTable cd = CDTable("CDLookUp.dat");
return 0;
}
So, I mean to construct a CDTable object by giving it a string fileName which is a .dat file of two dimensional array (the first line of .dat file is not real data but a title line). I have other relevant methods implemented in CDTable.cpp as well, but I have not included them since they do not seem to be the problem.
CDTable.cpp compiles without any errors, but when I run it, I get Segmentation fault (core dumped).
Can somebody tell me what's wrong here? Is there a better way to convert a .dat or .txt two dimensional data to a two dimensional array of doubles in C++?