In the while loop, "text" is always replaced by a new line in the file, so in the end, it only contains the last line in the file, as getline() does not append to the end of the string, but replaces the previous content. That example worked since the input from the file was immidiately outputed to cout, before STRING was being replaced with the new line.
If you want to make a long string which contains all the newline characters, etc. you could use a temp string which gets the line from the file, and the append() function to append the temp string to a main string.
Last edited on
It's likely that you're looking in the wrong directory for the file. It has to be in the current directory, but if you're running your program from an IDE, it's not always clear what directory that is.
The original draft of the program should work.