Solved

Read the file

Posted on 1998-01-17
5
184 Views
Last Modified: 2010-04-10
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
Comment
Question by:setyo011798
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 

Author Comment

by:setyo011798
ID: 1178433
sorry about <br>, I do not know if this form doesn't support html code.
0
 
LVL 32

Expert Comment

by:jhance
ID: 1178434
You said what it does (test1test2test3) and what it does with the endl, but what do you WANT it to do?
0
 
LVL 2

Expert Comment

by:Llandr
ID: 1178435
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
 
LVL 3

Accepted Solution

by:
stefanr earned 50 total points
ID: 1178436
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
 

Author Comment

by:setyo011798
ID: 1178437
Thanks, i use istream::getline and it works.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question