Search code examples
c++ifstream

File reading trouble


@Jason Ball this is the whole function:

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;

and here is a file:

#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 here you have those structures as well:

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;
};

When I set breakpoints everywhere, it shows that after first if block it jumps to return line even when the file contains more than 5 000 lines.
I had the same problem a few months ago, but it somehow got to work. Any ideas what can cause this problem?


Solution

  • try this. Hope it helps.

    {
        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;
    }