Solved

Reading a file with out of FILE.

Posted on 2007-03-21
5
229 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
[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
  • 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

710 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