trying to figure out the reason for memory loss any ideas would be helpful requirements should be done however i need a working file for turn in thanks for help.
leak seems to be around line 59-61 i think in demo.cpp

Demo.cpp

/* File Name: Demo.cpp
 Chapter No. 15 - Exercise No. 5
 Programmer:         Carl Sue
 Date Last Modified: Apr 17, 2010
 
 Problem Statement: (what you want the code to do)
 A file filter reads an input file, transforms it in some way, and writes
 the results to an output file. Write an abstract file filter class that
 defines a pure virtual function for transforming a character. Create
 one subclass of your file filter class that performs encryption, another
 that transforms a file to all uppercase, and another that creates an
 unchanged copy of the original file.
	The class should have a member function
	void doFilter(ifstream &in, ofstream &out)
 that is called to perform the actual filtering. The member function for
 transforming a single character should have the prototype
	char transform(char ch)
 The encryption class should have a constructor that takes an integer as
 an argument and uses it as the encryption key.


 
 Overall Plan (step-by-step how you want the code to make it happen):
 0. create instances of subclasses
 1. read data in from a file
 2. Preform enctiption
 3. set all to uppercase
 4. transfer unchanged form input to output
 5. output done.
 etc.
 
 Classes needed and Purpose (Input, Processing, Output):
 
 
 */

#include <iostream>
#include <fstream>
#include "FileFilter.h"
#include "Encripted.h"
#include "ToUpper.h"
#include "Copy.h"

using namespace std;

int main(int argc, char * const argv[]){
	ifstream input;
	ofstream toUpper, encrypt, copy;
	ToUpper up;
	Copy cpy;
	input.open("original.txt", ios::in);
	toUpper.open("ALLUPPER.txt", ios::out | ios::trunc);
	encrypt.open("encription.txt", ios::out | ios::trunc);
	copy.open("copy.txt", ios::out | ios::trunc);
	if (input.fail()) {
		cout << "File not found";
		return 1;
	}
	cout << "Enter encription value(int):";
	int encryptionValue = 5;
	cout << "data loss here i think";
	Encripted encrypted(encryptionValue);
	up.doFilter(input,toUpper);
	cout << "Upper done...";
	cpy.doFilter(input,copy);
	cout << "copy done...";
	encrypted.doFilter(input,encrypt);
	cout << "encryption done...";


	return 0;
}

FileFilter.cpp

/*
 * FileFilter.cpp
 *
 *  Created on: May 16, 2010
 *      Author: Carl
 */

#include "FileFilter.h"

FileFilter::FileFilter() {
	// TODO Auto-generated constructor stub

}

FileFilter::~FileFilter() {
	// TODO Auto-generated destructor stub
}

void FileFilter::doFilter(ifstream &in, ofstream &out){
	vector<string> page;
	while(!in.eof()){
		char line[50];
		in.getline(line, 49, '\n');
		for (int i = 0; i < strlen(line); i++) {
			line[i] = transform(line[i]);
		}
		string strLine = line;
		page.push_back(strLine);
	}
	for (int i = 0; i < page.size(); i++) {
		out.write(page[i].c_str(), page[i].length());
	}
}

FileFilter.cpp

/*
 * FileFilter.h
 *
 *  Created on: May 16, 2010
 *      Author: Carl
 */

#ifndef FILEFILTER_H_
#define FILEFILTER_H_
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
class FileFilter {
public:
	FileFilter();
	~FileFilter();
	virtual char transform(char input) = 0;
	void doFilter(ifstream &in, ofstream &out);
};

#endif /* FILEFILTER_H_ */

rest are in zip i can add if needed

Recommended Answers

All 17 Replies

You say you think there is a memory leak. What is giving you that impression?

There is a mistake in the code, it won't stop it working and it's so glaringly obvious I'm reluctant to point it out =/

A memory leak normally involves actually allocating memory and then forgetting to delete it.

Since I see no allocations I fail to see how you could be leaking memory.

Tell us what actual errors you are getting.

Ketsuekiame may have been right may not be a memory leak it isnt throwing an error it seems to hit a cin and doesn't take it. Ive been staring at it for to long Ketsuekiame mind at least pointing me in the direction to look?

The problem I was getting at is not important, but I didn't want to point it out in case that *was* the home work ;)

You aren't closing your open file handles. It's not strictly necessary but can cause some strange behaviour including memory leaks and locked files. Generally this gets seen and the file is closed when the method exits. I just weren't sure in your case.

