hi,
can somebody please tell me what would be the problem that it couldn't open the file? i compiled it in dev c++ and it gives me no error. the file 512.dat is in the same directory.

#include<iostream>
#include<fstream>
#include<stdlib.h>
#include<stdio.h>

const int sc = 768;

int main()
{
 int IF[sc];
 int DEF[sc];

 FILE *re1;
 re1 = fopen("512.dat","r");

 if(!re1)
 {
  // file couldn't be opened
  cerr << "Error: file could not be opened" << endl;
 }

 for(int i=0;i<sc;i++)
 {
  int j,k;
  fscanf(re1,"%d %d\n",&j,&k);
  IF[i]=j;
  DEF[i]=k;
 }
 fclose(re1);

 for(int i=0;i<sc;i++)
 {
  cout<<"IF="<<IF[i]<<" "<<"DEF="<<DEF[i]<<endl;
 }

 system("PAUSE");
 return 0;
}

thank you.

Recommended Answers

All 4 Replies

How do you know it can't open the file? Does your error message print? If so, change it to this for more details:

perror ( "Error: file could not be opened" );

thanks for the reply.

ive changed the message as you suggest, i print the error message 'no such file or directory'. ive put the 512.dat file in the same directory with the .cpp file. is there anything else i should do?

In the same directory as the .exe might be a choice.
As would the "root" directory of your project.

IDE's, when running a program, can often be told where to assume the current directory is.

amazing!

thanks very2 much...it works now :D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.