-
Dev-C++4
Hi. I want to be able to get data from a file into my program so that the program can do work on the data set. I am using devc++4. This program should get data from file "datafile" and print it out, but I always get back the error "Can't open data file." that's in the routine. I'm wondering what else I have to do find the data file from within the program. I have attached "datafile".
Code:
#include <iostream>
#include <iomanip>
#include <fstream>
#include <stdlib.h>
int main()
{
double a[100];int i,N;
cout<<"Type in the number of data points"<<endl;cin>>N;
if(N<=64)
{ //Read in data from datafile
ifstream source;
source.open("datafile");
if(!source)
{cerr<<"Can't open data file."<<endl;
system("PAUSE");
return(1);}
for(i=0;i<N;i++)
{source>>a[i];}
source.close();
}
else
{cerr<<"Too many data points."<<endl;return (1);}
int k,j=0,colnum=8; //Print out data set
for(j=0;j<N;j=j+colnum){
k=colnum;
if((j+colnum)>N){k=N-j;}
for(i=0;i<k;i++){cout<<setw(8)<<a[i+j];}
cout<<endl;}
system("PAUSE");
return 0;
}
-
Re: Dev-C++4
Some minor errors (It surprises me that it compiled at all, in fact):
Code:
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstdlib> // The non-deprecated version
using namespace std; // Not the best way to do things, but easiest for beginners
int main()
{
double a[100];int i,N;
cout<<"Type in the number of data points"<<endl;cin>>N;
if(N<=64)
{ //Read in data from datafile
ifstream source;
source.open("datafile");
if(!source)
{cerr<<"Can't open data file."<<endl;
system("PAUSE");
return(1);}
for(i=0;i<N;i++)
{source>>a[i];}
source.close();
}
else
{cerr<<"Too many data points."<<endl;return (1);}
int k,j=0,colnum=8; //Print out data set
for(j=0;j<N;j=j+colnum){
k=colnum;
if((j+colnum)>N){k=N-j;}
for(i=0;i<k;i++){cout<<setw(8)<<a[i+j];}
cout<<endl;}
system("PAUSE");
return 0;
}
-
>Follow that with the following call
perror( "Reason for fault" );
This gives me back an error at compiling of "implicit declaration of function `int perror(...)'
-
>At the moment, I guess its trying to find the file in the same directory as the executable.
Got that, but "datafile" is saved in the same folder as my program executable and it can't find it.
-
Sorry, yes. "No such file or directory".THX
-
getcwd() statement says cwd is file where program executable sits. "datafile" is in same folder but I still get "no such file or directory" when I try to get the data. I did find a forum for Dev-C++ on sourceforge though, maybe this is a software specific question?
-
Thanks for all the help Salem. It turned out that I needed to call the file "datafile.txt" instead of "datafile". This will really help lots of my applications--Best
-
I tried your code and modified a thing.
Than I get this funny result:
If you change IF(!source) to IF(source) than the prgram displays all kindof weird (possible hexadecimal) stuff. =)
-
How is that odd? By making that change, you read the stream only if it is bad (instead of only if it is good). Reading a bad stream is unpredictable.