// In this little project I am trying to learn to get and manipulate data from
// a file input using fstream strings I also wanted to use the formating available
// with the printf and related string handling methods. I have a question in regards
// to the two usages that I have shown in order to get char * from std::string.
// Is there a differance between the two things I have done?
// Is one safer than the other because of allocatin for size?
// Does it matter?
// Does anyone have any comments or suggestions regarding this code?
// I have a severe case of the newbies, and this is just getting my toes wet
// a little bit, so bear with me here.
// Thank You for Your Kind indulgence.
// 73
// -Grace
// z

#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

string STRING;
char * ptrNewBuffer = new char[STRING.size() + 1];

const char* StringBuffer;

int main ()
	ifstream infile;
	ofstream outfile;

	infile.open ("ReadWriteLine.cpp");

	while (!infile.eof())
		// infile is this file since it changes and it's fun
		getline(infile, STRING);

		// method 1 
		strcpy(ptrNewBuffer, STRING.c_str());
		printf("%s\n", ptrNewBuffer);	// output once

		// method 2
		StringBuffer = STRING.c_str();
		printf("%s\n", StringBuffer);	// now output twice

//was	cout<<STRING<<endl;

		// experiment with output file
		outfile<<STRING<<endl; //try replace cout eith outfile


// -eof-

There is an issue in how you went about creating the array of char:

char * ptrNewBuffer = new char[STRING.size() + 1];

At the point of creation of this array of char, what is the size of STRING? Later in the code, you will repeatedly attempt to copy a line of text into this array. As such, you need this array to be at least one char bigger than each line of text you copy across.

Given that you create this array before you even read the first line, it's not looking good. Your strcpy call is stomping over memory that isn't yours to stomp over.

That aside, for the purposes of output using printf, there's not much difference in your methods. Be aware, however, that the pointer you get back from c_str() points into the C++ string object itself, so if you did now use that pointer (i.e. your copy of it, StringBuffer) to write characters, you'd be changing the actual string object. Your strcpy version creates your own separate copy that you can change without affecting the C++ string object.

Clearly, the second method in which you simply grab a copy of the pointer to the char array buried deep inside the string object is safer, as you are actually stomping memory every time you call strcpy, since your pointer ptrNewBuffer is pointing to an array of char of probable size one! Running your code using valgrind (which keeps an eye on such things) causes it to scream loudly about using memory that it really shouldn't be using.

strcpy is often considered less safe than some alternatives; look into strncpy, for example, in which you can set the maximum number of char to be copied (which should ths be set to the size of the char array, ensuring that you can't write beyond the array and stomp some memory).

Thank You for a very good explanation! That was about what I suspected. You have confirmed it - Many thanks and Merry Christmas to You and Yours! Good luck to You in all the new years ahead!


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.