std::string headerInfo = " ";

	//Verify stream is still in good condition
	if(streamReader->good())
	{
		streamReader->ignore(fileSize, ':');

		//work arounds =)     v------------v
		streamReader->getline(&headerInfo[0], sizeof(headerInfo), '\n');
	}
	else
	{
		//Error
	}

	return headerInfo;

The first parameter of getline wants a char* to store the data, so instead of creating an array with to many indexes(unless I add more lines of code to figure out how big i need to make my array), that wont be used, I just did this. I wanted someone to check my understanding of why this works.

a char* is a pointer that points to the address of the first index in an array of type char....So passing the address of the first index in my string works because a string is simply a char array correct?

Edit: StreamReader is a pointer to a fstream object.

Recommended Answers

All 3 Replies

Just use the other version of getline:

getline(*streamReader,headerInfo);

string is simply a char array correct?

I'm not an expert on this sort of thing, but I think the std::string class code could be implementation dependent, so I don't know to what extent you can rely on it being a simple wrapper for a C-string. The above solution gets rid of the issue altogether.

The problem is that string is not just an array of characters. First, the array of characters that it does hold is just big enough for the number of characters the string currently has. In this case, you initialize it to " " so it will contain only one character, so that won't be enough to store the result of getline. Second, taking the sizeof(string) will not give you the number of characters that the string holds, it will give you the size of the string class, which is fixed and is not related in any way to the number of characters. To get the number of characters, you would need to use headerInfo.size(). Finally, the string class in not necessarily a null-terminated array of characters, which is what getline will output. You are basically screwing with the string object in a way that will create, at best, undefined behaviour, possibly, memory corruption, at worst, a crash.

Fortunately, there is a very simple solution to this. The <string> library provides a special getline function to read a string from a stream object. It is a global function in the std namespace where you just pass the stream as the first parameter, and the rest is the same:

std::string headerInfo = " ";

  //Verify stream is still in good condition
  if(streamReader->good())
  {
    streamReader->ignore(fileSize, ':');

    std::getline(streamReader, headerInfo, '\n');
  }
  else
  {
    //Error
  }

  return headerInfo;

Thanks for the quick responses.

Edit: That is definitely a better way to do it than what I was trying. Thanks again!

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.