Hi there,

I am trying to make a program, as shown below. I have used a snippet with hello and bye as the program is really very big. Unfortunately I am unable to edit these functions as they are all part of a complex linked list.

I am trying to pass both functions from the class CWrite into a text file however what currently occurs is the second function overwrites the first function (i think). I have genuinly no idea what to do to solve this.

Any help fixing this would be greatly appreciated.

#inlcude <iostream>
#include <fstream>
#include <sstream>

class CWrite
{
public:
	int write(std::string name);
	int write(std::string name);
};

#include “stdafx.h”
#include “output.h”

int CWrite::write(std::string name)
{
	std::ofstream output(name.c_str());
	output<<”HELLO”<<std::endl;
output.close();
return 0;
}

int CWrite::write(std::string name)
{
	std::ofstream output(name.c_str());
	output<<”HELLO”<<std::endl;
output.close();
return 0;
}

Private: System::Void ButOut_Click(System::object^ sneder, System::eventArgs^ e)
{
	SaveFileDialog ^ saveFileDialog1 = gcnew SaveFileDialog();
	savefileDialog1->Filter = “txt files (*.txt)|*.txt|All files (*.*)|*.*”;
	saveFileDialog1->InitialDirectory = “library”;

	if(saveFileDialog1->ShowDialog() == System::Windows::Forms::DialogResult::Ok)
	{
		CWrite a;
		std::string std_name = SystemToStdString(saveFileDialog1->FileName);
		int success = a.write(std_name) + a.writeSDF(std_name);
}

Please ignore any syntax errors as the code had to be written from Word.

Thanks in advance,

Andy

Hi, thanks for this. I have had a look at the webpage and this does appear to be what I want however I am having real difficulty trying to implement the code within the above example. Like I say this could probably be done much better ways but all the groundwork has already been laid with a lot of linked lists.

Thanks again in advance for any help, is really appreciated.

Well I have tried doing this all day it seems. This is where i am at the moment. This is just the code for the save dialog box to make it save.

It appears the first part goes in well but it keeps producing an exception error post compilation and after the button is pressed; leaving a text file that says HELLO only.

CWrite a;
std::string std_name = SystemToStdString(saveFileDialog1->FileName);
a.write(std_name);
a.writeSDF(std_name.append(NULL));

Just to clarify SystemToStdString is just a function that converts system string to std string.

Thnks in advance.

I believe you want this as your second parameter in your ofstream object in your CWrite function std::ios_base::app

I may have got it completely wrong what you are saying (I really dont have too much experience with programming) but I can't change the design of CWrite really, only the dialog box actions.

So do you reckon this means it is not possible to accomplish what I am after?

Thanks again for all the help.

std::ofstream output(name.c_str(),std::ios_base::app);

would be the only modification. I'm just confused since you did modify the code to output hello in that function. That's the only way I can think of but perhaps someone has a better method.

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.