C++ what is in string after std::istreambuf_iterator< char >(sourceFile))

Started by
16 comments, last by Servant of the Lord 7 years, 8 months ago
You seem to keep wanting to break back to C'ish behavior. You'll be much happier if you stick to idiomatic C++ instead. Mixing the two is begging for problems.
#include <string>
#include <fstream>
#include <iostream>
#include <iomanip>
#include <sstream>

#define infile "in.txt"

std::stringstream loadFromFile(const std::string& filename) {
  std::ifstream sourceFile(filename);
  std::stringstream ss;

  if(!sourceFile) {
    ss << "Error opening file [" << filename << "].\n";
    throw std::runtime_error(ss.str());
  }

  ss << sourceFile.rdbuf();
  return ss;
}

int main() {
  try {
    std::stringstream stream = loadFromFile(infile);

    std::string line;
    int lineNum = 1;
    while(std::getline(stream, line)) {
      std::cout << std::setfill('0') << std::setw(6) << lineNum++ << "] " << line << "\n";
    }
  }
  catch(std::exception& e) { std::cerr << e.what() << "\n"; }

  std::cin.get();
}
void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.
Advertisement

thanks for the reply.

I am not interested in inputting from a file os ascii of text that end each lines with a '\n' and the end of the string with '\0'. the string is in that format in the ram, not a file.

However, if you can process the line more efficiently in print_string(), Or in search for the '\n' char with some c++'s func then it would be really greatly appreciated.

You seem to keep wanting to break back to C'ish behavior. You'll be much happier if you stick to idiomatic C++ instead. Mixing the two is begging for problems.

I am only writing this small program to use in my Opengl project. OpenGL shader are written in C. hence the C codes.

You seem to keep wanting to break back to C'ish behavior. You'll be much happier if you stick to idiomatic C++ instead. Mixing the two is begging for problems.

I am only writing this small program to use in my Opengl project. OpenGL shader are written in C. hence the C codes.

Actually, they're not written in C, but even if they were, what you've written makes no sense.

This "small program" is using C++ (string, ifstream are C++ and won't work in C). You can use this program to read and write shaders, and it doesn't matter what language you write it in ..... java, C#, python ... it's just basic file manipulation and any decent language can do that.

if you think programming is like sex, you probably haven't done much of either.-------------- - capn_midnight
Using C++-style casts, instead of dangerous C-style casts, would've also helped highlight the problem.

thanks for the reply.

can you give me an example of what you would do?

Using C++-style casts, instead of dangerous C-style casts, would've also helped highlight the problem.


thanks for the reply.

can you give me an example of what you would do?

Fast code is important, but you should write clean code first. Clean code can always be made faster when needed, but fast code is harder to make clean. Clean code contains less bugs, and is easier to find the bugs when they do occur. Write clean, not fast. Clean code is often very fast anyway, and can easily be made faster when required.

LoadFileAsString()


std::string LoadFileAsString(const std::string &filepath, bool *successful)
{
	if(successful) *successful = false;

	//Open the file for reading.
	std::ifstream file(filepath.c_str(), std::ios_base::in);

	if(!file)
	{
		Log::Message(MSG_SOURCE("FileFunctions", Log::Severity::Error)) << "Failed to load '" << Log_HighlightCyan(GetFilenameFromPath(filepath)) << "' at " << Log_DisplayPath(filepath)
																		<< "\nDo I have sufficient privileges? Does the file even exist?" << Log::FlushStream;

		return "";
	}

    //Read the file into the stringStream.
    std::ostringstream stringStream;
    stringStream << file.rdbuf();

    if(successful) *successful = true;
    
    //Convert the stringStream into a regular string.
    return stringStream.str();
}

Seperate()


std::vector<std::string> Separate(const std::string &str, char divider)
{
	std::vector<std::string> stringArray;

	size_t startOfSegment = 0;
	for(size_t pos = 0; pos < str.size(); pos++)
	{
		if(str[pos] == divider)
		{
			//Grab the past segment.
                        stringArray.push_back(str.substr(startOfSegment, (pos - startOfSegment)));

			//Mark the beginning of a new segment.
			startOfSegment = (pos + 1);
		}
	}

	//The final segment.
	stringArray.push_back(str.substr(startOfSegment, (str.size() - startOfSegment)));
    
	return stringArray;
}

Hi Servant of the Lord,

thanks for the reply.

the stream variable could had been :

stream = "\nthis is the 2nd line.\n\nthis is the fifth line.\n\0";

that is the data structure that I have to work with. That is what the Opengl shader compiler understand.

a little history. I learned coding began in 1980 by taking a C program in college. after that I had not done any coding after that until a few years ago i started to code again as a hobby. And all these times i learned C++ by myself. so sorry for the Cish coding style.

So after using the separate(), how do i print out the whole file?

So after using the separate(), how do i print out the whole file?

std::ofstream, or another method.

This topic is closed to new replies.

Advertisement