Why wont this code output to console line by line?

Im trying to read a textfile and output everything line by line to konsole. but rather every word encountered results in a new line. How would i go about to getting line by line done in the console rather a new line for every word space?

heres the code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
//THis file will read a file to console
#include <fstream>
#include <iostream>
using namespace std;

int main()
{
ifstream intext;
char get[1000];
string consoleout;

cout << "Enter the file name to open";
cin.get(get,1000);

intext.open(get);

while(intext.good()){
  intext >> consoleout;
  cout << consoleout << endl;
}

}
cout << consoleout << endl; // endl will result in a new line everytime your while loop executes

Can you post your current output, and what you want your output to look like? I'm not 100% sure of what you're asking.
1
2
3
4
5
6
7
8
9
10
11
Enter the file name to opentext
hi
i
am
a
text
file
and
i
am
awesome


this is the text file
1
2
3
4
hi i am 
a text file
and i am 
awesome
im trying to get it to output like text file line by line not every word like above
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
#include <iostream> // for console I/O
#include <fstream> // for file I/O
#include <string> // almost forgot this, you need this for getline

using namespace std;

int main()
{
    string szInput; // going to store the input in a string
    ifstream fin; // creating an object of ifstream
    fin.open(/*file name*/); // open the file

    if (!fin.is_open())
        return 1; // return error if the file isn't opened

    while(!fin.eof()) // loop until the end of the file
        {
            getline (fin, szInput, '\n'); // get a line of characters until a newline character is found
            cout << szInput << endl; // display input, end line
        }

    cin.get(); // so you can see the results
    fin.close(); // close the file
    return 0; // self explanatory
}


I'll be honest, haven't tested it, but I'm confident it works.
Last edited on
Topic archived. No new replies allowed.