Reading file line by line into string

Hi, so I'm trying to open a file and read the sentences in it line by line and put them into string and then put that string into a function.
So far I have this:

#include <iostream>
#include <string>
#include <fstream>
using namespace std;

int main(){

ifstream file ("engsent"); //file just has some sentences
if (!file) {
cout << "unable to open file";
return false;
}
string sent;
while (getline (file, sent)){

cout << sent << endl;

}

//function goes here...

i have a function but not sure how to connect it so that when the file is opened it takes the first line/sentence and puts it into the function and then takes the second line/sentence and puts it into the same function, etc. until all the sentences in the file has gone through the function.
Maybe like this.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <string>
#include <fstream>
using namespace std;

void processLine(const string& line)
{
  // use line
}

int main(){

ifstream file ("engsent"); //file just has some sentences
if (!file) {
cout << "unable to open file";
return false;
}
string sent;
while (getline (file, sent))
{
cout << sent << endl;
 processLine(sent);
}
hey thanks for the help! I tried it and it opened the files but instead it just printed out each line for me, I'm trying to get each line so that all the characters will be lowercase. I'm not using any advanced method because I'm not there yet

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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include<iostream> 
#include<string> 
#include<fstream>
using namespace std;

void processLine(const string & line) {
  // use line
}

int main() {
  string word1, word2, word3, word4;

  ifstream file("engsent"); //file just has some sentences
  if (!file) {
    cout << "unable to open file";
    return false;
  }
  string sent;
  while (getline(file, sent)) {
    cout << sent << endl;
    processLine(sent);
  }

  //takes each word from a string and stores them into separate strings
  int pos = 0, pos2, pos3, pos4;

  //WORD1
  while (sent[pos] != ' ') { //pos is 0 and starts from the beginning of string
    word1 += sent[pos]; //stores word1 until the space
    pos++;
  }
  //WORD2
  for (pos2 = pos; sent[pos2] == ' '; pos2++); //finds the next word after word1 (word2)
  while (sent[pos2] != ' ') {
    word2 += sent[pos2];
    pos2++;
  }
  //WORD3
  for (pos3 = pos2; sent[pos3] == ' '; pos3++); //finds the next word after word2 (word3)
  while (sent[pos3] != ' ') {
    word3 += sent[pos3];
    pos3++;
  }
  //WORD4
  for (pos4 = pos3; sent[pos4] == ' '; pos4++); //finds the next word after word3 (word4)
  while (sent[pos4] != '.') {
    word4 += sent[pos4];
    pos4++;
  }

  //takes each word/character and converts them all to lowercase
  //WORD1
  for (int i = 0; i < word1.length(); i++) {
    if (word1[i] >= 'A' && word1[i] <= 'Z') { //checks if each character is lowercase
      word1[i] = char(((int) word1[i]) + 0x20); //changes uppercase to lowercase
    }
  }
  //WORD2
  for (int i = 0; i < word2.length(); i++) {
    if (word2[i] >= 'A' && word2[i] <= 'Z') {
      word2[i] = char(((int) word2[i]) + 0x20);
    }
  }
  //WORD3
  for (int i = 0; i < word3.length(); i++) {
    if (word3[i] >= 'A' && word3[i] <= 'Z') {
      word3[i] = char(((int) word3[i]) + 0x20);
    }
  }
  //WORD4
  for (int i = 0; i < word4.length(); i++) {
    if (word4[i] >= 'A' && word4[i] <= 'Z') {
      word4[i] = char(((int) word4[i]) + 0x20);
    }
  }
  //print out the sentence in all lowercase
  cout << word1 << " " << word2 << " " << word3 << " " << word4 << endl;

  return 0;
}


p.s. i tried this with a user input and it works but not sure how to go about it for an input file
Last edited on
I tried it and it opened the files but instead it just printed out each line for me, I'm trying to get each line so that all the characters will be lowercase.

That's all your program does, since processLine does nothing.

Where the comments say // use line is where your code to deal with a line should be.

You might also want to check out http://en.cppreference.com/w/cpp/string/byte/tolower
Your code is more complicated than it needs to be.
yes, thank you that cleared that part up for me. and yeah, i'm restricted to the basics so i can't use tolower in my program or other libraries and vectors (basically have to do it the long way since my class haven't gotten to that part yet). Thanks!
Even if you are "restricted to basics" you can take inspiration from library functions that you aren't allowed to use yet.

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
#include<iostream> 
#include<string> 
#include<fstream>
using namespace std;

char aslower(char ch) {
    if (ch >= 'A' && ch <= 'Z')  // Practically correct
        ch = ch - 'A' + 'a';

    return ch;
}

void processLine(const string & line) {
    std::string result;

    for (std::size_t i = 0; i < line.size(); ++i)
        result += aslower(line[i]);

    std::cout << result << '\n';
}

int main() {
    ifstream file("engsent"); //file just has some sentences
    if (!file) {
        cout << "unable to open file";
        return false;
    }
    string sent;
    while (getline(file, sent)) {
        cout << sent << endl;
        processLine(sent);
    }
}
Topic archived. No new replies allowed.