Boost logo

Boost Users :

From: Ovanes Markarian (om_boost_at_[hidden])
Date: 2006-08-10 09:19:08


Patrik this will not work the way you write it...

First of all:
According to C++ scope rules you create 2 streams: global and later in main a local one.

> boost::filesystem::ifstream inputDataStream; //declaration of inputDataStream
this is not declaration!!! This is a global stream

In your case you need to use a heap object (pointer) or pass the stream by reference to a function.

With Kind Regards,
Ovanes Markarian

On Thu, August 10, 2006 15:08, pktao_at_[hidden] wrote:
> Im having trouble calling an input file stream from inside a function and was
> hoping if anyone can help me with a solution. Im compiling with MSVC++ 8.0.
> Below is a detailed description of the problem:
>
> The input file that Im trying to read is pt03-test-1.txt. Its just a text file
> with 2 integer values in a single column:
>
> 24
> 35
>
> the path to the file is : /tao/data/pt03-test-1.txt
>
> The following code works:
> #include <iostream>
> #include <vector>
> #include <fstream>
> #include <boost/filesystem/operations.hpp>
> #include <boost/filesystem/fstream.hpp>
>
> int main() {
> boost::filesystem::ifstream inputDataStream("/tao/data/pt03-test-1.txt");
> int mobilityIndexMarker = 0;
> inputDataStream >> mobilityIndexMarker;
> std::cout << mobilityIndexMarker << std::endl;
> return (0);
> };
>
> Encapsulating the lines in main in a separate function breaks the code:
> #include <iostream>
> #include <vector>
> #include <fstream>
> #include <boost/filesystem/operations.hpp>
> #include <boost/filesystem/fstream.hpp>
>
> boost::filesystem::ifstream inputDataStream; //declaration of inputDataStream
>
> void trialOnsetWrite() {
> int mobilityIndexMarker = 0;
> inputDataStream >> mobilityIndexMarker;
> std::cout << mobilityIndexMarker << std::endl;
> };
>
> int main() {
> boost::filesystem::ifstream inputDataStream("/tao/data/pt03-test-1.txt");
> trialOnsetWrite();
>
> return (0);
> };
>
> Very Very Appreciative,
> Patrick
> _______________________________________________
> Boost-users mailing list
> Boost-users_at_[hidden]
> http://lists.boost.org/mailman/listinfo.cgi/boost-users
>


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net