Solved

reading a file

Posted on 2000-03-04
4
180 Views
Last Modified: 2010-04-02
How can I read a file (line by line)and store it in an array.

thanks
0
Comment
Question by:tparvaiz
  • 3
4 Comments
 
LVL 5

Accepted Solution

by:
Wyn earned 50 total points
ID: 2582853
You'd better use string class.
#include <string>
#include <fstream>

ifstream MyFile("filename");
string Lin;

while (!MyFile.eof())
{
   getline(MyFile,Lin);
//do whatever you want.
} ;
0
 

Author Comment

by:tparvaiz
ID: 2583325

I've modified the above code as bellow
ifstream MyFile("PbDir.pbk");
CString Lin;

while (!MyFile.eof())
{
   MyFile.getline(MyFile,Lin);

} ;
MyFile.close();


getting this message

error C2664: 'class istream &__thiscall istream::getline(char *,int,char)' : cannot convert parameter 1 from 'class ifstream' to 'char *'
0
 
LVL 5

Expert Comment

by:Wyn
ID: 2583352
CString is a MFC class which can be used as char* .But string is a STL class ,you should use string which need #include <string>...

I suggest you use string class before it can change its length according to the line length you read from file.But use char* is bad because you dont now how much buffer you use save.
Regards
W.Yinan

 
0
 
LVL 5

Expert Comment

by:Wyn
ID: 2583361
//if you want to use char*
#include <fstream>  
#include <iostream>

const int sz = 100;
char buf[sz];
  ifstream in("myfile.my");
  while(in.getline(buf, sz)) {
//do whatever here.
   }
}
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
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 goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

747 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

12 Experts available now in Live!

Get 1:1 Help Now