dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 2 FirstFirst 12
Results 16 to 21 of 21

Thread: code problem

  1. #16
    Join Date
    Sep 2003
    Posts
    22

    Re: code problem

    try inserting the line
    file.resize(filesize);

    before the loop in your code and it will work

  2. #17
    Join Date
    Apr 2004
    Location
    Canada
    Posts
    1,342

    Re: code problem

    Hixidom,

    You don't have to use vector if you don't want to. String will work just fine.
    Your code doesn't work because you changed two important things in my code. I made these critical things bold:

    My code:
    Code:
    ifstream object("hscene.txt", std::ios_base::in | std::ios_base::out | ios_base::ate);  // 'ate' flag means bring the file pointer to the end of the file
    int file_size = object.tellg();  // determine file size
    object.seekg(0);  // go to beginning of file
    string file(file_size, '\0');  // pre-allocate required length of string
    for (int i = 0; i < file_size; ++i)
        file[ i ] = object.get();
    Your code:
    Code:
    void fdownload()
         {ifstream object("hscene.txt", std::ios_base::ate);
         int x, filesize=object.tellg();
         object.seekg(0);
         for(x=0; x<filesize; x++)
                 {object.get(file[x]);}
         object.close();}
    The first is the way you use get().
    Code:
    file[x] = object.get();
    get() with no arguments reads one character from the file and assigns it to the x-th position in the string 'file'.

    What you did was this:
    Code:
    object.get(file[x]);
    You're trying to use the version of get() that takes an argument. This version reads not just one character, but an entire string of characters from the file up until the next newline (or whatever character you specified). The problem is, this version of get() expects a char* argument, but file[x] is a char. So you're probably having the value of a char interpreted as a pointer which is bound to give you problems.

    In any case, you just want to read one character so get() with no arguments is the version you need.

    EDIT: I see you've already corrected this in your most recent version.

    The second thing you left out is the statement
    Code:
    string file(file_size, '\0');  // pre-allocate required length of string
    This makes sure your string is large enough to hold the file. The reason you need to do this is because of the way you are putting data into your string:

    file[x] = object.get();

    This reads a character from the file and assigns the character to the x-th position in the string 'file'. This code assumes that the string is already large enough to hold x characters. So you need to pre-allocate the space.

    If you did it the other way (how IllegalCharacter suggested):
    Code:
    file += object.get();
    This code reads a single character from the file and appends it to the end of the string 'file'. In other words it allocates more space for the character. The reason I didn't like this solution is that it has to reallocate the string quite a few times to constantly make space for new characters. Better to allocate the entire space you need at the beginning, which is what this code does:

    Code:
    string file(file_size, '\0');  // pre-allocate required length of string
    Innovaltec's code

    Code:
    file.resize(file_size);
    does essentially the same thing (pre-allocates file_size characters).

    Question: How is x++ different from ++x ?
    Without getting into it too much, the expression x++ evaluates to x but has the effect of incrementing x by 1. The expression ++x evaluates to x + 1 and also has the effect of incrementing x by 1.

    Try these code fragments to see:

    Code:
    int x = 5;
    cout << x++;  // displays 5 (original value of x), but increments x to 6
    cout << x;    // x is now 6
    Code:
    int x = 5;
    cout << ++x;  // increments x to 6, displays 6 (new value of x)
    cout << x;    // x is now 6
    In a for loop, it doesn't matter which one you use. I use ++x out of habit because it can be a little more efficient when x is not a simple integer but an iterator object (if you haven't heard of iterators, don't worry about it).
    Last edited by HighCommander4; July 13th, 2007 at 02:54 PM.
    Old Unix programmers never die, they just mv to /dev/null

  3. #18
    Join Date
    Jul 2007
    Posts
    33

    Re: code problem

    Sorry for neglecting this thread!
    I've been out of town for a few days. Actually, I'll be out of town for the next week, but I'll try to get in one more post before I leave (regarding program code modifications).

    Thanks, HighCommander4, for your most recent post, and thanks again to everyone else who has helped out with my problem!
    Andy

  4. #19
    Join Date
    Jul 2007
    Posts
    33

    Re: code problem

    Alright!

    Thanks for everyone's help!
    I finished "debugging" the program about an hour ago. It's working perfectly!

    One thing that I lament is how long it takes for the program to run. It's a type of raytracing program, so I guess long run-time is typical (depending on the size of the file, resolution of the rendered image, etc.). I'm not suprised that it's taking as long as it is (about 1.5 hours for a simple model), but I sure am glad that it is working fine.

    Thanks again for the help;
    I've learned a lot from this project, that's for sure!

    Andy

  5. #20
    Join Date
    Jul 2007
    Posts
    33

    Re: code problem

    The program finished the output model after running for 20 hours.

    Do you know why it takes so long to run? It doesn't seem like it should take as long as it does.
    Is there a way to make the program run faster on my computer? Is there a way to allocate more processing "space" to the program?

    Andy

  6. #21
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,651

    Re: code problem

    Hard to say why it takes so long for your program to finish. Get yourself a profiler to find out where the time is consumed. Maybe there is a profiler already in DevC++?

    Regarding processing time, I don't think there is anything you can do since the normal windows settings for a desktop machine is (as far as I know) is to prioritize the active window. This means that your application get as much time as possible. You should be able to see this in task manager. Here you also can modify process priority to test if I'm right on this...

Page 2 of 2 FirstFirst 12

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)