-2

@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?

Quest
  • 2,764
  • 1
  • 22
  • 44
  • 2
    You're practically begging for a buffer overflow. – chris Oct 25 '14 at 15:13
  • @chris I learned this "style" from Microsoft examples. – Quest Oct 25 '14 at 15:14
  • @for downvoters **LEAVE SOME COMMENT** – Quest Oct 25 '14 at 15:15
  • @πάνταῥεῖ same if i change that to `while (true)` but they should! – Quest Oct 25 '14 at 15:18
  • 2
    while (InFile >> cmd) – Jason Ball Oct 25 '14 at 15:18
  • @Quest, I can assuredly tell you that MSDN examples are not the way to go for good style. They're useful for learning how to use the Windows API and such, but even then, you really kind of have to ignore the style on most things. It really doesn't help that it's usually entirely in C, and C style doesn't translate well to C++. – chris Oct 25 '14 at 15:21
  • [`while (!InFile.eof())`](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong) Don't yell on the downvoters, [they don't need to do so](http://meta.stackoverflow.com/questions/267498/getting-downvoted-yet-no-comment-as-to-why). – πάντα ῥεῖ Oct 25 '14 at 15:23
  • @chris At least i know that DirectXs work with files is painly wrong :D – Quest Oct 25 '14 at 15:24
  • What guarantees that InFile will limit to 256 bytes? At least use a `std::string` instead of a `char[]`. – tillaert Oct 25 '14 at 15:26
  • if the change I told you didnt work. If you post up more of your code and a small portion of the text file you are trying to parse, I will see where the other errors are. – Jason Ball Oct 25 '14 at 15:29
  • **Even after changing `cmd` to `std::string` and `while(true)` to `while(InFile >> cmd)` didn't help. The same problem still occur – Quest Oct 25 '14 at 15:29
  • 1
    Remember that whenever you read from the stream, you need to check whether the read was successful. You cannot blindly work with the read value. – Jiří Pospíšil Oct 25 '14 at 15:32
  • @JasonBall There is no problem with just a sigle if statement but when i add one more if statement, that weird thing happen again – Quest Oct 25 '14 at 15:44
  • you would make my life much easier if you post up structs as well. I think I have it fixed for you but not 100% sure because i dont have the Layer and those – Jason Ball Oct 25 '14 at 15:54

1 Answers1

1

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;
}
Jason Ball
  • 163
  • 8