Dev-C++4

This is a discussion on Dev-C++4 within the C++ Programming forums, part of the General Programming Boards category; Hi. I want to be able to get data from a file into my program so that the program can ...

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    22

    Question 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;
    }

  2. #2
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571

    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;
    }

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    22
    >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(...)'

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    22
    >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.

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    22
    Sorry, yes. "No such file or directory".THX

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    22
    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?

  7. #7
    Registered User
    Join Date
    Apr 2003
    Posts
    22
    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

  8. #8
    Registered User
    Join Date
    Apr 2003
    Posts
    50
    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. =)
    I dont know half as many of you half as well as I should like, and I like less than half of you half as well as you deserve...

  9. #9
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. New to Dev C++/<windows.h>...
    By Cilius in forum C++ Programming
    Replies: 3
    Last Post: 02-23-2005, 01:05 AM
  2. Glut and Dev C++, Programs not Quitting?
    By Zeusbwr in forum Game Programming
    Replies: 13
    Last Post: 11-29-2004, 08:43 PM
  3. openGL programming - From MSVC++ to Dev C++
    By WDT in forum Game Programming
    Replies: 3
    Last Post: 03-08-2004, 08:47 PM
  4. openGL programming - From MSVC++ to Dev C++
    By WDT in forum Game Programming
    Replies: 1
    Last Post: 03-08-2004, 05:19 PM
  5. DEV C++ Limitations?
    By Kirdra in forum Game Programming
    Replies: 3
    Last Post: 09-09-2002, 10:40 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21