Solved

reading a file

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

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

Suggested Solutions

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
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. …
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.

751 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