文件阅读麻烦

时间:2014-10-25 15:12:12

标签: c++ ifstream

@Jason Ball这是整个功能:

std::ifstream InFile(filename);
if (!InFile)
    return E_FAIL;

std::vector<Layer>Layers;
std::vector<PatternSet>Patterns;
std::vector<Mood>Moods;
Layer layer;
PatternSet ps;
Mood mood;
Theme theme;

layer.fileInfo.padding = layer.fileInfo.data = nullptr;
layer.fileInfo.len = 0;

std::string cmd;
while (true)
{
    InFile >> cmd;
    if (!InFile)
        break;

    if (cmd == "f")
    {
        InFile >> layer.fileInfo.filename;
    }
    else if (cmd == "fp")
    {
        int data, n; InFile >> n;
        for (int a = 0; a < n; a++)
        {
            InFile >> data;
            layer.fadePoints.push_back(data);
        }
    }
    else if (cmd == "sp")
    {
        int data, n; InFile >> n;
        for (int a = 0; a < n; a++)
        {
            InFile >> data;
            layer.syncPoints.push_back(data);
        }
    }
    else if (cmd == "v")
    {
        InFile >> layer.volume;
    }
    else if (cmd == "#layerend")
    {
        Layers.push_back(layer);
    }
    else if (cmd == "#patternset")
    {
        int Index;
        for (int a = 0; a < 5; a++)
        {
            InFile >> Index;
            if (Index != -1)
                ps.pattern[a] = std::move(Layers[Index]);
        }
        Patterns.push_back(ps);
        memset(ps.pattern, 0, sizeof(Layer)* 5);
    }
    else if (cmd == "#mood")
    {
        InFile >> mood.name;
        int Index, n; InFile >> n;
        for (int a = 0; a < n; a++)
        {
            InFile >> Index;
            mood.data.push_back(Patterns[Index]);
        }
        Moods.push_back(mood);
        mood.data.clear();
    }
    else if (cmd == "#theme")
    {
        InFile >> theme.name;
        int Index, n; InFile >> n;
        for (int a = 0; a < n; a++)
        {
            InFile >> Index;
            theme.data.push_back(Moods[Index]);
        }
        m_vTheme.push_back(theme);
        theme.data.clear();
    }
    else
    {
    }
}
return S_OK;

这是一个文件:

#layer
f filename
fp 4 0 1998 1245 1003482
sp 3 500 1200 9500
v 0.95
#layerend

#layer
f filename2
fp 4 0 1998 1245 1003482
sp 3 500 1200 9500
v 0.75
#layerend

#patternset -1 0 -1 -1 -1
#patternset -1 1 -1 -1 -1

#mood name n 0 1

#theme name n 0

@Jason Ball在这里你也有这些结构:

struct node
{
    union{
        struct{
            std::string filename;
            void *padding;
        };
        struct{
            void *data;
            unsigned int len;
        };
    };
};
struct Theme;
struct Mood;
struct PatternSet;
struct Layer
{
    node fileInfo;
    std::vector<int> fadePoints;
    std::vector<int> syncPoints;
    float volume;
    PatternSet *pUp;
};
struct PatternSet
{
    union{
        struct{
            Layer pattern[5];
        };
        struct{
            Layer start;
            Layer main;
            Layer end;
            Layer rhytmic;
            Layer incidental;
        };
    };
    Mood *pUp;
};
struct Mood
{
    std::vector<PatternSet> data;
    std::string name;
    Theme *pUp;
};
struct Theme
{
    std::vector<Mood> data;
    std::string name;
};

当我在任何地方设置断点时,它显示在第一个if块后,即使文件包含超过5 000行,它也会跳转到返回行。
几个月前我遇到了同样的问题,但它不知何故开始工作了。什么可以导致这个问题的想法?

1 个答案:

答案 0 :(得分:1)

试试这个。希望它有所帮助。

{
    ifstream InFile("text.txt");

    if (!InFile) return 0;

    string cmd;
    while (InFile >> cmd)
    {
        cout << "\ncmd " << cmd;
        if (cmd == "f")
        {
            string name;
            if (InFile >> name) {
                layer.fileInfo.filename = name;
                cout << "\nfilename: " << layer.fileInfo.filename;
            }
        }
        else if (cmd == "fp")
        {
            int data, n; 
            if (InFile >> n) {
                for (int a = 0; a < n; a++)
                {
                    if (InFile >> data) {
                        cout << "\nAdding " << data;
                        layer.fadePoints.push_back(data);
                    }
                }
            }
        }
        else if (cmd == "sp")
        {
            int data, n; 
            if (InFile >> n) {
                for (int a = 0; a < n; a++)
                {
                    if (InFile >> data) {
                        layer.syncPoints.push_back(data);
                    }
                }
            }
        }
        else if (cmd == "v")
        {
            float vol;
            if (InFile >> vol) {
                layer.volume = vol;
            }
        }
        else if (cmd == "#layerend")
        {
            Layers.push_back(layer);
        } 
        else if (cmd == "#patternset")
        {
            int Index;
            for (int a = 0; a < 5; a++)
            {
                if (InFile >> Index) {
                    if (Index != -1) {
                        ps.pattern[a] = std::move(Layers[Index]);
                    }
                }
            }
            Patterns.push_back(ps);
            memset(ps.pattern, 0, sizeof(Layer)* 5);
        }
        else if (cmd == "#mood")
        {
            if (InFile >> mood.name) {
                cout << "\nmood.name " << mood.name;
                int Index, n;
                if (InFile >> n) {
                    for (int a = 0; a < n; a++)
                    {
                        if (InFile >> Index) {
                            cout << "\nmood.data.push_back( " << Index << " )";
                            mood.data.push_back(Patterns[Index]);
                        }
                    }
                    Moods.push_back(mood);
                }
            }
        }
        else if (cmd == "#theme")
        {
            if (InFile >> theme.name) {
                cout << "\ntheme.name " << theme.name;
                int Index, n;
                if (InFile >> n) {
                    for (int a = 0; a < n; a++)
                    {
                        if (InFile >> Index) {
                            cout << "\ntheme.data.push_back( " << Index << " )";
                            theme.data.push_back(Moods[Index]);
                        }
                    }
                    m_vTheme.push_back(theme);
                }
            }
        }
        else
        { 
            //
        }
    }
    return 1;
}
相关问题