• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 187
  • Last Modified:

Read the file

I created a small program to read file.log and display it on the screen. My file.log looks like this
<br><br>
test1<br>
test2<br>
test3<br>
<br><br>
but my program display it like
test1test2test3

Here is my program looks like:

#include <fstream.h>
#include <iostream.h>

void read(char filename[]);

void main()
{
  read("file.log");
}

void read(char filename[])
{
 char tmp;
 ifstream fin;
 fin.open(filename, ios::in);

 while (fin.good())
 {
   fin >> tmp;
   if(!fin.good()) break; //check if reach end of file
   cout << tmp;
 }
 
 fin.close();  
}

If I put endl like cout << tmp << endl;
It will display:
t
e
s
t
1
t
e
s
t
2
t
e
s
t
3
0
setyo011798
Asked:
setyo011798
1 Solution
 
setyo011798Author Commented:
sorry about <br>, I do not know if this form doesn't support html code.
0
 
jhanceCommented:
You said what it does (test1test2test3) and what it does with the endl, but what do you WANT it to do?
0
 
LlandrCommented:
Do you hav access to a string class. Try using this instead of the char variable. Usually things like reading from input streams are better wrapped in the a string class.
0
 
stefanrCommented:
What you probably want to use is the getline member function (istream::getline). Because istream is the base class for ifstream you have access to that function for disk files too.
The function getline extracts the delimiter (default '\n'), but do not store it. If you don't want to extract/store the delimiter, use istream::get instead.

Hope this helps.

/Stefan
0
 
setyo011798Author Commented:
Thanks, i use istream::getline and it works.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now