Jump to content

Displaying External File Contents C++

Go to solution Solved by Mr_KoKa,
#include <iostream>
#include <fstream>
#include <sstream>

std::string readFile(std::string s){
	std::stringstream ss;
	std::ifstream inputFile(s.c_str());
	
	if(inputFile.good()){
		ss << inputFile.rdbuf();
	}
	
	return ss.str();
}

int main () {
  std::string fileContents = readFile("test.cpp");
  std::cout << fileContents << std::endl;
  
  return 0;
}

 

Hey guys, I'm currently trying to write a function that when it is

called, it will display everything that is within the file.  The same

program is writing to an external data file and the function for 

displaying the contents will basically read everything that has been

written to the file.  Frankly, I was not taught how to do file input and

output streams and its a miracle that I got the write out to work.  

Does anyone have any idea how I would read in and display 

everything? 

http://pastebin.com/VDN9cAjP

Thanks in advance,

Cj

i5 4670k| Asrock H81M-ITX| EVGA Nex 650g| WD Black 500Gb| H100 with SP120s| ASUS Matrix 7970 Platinum (just sold)| Patriot Venom 1600Mhz 8Gb| Bitfenix Prodigy. Build log in progress 

Build Log here: http://linustechtips.com/main/topic/119926-yin-yang-prodigy-update-2-26-14/

Link to comment
https://linustechtips.com/topic/545932-displaying-external-file-contents-c/
Share on other sites

Link to post
Share on other sites

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

std::string readFile(std::string s){
	std::stringstream ss;
	std::ifstream inputFile(s.c_str());
	
	if(inputFile.good()){
		ss << inputFile.rdbuf();
	}
	
	return ss.str();
}

int main () {
  std::string fileContents = readFile("test.cpp");
  std::cout << fileContents << std::endl;
  
  return 0;
}

 

Link to post
Share on other sites

2 hours ago, Mr_KoKa said:

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

std::string readFile(std::string s){
	std::stringstream ss;
	std::ifstream inputFile(s.c_str());
	
	if(inputFile.good()){
		ss << inputFile.rdbuf();
	}
	
	return ss.str();
}

int main () {
  std::string fileContents = readFile("test.cpp");
  std::cout << fileContents << std::endl;
  
  return 0;
}

 

Thanks, I actually kept playing around with my code after posting and figured it

out and this was basically my result.  I forgot to say that I no longer needed help,

but the way you put it allows me to implement something like this in the future.

i5 4670k| Asrock H81M-ITX| EVGA Nex 650g| WD Black 500Gb| H100 with SP120s| ASUS Matrix 7970 Platinum (just sold)| Patriot Venom 1600Mhz 8Gb| Bitfenix Prodigy. Build log in progress 

Build Log here: http://linustechtips.com/main/topic/119926-yin-yang-prodigy-update-2-26-14/

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×