Solved

Read the file

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
std::atomic with custom class 6 190
Lambda for random numbers problem 7 107
c++ substatte a varabe for a string in a LPCTSTR statment 8 83
I could not build boost code, 10 79
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

895 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

14 Experts available now in Live!

Get 1:1 Help Now