so I'm just starting to learn fileIO. I've been using a program called CodeRunner on my mac and I have a folder that contains:
validsudoku.cpp, validsudoku, sudokugood0.txt
The beginning of the code I wrote is:
int main(int argc, char const *argv[]){
//string filetoopen;
ifstream sudokutxtfile;
string txtline;
string sudokubox[9];
//bool goodsudoku = true;
//int i, j, row, column;
/*
if (argc == 2)
filetoopen = argv[1];
else
filetoopen = "sudokuboard.txt";
*/
//read in file, save to array, close file
sudokutxtfile.open("sudokugood0.txt");
while (getline(sudokutxtfile,txtline))
{
sudokubox[row] = txtline;
row++;
}
sudokutxtfile.close();
Right now to test this I just have the file to open as "sudokugood0.txt", although once I get this working I'll change it to my 'filetoopen' variable so I can terminal input the file name.
Now onto my issue:
When I run the .cpp in CodeRunner it runs correctly sees the .txt file and processes it, but when I put the executable and the .txt file in my bin folder and try to run it from terminal it doesn't see the .txt file. Am I using the wrong location or am I missing something else?
Side question: It runs in OSX, but in my Xubuntu VMbox using Codeblocks that I'm required to use for class I get 'Segmentation fault (core dumped)' Anyone know why? I have this at the top of my file for both:
#include <iostream>
#include <fstream>
#include <sstream>
using namespace std;
The program tries to find the file you open in the current working directory, not from the location where the executable file is. When you're executing a program from a terminal the current directory is the directory you're in. So if you do e.g. cd ~/ to go to your home-directory, and then run the program, the program will look for the file in your home-directory. If you change to some other directory, the program will look for the file in that new directory.
The natural solution is either to pass the input file as an argument to the executable, or to ask for it as input (I recommend the first).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With