Now you've said you're worried about the cin call you had. Could you post how you had it before?

I would use

cin >> variable;

You can also use

cin.get();

to return the first character, but you must empty the buffer to get more as subsequent calls to cin.get(); will return the first character you entered.

Apologies for seemingly being rude in my first post. It wasn't my intention, but after re-reading it I can see how it may be interpreted that way.

no worries Ketsuekiame ok i added the close function cant believe i missed that again been staring at this code for to long working at about a months work of home work due Tuesday :D the home work part of it was the file i/o however i do miss stuff like close when im tired and need to sleep cant figure out why but adding the close still isn't fixing the issue of the mem hoging its just running in circles eating my memory really quickly any ideas heres the moded demo.cpp

#include <iostream>
#include <fstream>
#include "Encripted.h"
#include "ToUpper.h"
#include "Copy.h"

using namespace std;

int main(int argc, char * const argv[]){
	ifstream input;
	ofstream toUpper, encrypt, copy;
	ToUpper up;
	Copy cpy;
	input.open("original.txt", ios::in);
	toUpper.open("ALLUPPER.txt", ios::out);
	encrypt.open("encription.txt", ios::out);
	copy.open("copy.txt", ios::out);
	if (input.fail()) {
		cout << "File not found";
		return 1;
	}
	cout << "Enter encription value(int):";
	int encryptionValue = 5;
	Encripted encrypted(encryptionValue);
	up.doFilter(input,toUpper);
	cout << "Upper done...";
	cpy.doFilter(input,copy);
	cout << "copy done...";
	encrypted.doFilter(input,encrypt);
	cout << "encryption done...";
	input.close();
	toUpper.close();
	encrypt.close();
	copy.close();

	return 0;
}

Ok, I see the problem.

It's the way that you are handling the file reads. Please look at how getline handles what you are asking for.

Google will help you there :)

Actually I was incorrect...

I've just tried running your code with a few debugging additions.

It appears ofstream.open does NOT behave as documented. ofstream.open is returning error code 183. Which means
"Cannot create a file when that file already exists."

However, the expected behaviour is that it should open the file and empty it.

I'm not sure why this is, I generally use FILE for working with files. I'll look into it.

Maybe, opening the files with ios::trunc opening mode open would help.

Ah my bad, I have my custom VS plugin set to throw when "GetLastError" changes ^^

I can read and write to the files fine, but your getline method in FileFilter doesn't work properly.

=)

Maybe, opening the files with ios::trunc opening mode open would help.

ios::out implies ios::trunc when used with ofstream.

My problem was a plug-in setting I was using, as for the original problem. That lies within the FileFilter class and the getline loop.

OP: Just another point, you aren't capitalising A's and Z's :) Check your if statement again.

Hmm changed the input type to getline and it still hasn't fixed the issue, Ketsuekiame you said it worked on your end at one point for writing to the file maybe its an OS specific issue. thanks for the help if you see anything let me know

ok its not OS speciffic

for (int i = 0; i < strlen(line); i++) {
    line[i] = transform(line[i]);
  }
  line[i] = '\0'; // Bug: this line is missing
  string strLine = line;

UncleLeroy you seem to be missing code should be...

void FileFilter::doFilter(ifstream &in, ofstream &out){
	cout << " got here";
	vector<string> page;
	while(!in.eof()){
		char line[50];
		in.getline(line, 49, '\n');
		for (int i = 0; i < strlen(line); i++) {
			line[i] = transform(line[i]);
		}
		string strLine = line;
		page.push_back(strLine);
	}
	for (int i = 0; i < page.size(); i++) {
		out.write(page[i].c_str(), page[i].length());
	}
}

Ok here's another hint.

Your getline method is not incrementing the position in the stream and because of that, the eof bit is never set. You need to find another way of checking you're at the end of the file and moving the stream position.

@UncleLeroy: The getline method automatically appends the null terminator. getline(*buffer, nCount, delimiter)

getline retrieves nCount - 1 characters up to the delimiter and then appends the null terminator.

sweet that was the issue just out of curiosity what would that error be called? thanks Ketsuekiame ill add the final zip file when done for reference.

To be honest, I have no idea what caused it. I think it's just a case of "One of Those Things" ^^

It *may* have been because you were asking getline to terminate on newline, which getline does anyway, so it might have been getting stuck after the first line.

The could have been an error deeper in the IO code that wasn't being flagged, so I'm not entirely sure. But it is strange behaviour to be sure.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.