Store strings

Hello everyone,
I'm stuck with this problem. For example, I have a text file:
"Hello
I Am
Liz Hurley"
How can I load the file and store "Hello" for str1, "I Am" for str2, & "Liz Hurley" for str3?
I came up with getline, but it doesn't work :( Thank you.

ifstream openFile;
openFile.open(input);
while(!openFile.eof())
{
getline(openFile, str1);
getline(openFile, str2);
getline(openFile, str3);
}
closed account (48T7M4Gy)
http://www.cplusplus.com/doc/tutorial/files/

Have a look at this tutorial. There's a sample at the top and a few others further down which you can easily adapt. Instead of the test in quotation marks just use the string . eg myfile << str1; etc

You can then read what you have on the file using notepad or similar until you write code to read your file.
Last edited on
Your idea with getline was right. Once you have read the 3 lines you need to check each line if it has quotes and add them if necessary.

One way to do it:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>
#include <string>
#include <fstream>

using namespace std;

void QuoteString (string& input)
{
  const char quote = '"';

  if (input[0] != quote)
  {
    input = quote + input;
  }
  if (input[input.size() - 1] != quote)
  {
    input += quote;
  }
}

int main ()
{
  string filename = "input.txt";
  ifstream src (filename.c_str());

  if (!src) // for some reason it can't be opend
  {
    perror ("File error: ");
    return -1;
  }
  string str1, str2, str3;
  while (src)
  {
    getline (src, str1);
    getline (src, str2);
    getline (src, str3);
  }
  QuoteString (str1);
  QuoteString (str2);
  QuoteString (str3);

  cout << "str1: " << str1 << "\n";
  cout << "str2: " << str2 << "\n";
  cout << "str3: " << str3 << "\n";
  
  system ("pause");
  return 0;
}
Thank you guys Kermort and Thomas. However, my getline functions didn't work at all :( The result I got are blanks which I assume it read nothing from the file.
closed account (48T7M4Gy)
1000 pardons because I misread your comment about strings. However, try this:

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

int main()
{
    std::fstream inFile("getstringfile.txt");
    std::string temp = "";
    
    if (inFile.is_open())
    {
        while (std::getline(inFile, temp))
            std::cout << '*' << temp << '*' << std::endl;
    }
    else
        std::cout << "File not opened\n";
    
    return 0;
}


Text file getstringfile.txt I used is:
Hello
I Am
Liz Hurley


And the output is:

*Hello*
*I Am*
*Liz Hurley*
Program ended with exit code: 0


The asterisks are there to verify each line was read as a single strings.

PS You wanted str1, str2, ... instead of temp. This means you read in separate variables but I'll leave that to you. It's not a complicated adaptation, and better still, keep your program and make the few necessary changes. :)
Last edited on
Thank you Kemort :)
closed account (48T7M4Gy)
My pleasure. On reflection the adaptation might need a hint:

1
2
3
4
5
6
7
8
9
if (inFile.is_open())
    {
        while (std::getline(inFile, str1) && std::getline( ... some more stuff for the other strings ){}
        
        std::cout << '*' << str1 << '*' << std::endl;
       //some more lines for the other strings
    }
    else
        std::cout << "File not opened\n";
Topic archived. No new replies allowed.