Solved

Reading a file with out of FILE.

Posted on 2007-03-21
5
198 Views
Last Modified: 2010-04-01
Hi experts,

I have the file  data.dat with 2 columns such as this:

12asb24   31niel93
89bhgj83   57yv76
...
32dae56  49jsn01

with N data.
I need read the file using "ifstream" , not FILE .   and store it in a matrix with dimention of N x 2 .

any idea? thanks a lot. !
0
Comment
Question by:hernan007
  • 3
  • 2
5 Comments
 
LVL 39

Accepted Solution

by:
itsmeandnobodyelse earned 50 total points
ID: 18762782
#include <string>
#include <fstream>
#include <sstream>
#include <vector>
using namespace std;

   struct Record
   {
       std::string s1;
       std::string s2;
       Record(const std::string& s)
       {
               std::istringstream iss(s);
               iss >> s1 >> s2;
       }
   };

int readData(const string& filename, vector<Record>& v)
{
   ifstream ifs(filename.c_str);
   string line;
   while (getline(ifs, line))
        v.push_back(Record(line));
   ifs.close();
   return v.size();
}

Regards, Alex
0
 

Author Comment

by:hernan007
ID: 18762986
Hi Alex thanks !!

In order to use the function only is with a call like this?   rigth?

main(){
....
vector<Record> x;
..........
int  val= readData("data.dat", x);
....
0
 

Author Comment

by:hernan007
ID: 18763415
Well   I have this error msg   :(

main.cc: In function ‘int readData(const std::string&, std::vector<Record, std::allocator<Record> >&)’:main.cc:38: error: no matching function for call to ‘std::basic_ifstream<char, std::char_traits<char> >::basic_ifstream(<unresolved overloaded function type>)’
/usr/lib/gcc/i386-redhat-linux/4.1.1/../../../../include/c++/4.1.1/fstream:442: note: candidates are: std::basic_ifstream<_CharT, _Traits>::basic_ifstream(const char*, std::_Ios_Openmode) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/i386-redhat-linux/4.1.1/../../../../include/c++/4.1.1/fstream:428: note:                 std::basic_ifstream<_CharT, _Traits>::basic_ifstream() [with _CharT = char, _Traits = std::char_traits<char>]
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 18763579
It needs to be

     ifstream ifs(filename.c_str());

It was a typo.

>>>> with a call like this?   rigth?

Yes, beside that main should have an int type and should return 0   ;-)


0
 

Author Comment

by:hernan007
ID: 18763668
Thanks! alex.
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

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…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
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.

707 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

18 Experts available now in Live!

Get 1:1 Help Now