Solved

Reading a file with out of FILE.

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

777 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