Solved

reading a file

Posted on 2000-03-04
4
193 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
[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
  • 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
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 user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

749 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