codingdir logo sitemap sitemap |
Home
PHP
C#
C++
ANDROID
JAVA
JAVASCRIPT
PYTHON

Reading input from fstream


By : , Category : c++

I'd suggest the simplest that would work is:

#include <fstream>
#include <iostream>

int main()
{
    std::ifstream inFile("celsius_input.dat"); //open the file

    double celsius;
    while (inFile >> celsius)
    {
        double fahrenheit = (9.0/5.0) * celsius + 32.0;
        std::cout << "The input value " << celsius << " degrees, converted into fahrenheit is " << fahrenheit << " degrees" << std::endl;
    }
}

If you really must read a line first, do it like:

#include <fstream>
#include <iostream>
#include <string>

int main()
{
    std::ifstream inFile("celsius_input.dat"); //open the file

    std::string input;
    while (std::getline(inFile, input))
    {
        double celsius = std::strtod(input.c_str(), nullptr);
        double fahrenheit = (9.0/5.0) * celsius + 32.0;
        std::cout << "The input value " << celsius << " degrees, converted into fahrenheit is " << fahrenheit << " degrees" << std::endl;
    }
}
ReLated :

So reading the docs on std::stream open method it looks like the open will fail when both 'in' and 'app' are specified in the open. Try leaving the 'in' off and see what happens.

"If the mode has both trunc and app set, the opening operation fails. It also fails if either is set but out is not, or if both app and in are set."

std::fstream reference

It would be a lot easier to help if you posted compilable code. :)

You're right in that there's an infinite loop, here:

void doFilter(ifstream &myFile, ostream &newFile)
{
  while (ch != EOF)   // <--- ch will never equal EOF
  {
    myFile.get(ch);   // <--- this overload of get() only sets the EOF bit on the stream
    this->transform(ch);
    newFile.put(ch);
  }
}

because the stream's get() method does not set the character to EOF at end-of-file. You can use the parameter-less version to get that behaviour: ch = myFile.get();

Otherwise, you should be testing for !myFile.eof() like you do in main().


Also, you're not actually using the transformed value of ch so this code won't change the value in the output file. Either make transform() work with a reference so it alters its parameter, or do ch = this->transform(ch);.

Here's a better way of reading your collection:

#include <vector>
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
#include <cstdint>
#include <iterator>

template<class T>
void Write(std::string const & path, T const & value, std::ios_base::openmode mode)
{               
    if (auto stream = std::ofstream(path, mode))
    {
        Write(stream, value);

        stream.close();
    }
    else
    {
        throw std::runtime_error("failed to create/open stream");
    }       
}

template<class T>
void Write(std::ostream & stream, T const & value)
{
    std::copy(value.begin(), value.end(), std::ostreambuf_iterator<char>(stream));

    if (!stream)
    {
        throw std::runtime_error("failed to write");
    }
}

template<class T>
void Read(std::istream & stream, T & output)
{
    auto eof = std::istreambuf_iterator<char>();

    output = T(std::istreambuf_iterator<char>(stream), eof);

    if(!stream)
    {
        throw std::runtime_error("failed to read stream");
    }
}

template<class T>
void Read(std::string const & path, T & output)
{               
    if (auto stream = std::ifstream(path, std::ios::in | std::ios::binary))
    {
        Read(stream, output);

        stream.close();
    }
    else
    {
        throw std::runtime_error("failed to create stream");
    }           
}


int main(void)
{
    // Write and read back text.

    {
        auto const s = std::string("I'm going to write this string to a file");

        Write("temp.txt", s, std::ios_base::trunc | std::ios_base::out);

        auto t = std::string();

        Read("temp.txt", t);
    }

    // Write and read back a set of ints.

    {
        auto const v1 = std::vector<int>() = { 10, 20, 30, 40, 50 };

        Write("temp.txt", v1, std::ios_base::trunc | std::ios_base::out | std::ios_base::binary);

        auto v2 = std::vector<int>();

        Read("temp.txt", v2);
    }

    return 0;
}

Pass in an iterable container rather than using "new".

You need provide some serialization mechanism for class Produto. For example:

class Produto {
  // ...
private: 
  std::string m_str;
private: 
  friend ostream& operator<<(ostream& stream, const Produto& obj);
  friend istream& operator>>(istream& stream, Prodoto& obj)
};

ostream& operator<<(ostream& stream, const Produto& obj)
{
  // Write all data to stream.
  // ...
  stream << obj.m_str;
  return stream;
}

istream& operator>>(istream& stream, Prodoto& obj)
{
  // Read all data from strem.
  // ...
  stream >> obj.m_str;
  return stream; 
}

And then use it as below:

Produto p1(1, "Refrigerante");
ofstream serializationWriter;
// ...
serializationWriter << p1;

Produto p2;
ifstream serializationReader;
// ...
serializationReader >> p2;

For more details see overload ostream istream operator

Comments


Message :
Login to Add Your Comments .
How to disable registered OpenCL platforms on Windows?
Is Observable broken in Angular 2 Beta 3?
Cross-thread operation not valid when using Invoke
How to pass an IEnumerable or queryable list of properties from Controller to View
Finding numbers after a certain keyword using Python
Pocketsphinx recognizes random phrases in a silence
Passing non-thread-safe objects through thread-safe containers
React scroll nav
BizTalk WCF-BasicHttp Adapter does not allow Empty string for Service Certificate Props
Why property ''cause" of Exception is repeating forever?
Privacy Policy 2017 © codingdir.com All Rights Reserved .