| rjschilling (3) | |
|
I would like to store the entire content of a file in a single c-string variable or in a standard string class variable. Is there a function that will do this for me? I am familiar with functions that get one character or one line at a time but I don't think I've encountered a function that gets the entire file. Does this function keep or disregard the end-of-line character? If no such function exists, I would write my own function to create and return such a variable. Thanks and grateful to have this forum - Randy | |
|
|
|
| gtm (53) | |||
|
create a string open the file loop through the file while getline is true add the results of getline to a string voila entire file is in the string here is an example of copying the contents to a vector:
| |||
|
Last edited on
|
|||
| rjschilling (3) | |
|
OK, thanks. That's about what I had in mind. I will probably create a string s and my loop will build s by concatenate line to existing s. Thanks | |
|
Last edited on
|
|
| Cubbi (1927) | ||||||
You can use the range constructor for the string class:
Another handy shortcut is the command to transfer the entire contents of a file into a stringstream:
| ||||||
|
Last edited on
|
||||||
| rjschilling (3) | |
| Yes Cubbi. That seems to be exactly what I need. Now to understand the functions in your code. I've not heard of stringstreams before. Thank you. | |
|
Last edited on
|
|