Loading entire contents of text file into string

I am trying to load the entire contents of a file into a string, and for some reason I cannot get it to work. I have done this many times before but I cant seem to keep the way to do it in my head and I always forget how to do it, i looked it up and people say that: std::string str((std::istreambuf_iterator<char>(ifs)), std::istreambuf_iterator<char>()); works but it isnt doing anythign for me, why is that?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <string>
#include <fstream>

std::string Load();

int main()
{
    std::string stringToSearch;

    std::cout << Load();

    return 0;
}

std::string Load()
{
    std::ifstream loadText("File.txt");

    std::string str((std::istreambuf_iterator<char>(loadText)), std::istreambuf_iterator<char>());

    return str;

}


Its supposed to load this text:

Lorem ipsum dolor sit amet, rebum clita fierent ne mei. Ad quo suas facilisis, recteque ocurreret aliquando mea at. Sit ut congue molestiae, scripta gubergren ne mel. Id fierent evertitur nec. Id dico clita vix, ea idque blandit patrioque mea, at quo dicant corrumpit.

Cu purto sapientem his, sanctus volutpat neglegentur cum in, pri id alia deseruisse. Tale postulant ut vis, no mel adhuc graecis perpetua. Cu usu minim inimicus adversarium, quaeque propriae usu ea. Vix et nisl persequeris, oratio hendrerit liberavisse an mea. Vero indoctum ne sea, nec aliquip evertitur ad, purto scribentur complectitur at duo. Eos nihil omnesque ex, has in probatus iudicabit intellegat.

Laudem tempor pri ea, ei omnis sonet iuvaret his. Te dicat corrumpit mei, utinam graece alienum mel eu. Tempor putent bonorum vim et, eum ne quas denique. Labitur fuisset vituperatoribus ea eum. Dolor animal deterruisset cu pri.

Pro ei patrioque prodesset efficiantur, dicit eloquentiam nam id. Sit no legere utroque phaedrum, wisi detraxit vix cu. Eam sint quaeque quaerendum ei, fugit mollis ius cu. Eam ad saepe referrentur.

At quod salutandi est, adolescens efficiendi mea ex. At hinc mundi appareat sea, ex ancillae epicurei vix, explicari rationibus reprimique in vel. Has te doming vivendum. Eu sea omnis possim incorrupte, eu per fastidii neglegentur instructior, pro ut munere essent voluptaria.

Pro ut nibh quidam, usu an amet integre abhorreant. Brute appetere eos id. Equidem sadipscing ne per, everti omittam eam id. An eripuit delenit nec.

Tantas vulputate sea eu, natum aliquip te per. Soluta definiebas no vis. Sea ei soluta vocent perpetua, verear voluptatum instructior in cum, errem senserit te nam. Qui prima ludus te. Te mei dicant intellegam, sed id duis quidam oporteat.

Id fugit tibique mei. Dico esse debitis in duo, est no consequat torquatos referrentur. Ex nihil postea officiis mea, mea iudico munere saperet ne. Ius petentium facilisis eu, qui viderer vivendo no, et tritani accommodare qui.

An nulla dictas perfecto his, eum ei quot appellantur. Suas nibh clita ne eam. Ex laoreet salutandi repudiare eam. Et cum wisi essent nonumes, ex eos odio fugit efficiantur, sea dolor praesent postulant at. Quo ne case prodesset adversarium, pro cu fugit debet.

Nonumes conceptam expetendis pro ea, te autem pericula mea, et modus tollit blandit has. No legere nostro has, nec no sint elit. Nec no exerci recusabo oportere, pri ea congue convenire repudiandae. Ut eos legere disputationi. Ex ornatus fabellas probatus mei. Vel albucius adolescens at, ex vix simul dicant.
That code is fine. If I had to guess, File.txt isn't in the working directory of the executable.
Ah, I didnt have the file in the right directory, however it still isnt displaying the text.
That code is fine. If I had to guess, you still don't have the file in the right directory.
ah, for some reason the file is supposed to read "File.txt, and the file is named "File.txt" but it worked when I just named it "File", not sure why that is, strange.
You're using Windows, and you've set your file explorer to not show you file extensions. The file is named File.txt, but it shows you File.

When you thought you had named it File.txt , it was actually named File.txt.txt

If you're going to be a programmer, change your settings so that file extensions are not hidden from you.
Last edited on
Hello Ch1156,

As Repeater has pointed out the file needs to be in the correct directory. Also the file name in the program has to match the file name stored on the hard drive. If that file name ends with ".txt.txt" so must what you use in the program.

When opening an input file you should follow with:
1
2
3
4
5
6
if (!inFile)
{
	std::cout << "\n File " << "iFileName" << " did not open" << std::endl;
	std::this_thread::sleep_for(std::chrono::seconds(3));  // <--- Needs header files chrono" and "thread".
	exit(1);
}
To make sure the file stream is open and usable.

hope that helps,

Andy
Topic archived. No new replies allowed.