用C ++读取文件中的数字

时间:2016-03-07 20:16:24

标签: c++

我希望通过逐个符号读取文本文件中的数字,然后将数字输出到另一个文件中。

数字 - 仅由数字符号组成的单词。

Word - 由点,逗号,括号,感叹号,问号或换行符分隔的任何符号字符串。

例如来自档案

in.txt:

masnd kasjd k!1234 564,7
7.,43, mb?? 67hh k4k 87 90.

我想输出

out.txt

1234
564
7
7
43
87
90

我试着读完整个单词以检查这个单词是否是一个数字,如果它被放入'光标'在单词的开头,将其输出到out.txt

我搞砸了,而且我在out.txt文件中的所有内容都是

out.txt

234
64

7
87
90

如果我没有在in.txt文件的末尾放置一个分隔符号,它会在某个地方停留在一个循环中,当我关闭程序并尝试打开out.txs文件时,记事本会破坏

这是我做了多远

#include <iostream>
#include <cctype>
#include <fstream>
using namespace std;
bool isStop(char c);
int main(){
    fstream fin;
    fstream fout;
    streampos p;
    char a,b=' ';
    fin.open("in.txt", ios::in);
    fout.open("out.txt", ios::out);
    fin.get(a);
    while(!fin.eof()){
        if(isdigit(a) && isStop(b)){
            p=fin.tellg();
            while(isdigit(a) && !fin.eof()){
            fin.get(a);
            }
            if(isStop(a) || fin.eof()){
                fin.seekg(p-1, ios::beg);
                fin.get(a);
                while(isdigit(a) && !fin.eof()){
                    fout.put(a);
                    fin.get(a);
                }
                fout.put('\n');
            }
        }
        b=a;
        fin.get(a);
    }
    fin.close();
    fout.close();
    return 0;
}
bool isStop(char c)
{
    return (c==' ' || c=='.' || c==',' || c=='(' || c==')' || c=='!' ||                             c=='?' || c=='\n');
}

3 个答案:

答案 0 :(得分:0)

我怀疑你的问题源于一些事情,例如将空格字符视为唯一的空白字符(当可能有换行符'\n',制表符'\t'或回车符{{1}时) }),另外使用'\r'作为循环检查。

事实上,文件中的所有跳跃都显得不必要且难以阅读。

我提出了以下简化算法(更多C ++ - esque):

  • 读一行

  • 将该行解析为单词

  • 确认单词是有效号码(根据您的规则)

  • 如果有效,请将该号码写入档案

以下是一种可能的实施方式(使用!fin.eof()cin代替文件):

定义分隔符:

cout

阅读一行:

const std::string separators = ",.()! \t";

将其分解为单词:

std::string next;
while(getline(std::cin, next))
{

确认该字是从第一个数字到下一个分隔符的所有数字:

    std::string::iterator beg = std::begin(next);
    while(beg != std::end(next))
    {
        beg = std::find_if(beg, std::end(next), [](char c)
        {
            return std::isdigit(c);
        });

        std::string::iterator end = std::find_if(beg, std::end(next), [](char c)
        {
            return separators.find(c) != std::string::npos;
        });

        // now we have digit to separator (or end of string). So long as elements between beg (inclusive) and end(exclusive) are all digits, we're good

如果验证通过,请写入文件:

        std::string::iterator it = beg;
        for(; it != end && isdigit(*it); ++it)
        {}

Live Demo

答案 1 :(得分:0)

更像c ++:

#include <fstream>
#include <string>
#include <algorithm>
#include <iterator>

using namespace std;
// string of separators 
static const string valid = ".,!? \n\0'";   

// check whether a character is in the separator
static bool isValid(char c) { 
  return (find(valid.begin(), valid.end(), c) != valid.end());
}

int main() {
  // open your input/output files
  ifstream inputf("./in.txt");
  ofstream outf("./out.txt");
  // save your input in a string
  string file((istreambuf_iterator<char>(inputf)), (istreambuf_iterator<char>()));
  int beg; // start of a number (index)
  std::string nb;
  // iterate over your string
  for (int i = 0; i < file.size() ;) {
    // while your char is an alpha char, do nothing
    while (isdigit(file[i]) == false) ++i;
    nb = "";
    beg = i;
    // search for a number
    while (isdigit(file[i]) == true)
      nb += file[i++];
    // check if the current number can be saved in the output file
    if (nb != "" && isValid(file[i]) && (beg == 0 || isValid(file[beg - 1])))
      outf << nb << "\n";
  }
  inputf.close(); outf.close();
}

然后输出文件:

$> cat out.txt 
1234
564
7
7
43
87
90

答案 2 :(得分:0)

我必须逐个符号检查文件符号,所以我不能使用getline,但最后我发现了这一切。

我将这个单词保存在一个字符串中,然后检查它是否是一个数字,如果是,我把它放在out文件中。

以下是代码:

#include <iostream>
#include <fstream>
using namespace std;
bool isStop(char c);
int main(){
    fstream fin;
    fstream fout;
    string s;
    char a,b=' ';
    fin.open("inF20.txt", ios::in);
    fout.open("outF20.txt", ios::out);
    while(fin.get(a)){
        if(isdigit(a) && isStop(b)){
                s+=a;
            while(fin.get(a) && isdigit(a)){
                s+=a;
            }
            if(isStop(a) || fin.eof()){
                for(int i=0;i<s.length();i++){
                    fout.put(s[i]);
                }
                fout.put('\n');
            }
        }
        s.clear();
        b=a;
    }
    fin.close();
    fout.close();
    return 0;
}
bool isStop(char c)
{
    return (c==' ' || c=='.' || c==',' || c=='(' || c==')' || c=='!' || c=='?' || c=='\n');
}