Solved

reading a file

Posted on 2000-03-04
4
196 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

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

Question has a verified solution.

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

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
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 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…

624 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