Opening a file... yes i know..

Worn topic but still. My program complies without error, but when I run it, my check statement returns the message that the file cannot be opened.

This file is in the same folder as the program, it is named simple.dat because deep in my heart I know the solution to this is simple.

Heres the code to the main:

#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
using namespace std;
const int SIZE = 10;
void read2arrays (istream &, int [], int [], int &);
bool isitavalidarray(int [], int);
void findrange(int [], int);
void sum2arrays (int [],int [],int [],int);
void subtract2arrays (int [], int [], int [], int);
void printresults(int [], int [], int [], int [], int);
int main()
{

int test1[SIZE], test2[SIZE], k;
int newvalues1[SIZE], newvalues2[SIZE];

ifstream infile;
infile.open("simple.dat", ios::in);

if(!infile.is_open()) {

cout << "Error opening file simple.dat" <<endl;
exit(1);

}

read2arrays(infile, test1, test2, k);

infile.close();

return 0;

}

i do not post the functions because my problem comes before main even gets to any function.
Try creating a file instead and make sure that the file is indeed in the proper directory. Especially in cases where you are using an IDE, it can be hard to figure out what the actual working directory is.
Zhuge this is how I did it the first time. i created the file. And its in the same directory as the main program i made sure of it.

Yanson.. i am not sure what i should derive from the posted article
Yanson is telling you to put your code inside of code tags when you post it on the boards. It makes it much easier for us to read.
When starting a new topic you have this by default:
Write your question here.

[code]
  Put the code you need help with here.
[/code]


I can absolutely not understand how people mess that up.
Anyway.

i do not post the functions because my problem comes before main even gets to any function.

So it just prints this?
Error opening file simple.dat
From Zhuge:
Especially in cases where you are using an IDE, it can be hard to figure out what the actual working directory is.


Try coding the full path to the file. If that works that would be a pretty good indication that the program executable is not in the same directory as simple.dat.

If you are using Visual Studio on Windows you will need to escape the backslash character in the path, i.e., "C:\\ProgramDirectory\\simle.dat" not "C:\ProgramDirectory\simle.dat" or just use a forward slash "C:/ProgramDirectory/simle.dat"

Generally in Visual Studio under the project directory you will find both a Release and a Debug directory which is where the executables reside.

Other IDEs use there own scheme.
Topic archived. No new replies allowed.