file not opening correctly

This is a discussion on file not opening correctly within the C++ Programming forums, part of the General Programming Boards category; I am trying to get a file to open that the user puts in. "filename" is the variable for the ...

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    416

    file not opening correctly

    I am trying to get a file to open that the user puts in. "filename" is the variable for the name of the file the user wants to open and read data from. The code compiles and works, but does not open the file correctly, and so the user can't get what is from whatever file it is the user is trying to open. It jumps to the else at the bottom and says it cannot open the file. The file is in the same folder/directory as the program.

    also, is there a way to make it stop reading in the data once there is not more data in the file? It is just reading in two sets of numbers, (x,y) coords.

    Code:
    #include <cstdlib>
    #include <iostream>
    #include <string>
    #include <conio.h>
    #include <cmath>
    #include <fstream>
    #include <ctime>
    
    using namespace std;
    
    void delaysec(double seconds)
    {
        time_t startTime, stopTime;
        double delTime;
        time( &startTime );
        do{
           time( &stopTime );
           delTime = difftime( stopTime, startTime );
        }while( delTime< seconds );
    }
    
    double distance(double x1, double y1, double x2, double y2)
    {
        double dist;       
        dist = sqrt((x1 - x2)*(x1 - x2) + (y1 - y2)*(y1 - y2));
        return dist;
    }
    
    int main(void){
        
        double x[200], y[200], x1, y1, x2, y2;
        int m, n, o, p;
        char filename[45];
        double dist1, dist2, dist3;
        
        m = 0;
        n = 0;
        o = 0;
        p = 0;
        
        ifstream file;
        
        cout<<"Enter file name: ";   <--- from here
        cin>> filename[45];
        
        file.open(filename);
        
        if(file.is_open()){   <--- to here is where i'm having trouble
        
            file >> x[n] >> y[n];
        
            x1 = x[200];
            y1 = y[200];
        
            dist1 = distance(0.0,0.0,x1,y1);
        
            if(dist1 > 46.0){
                cout<<"OUT OF RANGE";
                m = 1;
            }
        
            dist2 = distance(0.0,0.0,x1,y1);
        
            if(dist2 < 6.0){
                cout<<"TOO CLOSE TO CENTER";
                m = 1;
            }
        
            dist3 = distance(-10.0,-10.0,x1,y1);
        
            if(dist3 < 3.0){
                cout<<"ROBOT HOME; ENDING PROGRAM";
                m = 1;
            }
        
            if(m != 1){
                cout<<"(" << x1 <<", " << y1 <<")"<< endl;
            }
        
            delaysec(2);
        
        
            while(p < 1 && o == 0){
                n++;
                m = 0;
            
                if(x == 0 && y == 0){
                    o = 1;
                }
            
                dist1 = distance(0.0,0.0,x1,y1);
            
                if(dist1 > 46.0){
                    cout<<"OUT OF RANGE";
                    m = 1;
                }
            
                dist2 = distance(0.0,0.0,x1,y1);
            
                if(dist2 < 6.0){
                    cout<<"TOO CLOSE TO CENTER";
                    m = 1;
                }
            
                dist3 = distance(-10.0,-10.0,x1,y1);
            
                if(dist3 < 3.0){
                    cout<<"ROBOT HOME; ENDING PROGRAM";
                    m = 1;
                }
            
                if(m != 1){
                    cout<<"(" << x <<", " << y <<")" << endl;
                }
            
                delaysec(2);
                
            }
        }
        
        else{
            cout<<"could not open file specified";
        }
        
        while(!_kbhit());
    }
    Last edited by scwizzo; 04-03-2007 at 09:12 PM.

  2. #2
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    The problem is you're only reading one character from cin (into index 45 of filename--which is out of range, by the way)

    Try:
    Code:
    cin>>filename;
    Or even better, to avoid buffer overflows, you could use std::string or cin.getline; e.g.:
    Code:
    cin.getline(filename,45);
    //or
    std::string filename;
    cin>>filename;
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    >> is there a way to make it stop reading in the data once there is not more data in the file?
    Use a loop. In the case of reading from a stream, it is usually best to use the read as the control for the loop so that the loop will break if there are any errors or if the end of the file is read.
    Code:
    while (file >> x[n] >> y[n])
    Note that you still have more work to do inside that loop, there are some issues in there that will need to be fixed.

  4. #4
    Registered User
    Join Date
    Mar 2007
    Posts
    416
    Those still didnt work, i get as the data. The first line should be (-10,-10)

    Code:
    Enter file name: locations.txt
    (8.59008e-298, -10)
    (0x22f920, 0x22f2e0)
    (0x22f920, 0x22f2e0)
    (0x22f920, 0x22f2e0)
    (0x22f920, 0x22f2e0)
    (0x22f920, 0x22f2e0)
    (0x22f920, 0x22f2e0)

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    As I said, there are still a number of issues in your code that need to be fixed. I'd consider taking out most of it (save it somewhere in a temporary file). Then start over adding only a few lines at a time and then compiling and running them to see if they work.

    Just to get you started, here is the first thing I see at a glance:
    Code:
    x1 = x[200];
    x is an array with a size of 200, so x[200] is out of bounds. Also, you never initialized anything but x[n], so x[200] would have random data anyway.

  6. #6
    Registered User
    Join Date
    Mar 2007
    Posts
    416
    I went through it again, and fixed what seemed like a problem. Here's the new int main function. It works the way I want it to now, thank you.

    Code:
    int main(void){
        
        double x[200], y[200], x1, y1, x2, y2;
        int m, n, o, p;
        char filename[45];
        double dist1, dist2, dist3;
        
        m = 0;
        n = 0;
        o = 0;
        p = 0;
        
        ifstream file;
        
        cout<<"Enter file name: ";
        cin.getline(filename,45);
        
        file.open(filename);
        
        if(file.is_open()){
        
            file >> x[n] >> y[n];
        
            x1 = x[n];
            y1 = y[n];
        
            dist1 = distance(0.0,0.0,x1,y1);
        
            if(dist1 > 46.0){
                cout<<"OUT OF RANGE";
                m = 1;
            }
        
            dist2 = distance(0.0,0.0,x1,y1);
        
            if(dist2 < 6.0){
                cout<<"TOO CLOSE TO CENTER";
                m = 1;
            }
        
            dist3 = distance(-10.0,-10.0,x1,y1);
        
            if(dist3 < 3.0 && n != 0){
                cout<<"ROBOT HOME; ENDING PROGRAM";
                m = 1;
            }
        
            if(m != 1){
                cout<<"(" << x1 <<", " << y1 <<")"<< endl;
            }
        
            delaysec(2);
        
        
            while(p < 1 && o == 0){
                n++;
                m = 0;
                
                file >> x[n] >> y[n];
                
                x1 = x[n];
                y1 = y[n];
            
                dist1 = distance(0.0,0.0,x1,y1);
            
                if(dist1 > 46.0){
                    cout<<"OUT OF RANGE" << endl;
                    m = 1;
                }
            
                dist2 = distance(0.0,0.0,x1,y1);
            
                if(dist2 < 6.0){
                    cout<<"TOO CLOSE TO CENTER" << endl;
                    m = 1;
                }
            
                dist3 = distance(-10.0,-10.0,x1,y1);
            
                if(dist3 < 3.0){
                    cout<<"ROBOT HOME; ENDING PROGRAM" << endl;
                    m = 1;
                    o = 1;
                }
                
                if(m != 1){
                    cout<<"(" << x1 <<", " << y1 <<")" << endl;
                }
            
                delaysec(2);
                
            }
        }
        
        else{
            cout<<"could not open file specified";
        }
        
        while(!_kbhit());
    }

  7. #7
    Registered User
    Join Date
    Mar 2007
    Posts
    416
    One last issue. I want the program to stop if it does not end within 3 units of (-10,-10). Right now, if the program says a coordinate is out of range as the last input, it will constantly say out of range without stopping. Is there a way to flag it so the program will know to stop?

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Hint: break exits from a loop.
    Code:
    while(1) {
        puts("before");
        break;
        puts("middle");
    }
    
    puts("after");
    
    /* output
    before
    after
    */
    Sorry for the use of puts() . . . this keyboard's broken and can't type less-than symbols for cout.

    Also see http://www.cprogramming.com/tutorial/c/lesson3.html (I know that's the C lesson, the C++ lesson doesn't have a break section).
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. Need Help Fixing My C Program. Deals with File I/O
    By Matus in forum C Programming
    Replies: 7
    Last Post: 04-29-2008, 07:51 PM
  3. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM

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