Solved

Read the file

Posted on 1998-01-17
5
168 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
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

708 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now