Runtime Error

This is a discussion on Runtime Error within the C++ Programming forums, part of the General Programming Boards category; Hello, I'm working on a model loader, and I'm getting a runtime error. I read the text in line by ...

  1. #1
    The Reel Thing
    Join Date
    Jun 2005
    Posts
    44

    Runtime Error

    Hello, I'm working on a model loader, and I'm getting a runtime error. I read the text in line by line, and read in the text using sscanf. I don't know how to explain how the error is caused, but here's my code.

    Code:
    // Include Files
    #include <iostream>
    #include <string>
    #include <vector>
    #include <fstream>
    #include "debug.h"
    
    // Using standard namespace
    using namespace std;
    
    // Main Program
    int main()
    {
       // Declare vars
       vector<MESH> meshes;       // Vector to hold the meshes of the model
       string current_line;       // Buffer for reading in the file
       bool error = false;
       
       // Open the model file and read it into the raw_data string
       ifstream File("target.txt");
       
       // Check to see if the file is open
       if (!File.is_open())
       {
          cout << "Error loading file" << endl;
          system("pause");
          return 0;
       }
       
       // Read in the file line by line
       while (getline(File, current_line, '\n') && error == false)
       {
          // Declare temp vars
          short num_meshes;
          short flag;
          short mat_index;
          string name;
          
          // Check the current line and see it contains the mesh count
          if (sscanf(current_line.data(), "Meshes: %hd", &num_meshes) == 1)
          {
             // Start a loop and go through each mesh
             for (short i = 0; i < num_meshes && error == false; i++)
             {
                // Initialize a new mesh
                meshes.push_back(MESH());
                
                // Get the next line
                if (!getline(File, current_line, '\n'))
                {
                     error = true;
                     break;
                }
                // Get the mesh name
                if (sscanf(current_line.data(), "\"%[^\"]\" %hd %hd", &name, &flag, &mat_index) != 3)
                {
                   error = true;
                   break;
                }
             }
          }
       }
       
       // Close the file
       File.close();
       
       system("pause");
       
       return 0;
    }
    The bolded text is the section of code I think is causing the problem. When ever I take that section out and recompile, it runs without an error. I'm sorry if I'm being vague on this, I just don't know how to explain it properly. Any help at all is appreciated

    Thanks
    Bagpipes putting the fun back in funeral.

  2. #2
    Registered User
    Join Date
    Nov 2002
    Posts
    491
    sscanf doesn't know what std::string's are, and what the heck is %hd? What is \"%[^\"]\" suposed to be?

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by orbitz
    and what the heck is %hd? What is \"%[^\"]\" suposed to be?
    The %hd is a directive corresponding to a short -- its argument should be the address of such, and the \"%[^\"]\" is a directive corresponding to a C-style string of text (of unspecified width) enclosed within double quotes -- its argument should be a pointer to char.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  4. #4
    The Reel Thing
    Join Date
    Jun 2005
    Posts
    44

    re:

    So you're saying that instead of passing std::strings to sscanf, pass char arrays? I don't know why this would be causing the error, because I've used sscanf to pass info into strings before without a problem.
    Bagpipes putting the fun back in funeral.

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    sscanf expects a null terminated string. The data() function does not return a null-terminated string. Use the c_str() function instead of the data() function. You can still use strings here.

  6. #6
    The Reel Thing
    Join Date
    Jun 2005
    Posts
    44
    Thank you, I tried what you said, but the error still popped up. Then, instead of passing the name value to a string, I tried it with a char array, and it worked fine. I really don't like using char arrays, I'd rather use strings, so is there any way to have the value passed into a string without causing an error?
    Bagpipes putting the fun back in funeral.

  7. #7
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Use a stringstream instead of sscanf for parsing?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    It should work the same for c_str() and for a char array, since c_str just returns a char array.

    Try adding a cout before both calls to sscanf (the first one might be messing up also). Do that with both the working version and the bad version. See if the strings are really the same.

    Of course, I normally use a stringstream for these types of things.

  9. #9
    The Reel Thing
    Join Date
    Jun 2005
    Posts
    44
    I have figured out a solution. Seeing as I can't get the thing to work with strings, I'll just use a char array. You see, what I'm doing is, instead of assigning the string value found using sscanf() to a string, I'll just use a char array, and then assign that array to the string that I need for my loader. This is going to be in a function when I'm finished with it anyway, so if my C++ teacher was right, that char array I declared in the function should fall out of scope after the function is done. Once again, thanks for all your help
    Bagpipes putting the fun back in funeral.

  10. #10
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    FWIW
    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <sstream>
    #include <vector>
    #include <iterator>
    #include <algorithm>
    using namespace std;
    
    struct MESH
    {
       string name;
       short flag, index;
    };
    
    void load(const char *filename, vector<MESH> &meshes)
    {
       ifstream file(filename);
       string line;
       short count;
       if ( file >> line >> count && line == "Meshes:" )
       {
          short i = 0;
          while ( i < count )
          {
             if ( getline(file, line) )
             {
                istringstream iss(line);
                MESH m;
                char quote;
                if ( iss >> quote && quote == '"' && getline(iss, m.name, '"') && 
                     iss >> m.flag >> m.index )
                {
                   meshes.push_back(m);
                   ++i;
                }
             }
          }
       }
    }
    
    ostream& operator<< (ostream &o, const MESH &m)
    {
       return o << m.name << ',' << m.flag << ',' << m.index;
    }
    
    int main()
    {
       vector<MESH> meshes;
       load("file.txt", meshes);
       copy(meshes.begin(), meshes.end(), ostream_iterator<MESH>(cout, "\n"));
       return 0;
    }
    
    /* file.txt
    Meshes: 3
    "Testing..." 1 2
    "Testing again" 3 4
    "Oh, yes -- I'm testing again!" 5 6
    "One too many lines" 7 8
    */
    
    /* my output
    Testing...,1,2
    Testing again,3,4
    Oh, yes -- I'm testing again!,5,6
    */
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Testing some code, lots of errors...
    By Sparrowhawk in forum C Programming
    Replies: 48
    Last Post: 12-15-2008, 03:09 AM
  2. Connecting to a mysql server and querying problem
    By Diod in forum C++ Programming
    Replies: 8
    Last Post: 02-13-2006, 09:33 AM
  3. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Stupid compiler errors
    By ChrisEacrett in forum C++ Programming
    Replies: 9
    Last Post: 11-30-2003, 04:44 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21