?
Solved

Read the file

Posted on 1998-01-17
5
Medium Priority
?
185 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 200 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

Technology Partners: 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

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
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 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 pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

800 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