1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

C++ runtime error

  1. Oct 31, 2003 #1

    enigma

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    Maybe someone can help find what I've done wrong.

    I'm using Windows XP, 2.3GHz, 80GB, 256MB, Student Version of MS Visual C++ .NET, 2003 version

    I've opened a file up for input, which works fine, but as soon as I try to pull information from the file, it gives exception errors in memory addresses.

    If someone can see a blazing error, or knows the fix for this problem, I'd be appreciative.

    Relevant code:
    Code (Text):

    main()
    {
        // ---------------------------------------------
        // Opens file: priorities.dat for
        //

        ifstream inProcesses("priorities.dat", ios::in);

        if (!inProcesses)
        {
            cerr<<"File could not be opened"<<endl;
            exit(1);
        }

        // Ignore first line from file stream
        clearLine( inProcesses );

        // --------------------------------------------
        // Set up Process pointer array

        Process *dataPtr[30]; // Read up to 30 processes

        int i=-1;

        do
        {
            dataPtr[++i] = new Process();

        }while(inProcesses>>*dataPtr[i]);

    .
    .
    .
    }

    void clearLine ( ifstream &input ) // Ignores a line from file stream
    {
        char dump[20];

        input>>dump>>dump>>dump>>dump>>dump>>dump>>dump>>dump>>dump;
            // This is the line where it quits.
    }

    // Overloaded input operator from 'Process' object code
    istream &operator>>( istream &input, Process &a )
    {
        input>>a.UserID>>a.ProcessID>>a.PPID>>a.CLS
            >>a.Priority>>a.STime>>a.TTY>>a.Time>>a.ProcessName;
            // This is the line which it dumps me if I comment out the first one.

        return input;
    }

     
    This is the file it's trying to read:
    Code (Text):

         UID   PID  PPID CLS PRI    STIME TTY   TIME CMD
       johny 27708 27707  TS  48 02:58:05 pts/4 0:00 -tcsh
       mikey 27795 27708  TS  48 02:58:52 pts/4 0:00 pine
      smitty 27846 27846  TS  40 02:59:12 pts/4 0:00 blah1
      ralphy 27915 27916  TS  55 02:59:13 pts/4 0:00 blah2
       carly 27925 27926  TS  12 02:59:13 pts/4 0:00 blah3
     
     
  2. jcsd
  3. Oct 31, 2003 #2

    Hurkyl

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    Using the extraction operator to read a "char*" is, in general, a bad idea. It just screams, "buffer overflow!" I also notice you don't do any error checking on your stream between reads, another bad idea.

    That being said, assuming you've opened the right file, I can't see why it would die on you...

    Anyways, the thing you need to investigate is the stream functions "get" and "getline". Generally, you want to use one of these functions to try and grab the contents of an entire line, then use some other means to read the individual tokens, such as string manipulation.


    The alternative is to use "string". I don't know how well this works since I tend to avoid using both string and the stream extraction operators (but then again, I've been working on applications where I'm worried about efficiency, so I handle these details myself).


    But even if this works, I bet we'll all still be curious what was causing your program to fail. :smile: What I would do in a situation like this is to do the reads one at a time and print them to "cout" to make sure you're getting what you think you're getting.
     
  4. Oct 31, 2003 #3
    What Hurkyl said, he beat me to this.
     
  5. Oct 31, 2003 #4

    dduardo

    User Avatar
    Staff Emeritus

    Code (Text):

    #include <iostream>
    #include <string>
    #include <fstream>

    using namespace std ;

    struct process
    {
        string UID ;
        string PID ;
        string PPID ;
        string CLS ;
        string PRI ;
        string STIME ;
        string TTY ;
        string TIME ;
        string CMD ;
    } ;

    istream &operator >> (istream &input , process &p )
    {
        input >> p.UID >> p.PID >> p.PPID >> p.CLS >> p.PRI
           >> p.STIME >> p.TTY >> p.TIME >> p.CMD ;

        return input ;
    }

    void remove_line( istream &input )
    {
        string line ;
        getline( input , line ) ;
    }

    int main( void )
    {
        ifstream infile("priorities.dat" , ios::in ) ;

        if( infile.fail() )
        {
            cerr << "File could not be opened" << endl ;
            exit(1) ;
        }

        remove_line( infile ) ;

        process *data[30] ;
        int pcounter = 0 ;

        while( true )
        {
            if( infile.eof() ) break ;

            data[pcounter] = new process ;
            infile >> *data[pcounter] ;
           
            pcounter += 1 ;
        }

        return 0 ;
    }
     
    Although im not using your process class, this code is in good working order.
     
  6. Oct 31, 2003 #5

    enigma

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    Meh

    It was my own stupid error. I wasn't allocating memory correctly, which was brought about by me trying to do:

    char a[] = char b[x],

    instead of:

    strcpy(a,b)

    and changing my variable definitions to pointers to a single memory address to pass the compiler errors for

    char a = char b lines.

    ... been a long time since I've done significantly large programs in C++. Matlab has been spoiling me...:frown:
     
    Last edited: Oct 31, 2003
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?