Home > C++, File-Handling, GLSL, Graphics > (ReadShader)Simple File Reading Code in C++ with File Size buffer

(ReadShader)Simple File Reading Code in C++ with File Size buffer

//impressed by lighthouse 3d and help taken from sources on the internet.
//this is not my idea, not my full code.

//It reads the full file in single go.
#ifndef READSHADER_H
#define READSHADER_H
#include
#include

using namespace std;

char * readShader(char *fileName){

cout <<“nothing”;
ifstream input(fileName);
if(!input){
cout << endl << “UNABLE TO OPEN FILE ” << fileName <<endl;
return 0;
}

int st = input.tellg();
input.seekg(0, ios::end);
int end = input.tellg();
int fileSize = end – st;
if(fileSize <= 0){return 0;}

char *data = new char[fileSize+50];//50 added to just have buffer
memset(data, ”,fileSize+10);
input.seekg(0, ios::beg);

input.read(data,fileSize);
input.close();
data[fileSize]= ”; //appending the zero as the read does not append that
return data;

};
#endif

Advertisements
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: