Reading floats/words/symbols from a file and ONLY storing the floats in an array C++

≡放荡痞女 提交于 2019-12-25 04:13:31

问题


I have a C++ programming question that I'm asked to read from a file that contains several floats, words and symbols (e.g. # ! %). From this file I must take only the floats and store it into an array.

The text file may look like this

11
hello
1.00
16.0
1.999

I know how to open the file; it's just grabbing ONLY the floats I'm struggling with.


回答1:


You have to use fscanf(). Read the documentation for information how to use it.




回答2:


As long as you don't mind treating integral numbers as floats, such as 11 in your post, you can use the following strategy.

  1. Read one token at a time that are separated by whitespaces into a string.
  2. Extract a floating point number from the string using one of several methods.
  3. If the extraction was successful, process the floating point number. Otherwise, move on to the next token.

Something along the lines of the code below should work.

std::string token;
std::ifstream is(filename); // Use the appropriate file name.
while ( is >> token )
{
   std::istringstream str(is);
   float f;
   if ( str >> f )
   {
      // Extraction was successful.
      // Check whether there is more data in the token.
      // You don't want to treat 11.05abcd as a token that represents a
      // float.
      char c;
      if ( str >> c )
      {
         // Ignore this token.
      }
      else
      {
         // Process the float.
      }
   }
}



回答3:


I'd use a ctype facet that classifies everything except for digits as being white space:

struct digits_only : std::ctype<char>
{
    digits_only() : std::ctype<char>(get_table()) {}

    static std::ctype_base::mask const* get_table()
    {
        static std::vector<std::ctype_base::mask>
            rc(std::ctype<char>::table_size, std::ctype_base::space);

        if (rc['0'] == std::ctype_base::space)
            std::fill_n(&rc['0'], 9, std::ctype_base::mask());
        return &rc[0];
    }
};

Then imbue the stream with a locale using that facet, and just read your numbers:

int main() {
    std::istringstream input(R"(
11

hello

1.00

16.0

1.999)");

    input.imbue(std::locale(std::locale(), new digits_only));

    std::copy(std::istream_iterator<float>(input), std::istream_iterator<float>(),
        std::ostream_iterator<float>(std::cout, "\t"));
}

Result:

11      1       16      1.999



回答4:


// reading a text file and storing only float values
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>

using namespace std;

int main () {
  string line;
  ifstream myfile ("example.txt");
  std::vector<float> collection;

  if (myfile.is_open())
  {
    while ( getline (myfile,line) )
    {
      if(checkIfFloatType(line))
      {
         collection.push_back(std::stof (line));
      }
    }
    myfile.close();
  }

  else cout << "Unable to open file"; 

  return 0;
}

bool checkIfFloatType( string str ) {
    std::istringstream iss(str);
    float f;
    iss >> noskipws >> f;
    return iss.eof() && !iss.fail(); 
}


来源:https://stackoverflow.com/questions/31645524/reading-floats-words-symbols-from-a-file-and-only-storing-the-floats-in-an-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!