File not open?!?!

#include <iostream>
#include <iomanip>
#include <fstream>
#include <cmath>
using namespace std;

const int MAX_ARRAY_SIZE = 50;
void PrintGen(char lifeBoard[][MAX_ARRAY_SIZE], ostream& outStream, int numRowsInBoard, int numColsInBoard, int generationNum);
void NextGen(char lifeBoard[][MAX_ARRAY_SIZE], int numRowsInBoard, int numColsInBoard);
int main()
{

int min_array = 0;
int max_array = 50;
int row = 0;
int column = 0;
int gens = 0;
int generationNum = 1;
ifstream infile;
ofstream outfile;
char in_file[99];
char out_file[99];
char board[50][50] = {0};
//main variables

cout << "Enter the name of the input file: ";
cin >> in_file;
infile.open(in_file);
if (infile.fail())
{
cerr << "Error: input file not opened correctly." << endl;
return 1;
}
cout << "Enter the name of the output file: ";
cin >> out_file;
outfile.open(out_file);
if (outfile.fail())
{
cerr << "Error, output file not opened correctly." << endl;
return 1;
}
if(!(infile >> row))
{
cerr << "ERROR: Cannot read number of rows." << endl;
return 1;
}
if (row <= 0 || row > 49)
{
cerr << "ERROR: Read an illegal number of rows for the board." << endl;
return 1;
}

if (!(infile >> column))
{
cerr << "ERROR: Cannot read number of columns." << endl;
return 1;
}
if (column <= 0 || column > 49)
{
cerr << "ERROR: Read an illegal number of columns for the board." << endl;
return 1;
}
if (!(infile >> gens))
{
cerr << "ERROR: Cannot read the number of generations." << endl;
}
if (gens < 0)
{
cerr << "ERROR: Read an illegal number of generations." << endl;
}
for (int count = 0; count < row; count ++)
{
for(int c = 0; c < column; c++)
{
infile >> board[count][c];
if (infile.eof())
{
cerr << "ERROR; Not enough data in the input file" << endl;
return 1;
}
if(board[count][c] != '.' && board[count][c] != 'X')

{
cerr << "Error: Input data for initial board is incorrect" << endl;
cerr << "Location "<< count << "," << c << " is not valid." << endl;
return 1;
}
}
}
for(int k = 0; k < row; k++)
{
if(board[0][k] != '.' || board [row-1][k] != '.')
{
cerr << " ERROR: organisms are present in the border of the board, please correct your input file." << endl;
return 1;
}
}
for( int j = 0; j < row; j++)
{
if(board[j][0] != '.' || board[j][column-1] != '.')
{
cerr << " ERROR: organisms are present in the border of the board, please correct your input file." << endl;
return 1;
}
}
PrintGen(board, cout, row, column, 0);
for(int a=0; a <=gens; a++)
{
PrintGen(board, outfile, row, column, a);
NextGen(board, row, column);
}
PrintGen(board, cout, row, column, gens);
infile.close();
outfile.close();
return 0;
}

I created a file named "test" but when i input test.txt into the command promt, it gives me "ERROR: input file not opened correctly." i'm not sure why.
Is the file in the same directory as the executable?
Sometimes when you compile and run from the IDE, the files don't open properly. Try running it again but by opening the exe in the bin file of your project, and making sure the files are created in the same directory...
I created a file named "test" but when i input test.txt into the command promt, it gives me "ERROR: input file not opened correctly." i'm not sure why.


Just to be clear; did you create file name "test" with no extension and tried to open "test.txt"? I can see why that would fail... Filenames myst match exactly for them to open.
Last edited on
Your file open part of the code works, check the file name.

add this to your code
system("dir test*.*");

after
cerr << "Error: input file not opened correctly." << endl;
Topic archived. No new replies allowed.