Search code examples
c++filestreammultidimensional-array

How do I read text into a 2D array of larger parameters than the text (2D array has more rows and columns) using object.get()?


I am attempting to read a text file of characters into a character array (char array[MAX_ROW][MAX_COL]) and the text file has less rows and columns than the character array. This leads to a problem of reading past the substance material that is within the text file. I run into this problem when I read in using the following method:

ifstream whiteboard; //ifstream object for reading from a file.
    whiteboard.open(board_name.c_str());

    for(int i = 0; i < MAXROW; i++){ //naive read for canvas, not considering the /n characters or the size of the image.txt
        for(int j = 0; j < MAXCOL; j++){ //for this to store the print properly it must be the exact size of the image dimensions
                canvas[i][j] = whiteboard.get();
            if(whiteboard.get() == '/n'){
               return;
            }
            else if(whiteboard.get() != '/n'){
            canvas[i][j] = whiteboard.get();
            }
        }
    }

    whiteboard.close();

The code above is supposed to run through the 2d array only to the point where the '/n' character is. Thus, allowing me to enter an array of any size. The newline character is at the end of each line of text in a text file (in the form of an enter keystroke). The read to file stops when a newline has been read. However, I am having issues implementing this correctly. Does anyone have any hints that may help me see this more clearly? Thank you very much!

EDIT:

Input File

xxxxxxxxxxxxxx
x            x
x            x
x            x
x            x
x            x
xxxxxxxxxxxxxx 

I am hoping to input the box above (along with all of its ' ' characters contained within it) into corresponding values of the character array. I wish to stop the read after reading the rightmost column of x's in and after reading the final x (the bottom right). You can see that my problem comes from having an array that is larger than the size of my text box here. I understand I could fix this by equating the character array to have the same dimensions as the text box, but I wish to keep a large enough constant so that I can read in a file of a relatively large size.


Solution

  • I have one solution, do the following:

    int n=1025, m=1024, MAX_LINES=1025, i=0, j=0;
    char character, Matrix[n][m];
    fstream file;
    file.open("file.txt", ios::in);
    while (file.get(character))// loop executes until the get() function is able to read contents or characters from the file
    {
    Matrix[n][m]=character; // copy the content of character to matrix[n][m], here n is used to work as lines and m as normal arrays.
    m++; // finished copying to matrix[n][m] then m++ else the program will overwrite the contents to the same array.
    If (m>=1024)// if string[n][m] reached the limit which is 1024.
    {
    Matrix[n][m]='\0'; //termimate that line
    m=0;// start with a new column
    if (n<MAX_LINES)// if n is less than 1024 then
    n++;// write to a next line because m  can support only 1024 chars.
    }
    Matrix[n][m]='\0';// this will terminate the whole string not just one line.
    file.close();
    

    The Matrix Arrays is filled until the file has contents, but less than 1024x1024 chars, u can increase the chars input from the file but i face problems when taking inn more than 4 kb of data into Matrix...

    I hope this helps u, if helps even a bit then +1 vote to my answer pls...