Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

reading a file

Posted on 2000-03-04
4
Medium Priority
?
200 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 200 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

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

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

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

721 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