Home > Blockchain >  Reading a Text file and Storing data in 2D Array in C
Reading a Text file and Storing data in 2D Array in C

Time:10-30

Basically, I'm reading a file and trying to store the data in a 2D, for the differentiation between rows and columns I use the logic below:

    int rows=0,column=0;
    char arr[50][50];
    while(my_file.eof()==0){
        my_file.get(ch);
        if(ch=='\n'){
            rows  ;
        }
        arr[rows][column]=ch;
        column  ;
    }
for(int j=0;j<rows;j  ){
    for(int k=0;k<column;k  ){
    cout<<arr[j][k];}
}

But the when I run It shows the following output: enter image description here

It looks like you are displaying characters beyond the bondary of the strings, or that your strings are not null terminated... Turns out it's both.

Your code:

int rows = 0, column = 0;
char arr[50][50];            // <-- your array is not initialized, while that is not
                             // a big issue, filling the array with zeroes is easy:
                             //  char arr[50][50] = {};

while (my_file.eof() == 0) {
    my_file.get(ch);
    if (ch == '\n') {
        rows  ;             // <-- you pass to the next string, but do not put a 
                            // null character to properly terminate your strings
                            // while this could have been avoided by initializing 
                            // the array, it's best to do it explicitely.

        // replace above line contents by:
        arr[row][column] = '\0';

        if (  row >= 50)     // consider using named constants for the size of your array.
            break;           // No use keeping on reading strings if there is no 
                             // more room to store them
    }

    arr[rows][column] = ch;   // <-- I suspect a bunch un undefined stuff will 
                            // start happening when column >= 50
    column  ;

    // Try replacing above code with:
    if (column < 50)      // consider using named constants for the size of your array.
        arr[rows][column  ] = ch;
}

// make sure the last string is null terminated.
if (row < 50 && column < 50)
   arr[row][column] = '\0';

// note that strings that are 50 bytes long are NOT null terminated.
// that's important to keep in mind, and only workss because we'll print
// byte by byte.

// your original print routine prints out all characters in the array, even 
// stuff that was not in the original file...
for (int j = 0; j < rows;   j){
    for (int k=0 ; k < column;   k){   // <-- you need to check for a null 
                                       // terminating character here...
                                       // also, column is the length of the last 
                                       // string in the array.  This is not a very 
                                       // useful value for displaying any other
                                       // strings, is it?

    // try this:
    for (int k = 0; k < 50 && arr[j][k] != '\0';   k)
        cout << arr[j][k];
    }
    cout << '\n';                      // insert a newline after each string.
}

As you can tell, this is overly complex for doing a very common operation... Here's a more concise way of doing the same thing:

#include <vector>
#include <string>
#include <iostream>
#include <fstream>


int main()
{
    std::vector<std::string> arr;
    std::ifstream ifs("testfile.txt");

    while (ifs && !ifs.eof())
    {
        std::string str;
        std::getline(ifs, str);
        arr.push_back(str);
    }

    for (size_t i = 0; i < arr.size();   i)
        std::cout << arr[i] << '\n';

    return 0;
}

CodePudding user response:

Because you haven't compile the array yet

char arr[50][50];
for (int r = 0; r < 50; r  ){
    for (int c = 0; c < 50; c  ){
        arr[r][c] = ' ';}
}
  • Related