Dynamic Two-Dimensional Arrays

This is a discussion on Dynamic Two-Dimensional Arrays within the C++ Programming forums, part of the General Programming Boards category; LloydUzari---If you have a debugger, step through it line by line. If not start throwing in lines like cout << ...

  1. #16
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    LloydUzari---If you have a debugger, step through it line by line. If not start throwing in lines like cout << "Here" << endl; to see how far your code gets before it bottoms out. Then you can start putting in lines to display a given variable to make sure it is being read/changed appropriately. This manual technique for debugging is a bit tedious, but it usually works, especially if you aren't comfortable with a debugger.

    The code for creating a 2D array dynamically seems okay in your post. Sometimes the problem is as simple as making sure that you have the dimensions correctly. That is, are you sure data_length is the first size read in and data_width the second?

  2. #17
    C++ n00bie :D
    Join Date
    Jul 2004
    Posts
    63
    Right now, that wouldnt matter cause their both the same currently(10)

    I have debugger in compiler, but the annoying thing itself is bugged and dont work I did the cout way of debuggin, and saw it freezes while declaring the array(I see the 'TEST' when I put it before it, but not after.)

    I tried both ways of declaring the array, too(the way I have on code I posted and [length*width])

    It also itsnt scanning the file right and is returning the values of length and height as 75x2406 instead of 10x10. Neither the file or the read mode is binary, got any clue why this is happening.

    Note: by some fluke chance, it worked ONCE when I made the file read only, but now even read only doesnt work. When it did work, however, it's length and width were right, but its return from file was wrong... It wrote a whole bunch of random numbers instead of 20x20 grid of 0's(each slot had 00)

    Gotta go right now, Ill try get() instead of >> later next to see if it works

  3. #18
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Show us the code for loading the dimensions from the file; and then post the file itself.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  4. #19
    C++ n00bie :D
    Join Date
    Jul 2004
    Posts
    63
    Its pretty darn absic... I updated the loading of dimensions with get(), and used atoi to make it int. works much better and now only problem is it doesnt load what I want it too(the values come out 0x0, instead of 10x10, doesnt close instant now though)

    Set-Size function:
    Code:
    void testclass::setsize() {
      char *char_length=new char[3];
      char *char_width=new char[3];
      std::ifstream s_size(dat_filename);
      s_size.get(char_length, 2, ' ');
      s_size.get(char_width, 2, ' ');
      s_size.close();
      dat_length=atoi(char_length);
      dat_width=atoi(char_width);
    }//testclass setsize func
    The file:
    Code:
    10 10
    00 00 00 00 00 00 00 00 00 00
    00 00 00 00 00 00 00 00 00 00
    00 00 00 00 00 00 00 00 00 00
    00 00 00 00 00 00 00 00 00 00
    00 00 00 00 00 00 00 00 00 00
    00 00 00 00 00 00 00 00 00 00
    00 00 00 00 00 00 00 00 00 00
    00 00 00 00 00 00 00 00 00 00
    00 00 00 00 00 00 00 00 00 00
    00 00 00 00 00 00 00 00 00 00
    The file is pretty basic, just a test right now. Its just that it doesnt load the first two properly.
    Last edited by LloydUzari; 08-09-2004 at 04:07 PM.

  5. #20
    Registered User
    Join Date
    Jan 2003
    Posts
    311
    Are you sure the file is opening at all? I don't see any error checking. Run this
    Code:
    std::ifstream s_size(dat_filename);
    if(s_size) {
        int length, width;
        if(s_size >> width >> length) {
            std::cout << "file is " << width << " wide by " << length << " long";
            int count=0,n=0; 
            while(s_size>>n && n==0) ++count;
            std::cout << " I read " << count << " zeros and expected "<< (width*length) << std::endl;
            if(s_size.eof()) std::cout << "I read the whole file" << std::endl;
            else if(s_size) std::cout << "I stopped because I read a " << n << std::endl;
            else if(s_size.fail()) {
                s_size.clear();
                char ch;
                if(s_size.get(ch)) std::cout << "I choked on the character '" << ch << "'" << std::endl;
                else std::cout << "I cannot even read a single character now" << std::endl;
            } else {
                std::cout << "The stream has somehow become courrupt, but not fail()" << std::endl;
            }
        } else if(s_size.fail()) {
            s_size.clear();
            char ch;
            if(s_size.get(ch)) std::cout << "I choked on the character '" << ch << "'" << std::endl;
            else std::cout << "I cannot even read a single character now" << std::endl;
        } else {
            std::cout << "The stream has somehow become courrupt, but not fail()" << std::endl;
        }
    } else {
        std::cout << "I could not open '" << dat_filename << "'" <<std::endl;
    }

  6. #21
    C++ n00bie :D
    Join Date
    Jul 2004
    Posts
    63
    Accually, the file checking is outside of the functions. I did have file checking in the functions before, but they were slowing the program down like crazy, so basically how it works now is before it starts loading, it uses the checkfile func I made

    Code:
    int testclass::checkfile() {
      std::ofstream c_file(dat_filename);
      if (c_file) {
        c_file.close();
        return 1;
      }
      c_file.close();
      return 0;
    }//testclass checkfile func

  7. #22
    Registered User
    Join Date
    Jan 2003
    Posts
    311
    std::ofstream c_file(dat_filename); Did you really want to truncate the file? You have a zero length file living somewhere, but it aint where you think.

  8. #23
    C++ n00bie :D
    Join Date
    Jul 2004
    Posts
    63
    Oh wait, the preset for ofstream deletes the file, right? That might explain it

    EDIT: I changed it to fstream, but now it does 1x0, not 10x10 Any idea why its stopping after 1 char instead of a space?
    Last edited by LloydUzari; 08-09-2004 at 06:00 PM.

  9. #24
    Registered User
    Join Date
    Jan 2003
    Posts
    311
    Because you are using get to only read 1 character, get(s,n,' ') reads n-1 characters at most and writes a '\0' at s[k] where k was the number of characters read(and thus at most n-1). get will also always leave the terminating charater so the second or later get's will never get past the first space. if(is.get(s,n,t) && is.get() == t) is the normal way to tell if you have read a whole line and discard the terminator
    Last edited by grib; 08-09-2004 at 06:13 PM. Reason: goud spehller me is

  10. #25
    C++ n00bie :D
    Join Date
    Jul 2004
    Posts
    63
    Thanks, that fixed the rest. I added seekg to get past that terminating thing

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating and freeing dynamic arrays
    By circuitbreaker in forum C++ Programming
    Replies: 8
    Last Post: 02-18-2008, 10:18 AM
  2. Replies: 16
    Last Post: 01-01-2008, 03:07 PM
  3. Dynamic two dimensional arrays
    By ThWolf in forum C++ Programming
    Replies: 14
    Last Post: 08-30-2006, 02:28 PM
  4. processing dynamic arrays
    By Mario F. in forum C++ Programming
    Replies: 9
    Last Post: 06-04-2006, 11:32 AM
  5. Dynamic (Numeric) Arrays
    By DavidB in forum C++ Programming
    Replies: 5
    Last Post: 05-03-2006, 07:34 PM

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