Solved

Reading a file with out of FILE.

Posted on 2007-03-21
5
206 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

863 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

16 Experts available now in Live!

Get 1:1 Help Now