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

Click For Summary

Homework Help Overview

The discussion revolves around a C++ runtime error encountered while attempting to read data from a file named "priorities.dat". The original poster describes issues with exception errors occurring when trying to extract information from the file using the MS Visual C++ .NET 2003 environment.

Discussion Character

  • Exploratory, Assumption checking, Problem interpretation

Approaches and Questions Raised

  • Participants discuss the potential pitfalls of using the extraction operator with character pointers, suggesting that it may lead to buffer overflow issues. There are recommendations to use functions like "get" and "getline" for safer input handling. Some participants also mention the importance of error checking between reads and consider the implications of using strings versus character arrays.

Discussion Status

Several participants have provided insights and alternative approaches to handling file input, including code snippets that demonstrate working solutions. There is an acknowledgment of the original poster's challenges, but no consensus has been reached regarding the specific cause of the runtime error.

Contextual Notes

Participants note that the original poster's memory allocation practices may have contributed to the issues, and there is mention of the constraints posed by the original code structure and the environment being used.

enigma
Staff Emeritus
Science Advisor
Gold Member
Messages
1,739
Reaction score
20
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
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 "count" to make sure you're getting what you think you're getting.
 
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 "count" to make sure you're getting what you think you're getting.

What Hurkyl said, he beat me to this.
 
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.
 
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:

Similar threads

  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 14 ·
Replies
14
Views
4K
  • · Replies 1 ·
Replies
1
Views
3K
Replies
23
Views
6K
  • · Replies 7 ·
Replies
7
Views
4K
  • · Replies 7 ·
Replies
7
Views
4K