使用包含字符和数字的c ++读取文件

时间:2018-05-25 07:34:47

标签: c++ file

我有一个包含图形表示的文本文件,如下所示:

7

{5,2,3},{1,5},{},{},{3},{},{}

现在,我知道如何读取文件并进入int

    while ((n = myfile.get()) != EOF) 

或逐行写入字符串

    getline (myfile,line)

我遇到的问题是,对于这两个选项,我似乎无法比较我提取的每个字符,并检查它是数字还是“,”或“{”或“}”。 有一个简单的方法吗?从昨天开始,我一直在用这几个小时敲打我的脑袋。我已经尝试了一些isdigit和cast,但这对我来说并不适用,而且非常复杂。

2 个答案:

答案 0 :(得分:0)

我认为最简单的解决方案是通过char读取char文件来弄脏你的手。我建议你将这些集合存储在int s向量的向量中(如果你愿意,可以将它看作2D阵列,矩阵)。

如果第i个集合为空,那么第i个向量也将为空。

在您将解析字符的循环中,您将跳过开头的花括号和逗号。除了需要更新索引这一事实外,你会对结束花括号做类似的事情,这将有助于我们更新索引向量。

当我们实际读取数字时,您可以convert a char to an int

完整示例:

#include <iostream>
#include <fstream>
#include <vector>
using namespace std;

int main(void) {
    char ch;
    fstream fin("test.txt", fstream::in);
    if(!fin) {
        cerr << "Something is wrong...! Exiting..." << endl;
        return -1;
    }
    int N; // number of sets
    fin >> N;
    vector<vector<int> > v;
    v.resize(N);
    int i = 0;
    while (fin >> ch) {
            //cout << ch << endl;
        if(ch == '{' || ch == ',')
            continue;
        if(ch == '}') {
            i++;
            continue;
        }
        v[i].push_back(ch - '0');
    }
    if(i == N) {
        cout << "Parsing the file completed successfully." << endl;
    } else {
        cout << "Parsed only " << i << " sets, instead of " << N << endl;
    }
    for(size_t i = 0; i < v.size(); ++i) {
        if(v[i].size() == 0)
            cout << i + 1 << "-th set is empty\n";
        else {
            for(size_t j = 0; j < v[i].size(); ++j)
                cout << v[i][j] << " ";
            cout << endl;
        }
    }
    return 0;
}

输出:

gsamaras @ aristotelis:/ Storage / homes / gsamaras $ g ++ main.cpp

gsamaras@aristotelis:/Storage/homes/gsamaras$ ./a.out 
Parsing the file completed successfully.
5 2 3 
1 5 
3-th set is empty
4-th set is empty
3 
6-th set is empty
7-th set is empty

重要提示:这应该作为一个起点,因为它不会处理具有多个数字的数字。在这种情况下,您将读取逗号或结束大括号,以确保您读取数字的所有数字,然后从字符串转换为整数,然后将其存储在相应的向量中。

答案 1 :(得分:0)

最好按字符读取字符如下:

#include<iostream>
#include<fstream>
#include<string>
using namespace std;

int main()
{
    string line;
    int lineNum = 0;
    ifstream myfile ("file.txt");
    if (myfile.is_open())
    {
        while ( getline (myfile, line) )
        {
            // Identifying the type of charcter
            char a;
            cout << "Line Number " << ++lineNum << ": ";
            for(int i = 0; i < line.length(); i++)
            {
                a = line[i];
                cout << "\n\t" << a;
                if((a >= 32 && a <= 47) || (a >= 58 && a <= 64) || (a >= 91 && a <= 96) || (a >= 123 && a <= 126))
                {
                    cout << "\t(This character is either a Punctuation or arithmetic mark.)";
                    // If you don't want to porcess these character, then you may 'continue' to the next sub string
                }
                else if(a >= 48 && a <= 57)
                {
                    cout << "\t(This character is a number.)";
                    // If you want to change the number ot int, you can use atoi or stoi
                }
                else if(a >= 65 && a <= 90)
                    cout << "\t(Capital letter)";
                else if(a >= 97 && a <= 122)
                    cout << "\t(Small letter)";

            }
            cout<<endl;
        }
        myfile.close();
    }
    else
        cout << "Unable to open file";
    return 0;
}

我跳这个有帮助。

相关问题