C++ Runtime Error Fix: Help Find What I've Done Wrong

In summary, the code is trying to open a file for input, but as soon as it tries to read from the file, it gives exception errors.
  • #1
enigma
Staff Emeritus
Science Advisor
Gold Member
1,757
17
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:
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:
     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
 
Physics news on Phys.org
  • #2
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.
 
  • #3
Originally posted by Hurkyl
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.

What Hurkyl said, he beat me to this.
 
  • #4
Code:
#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 I am not using your process class, this code is in good working order.
 
  • #5
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:

1. What is a C++ runtime error?

A C++ runtime error is an error that occurs while a program is running, typically due to a mistake in the code. It can cause the program to crash or behave unexpectedly.

2. How can I fix a C++ runtime error?

To fix a C++ runtime error, you need to identify the specific line of code causing the error and then make appropriate changes to the code. This may involve debugging and troubleshooting techniques such as stepping through the code, using error messages, and checking for common mistakes.

3. Why am I getting a C++ runtime error?

There are many possible reasons why you may be getting a C++ runtime error. Some common causes include memory issues, undefined variables, and incorrect syntax. It is important to carefully review your code to identify the specific cause of the error.

4. Can a C++ runtime error be prevented?

While it is not possible to completely prevent all runtime errors, there are steps you can take to reduce the likelihood of encountering them. These include writing well-structured and error-free code, using debugging tools, and regularly testing and optimizing your code.

5. What should I do if I cannot fix a C++ runtime error?

If you are unable to fix a C++ runtime error on your own, it may be helpful to seek assistance from a more experienced programmer or consult online resources and forums for advice. You may also consider using a different programming language or seeking alternative solutions for your project.

Similar threads

  • Programming and Computer Science
Replies
2
Views
2K
Replies
58
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
14
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
Replies
23
Views
5K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
7
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
7
Views
3K
Back
Top