stream to a 2d array(beginner help)

Discussion in 'C++' started by isaac2004, Oct 16, 2007.

  1. isaac2004

    isaac2004 Guest

    hello, i posted with a topic like this but got no real feedback(prob
    cuz of lapse in my explanation) so i am trying it again. i am trying
    to set up a function that brings in a txt file and adds the file into
    a 2d array. I have this to get the file.

    #include <iostream>
    #include <string>
    #include <fstream>
    using namespace std;

    int main()
    {
    ifstream file;
    string name;

    cout << "File name? ";
    cin >> name;
    file.open(name.c_str());
    if (file.fail()) {
    cout << "Could not open " << name << ".\n";
    return 1;
    }
    char c;
    c = file.get();

    while (!file.fail()) {
    cout << c;
    c = file.get();
    }

    file.close();

    return 0;

    }

    This just grabs and couts the file. What I want to do is grab the file
    and put it into a 2d array. I know it has to be like

    char ** in_array;
    in_array = new *int[height];

    then some looping construct and that is what I am having a problem
    with, Thank you for anyhelp.
     
    isaac2004, Oct 16, 2007
    #1
    1. Advertisements

  2. isaac2004

    Jim Langston Guest

    You never specified what type of data was in the file. Numbers, letters,
    characters, what.

    Also, this sounds a lot like homework. Homework we tend not to give code
    for but pointers when someone has shown some effort.
     
    Jim Langston, Oct 16, 2007
    #2
    1. Advertisements

  3. isaac2004

    Guest Guest

    Is the data in the file somehow arranged as an array or how are you
    supposed to know how large the array should be? The most important thing
    when reading data from a file is to know how it is stored, and you have
    not said a word about the data.

    BTW: When it comes to multi-dimensional arrays it might sometimes be
    easier to simulate them using a normal array that is height*width large
    and calculate the correct index.
     
    Guest, Oct 16, 2007
    #3
  4. You already have the looping construct in your sample code. You might
    want to keep a counter of which iteration of the loop you're on. You
    also need to decide on how you determine which element of the arrays
    you want to put each element into.
     
    Jonathan Lane, Oct 16, 2007
    #4
  5. isaac2004

    isaac2004 Guest

    the problem is a cell automation game of life type problem. the input
    file looks like

    *****
    **** ***
    *** ***

    i know how to put it into an array of char but its the double loop im
    having a problem with
     
    isaac2004, Oct 16, 2007
    #5
  6. isaac2004

    Jim Langston Guest

    Instead of an array of char, I would probably go with a vector of
    std::string. Either way, it's about the same.

    clear the string.
    while not end of file
    read a character.
    if it's end of line
    push the string onto our vector
    clear the string
    else
    add the character to our string

    Now, this is using a vector of strings, but you can also juse a vector of
    vectors
    or a two dimentional char array

    Again, I'm not showing code because I think this is homework.
     
    Jim Langston, Oct 16, 2007
    #6
  7. isaac2004

    isaac2004 Guest

    i understand the algorithm, i just dont know how to setup a two dim
    array. is it just

    char ** in_array;

    for(i= 0; i < height; i++)
    {
    for(j= 0; i < width; j++)
    {
    in array = file.get(i,j);
    }
    }

    or is it something more than that. yes it is homework and i understand
    that providing code isnt cool but its the implementation that i am
    having issues with. thanks for any help
     
    isaac2004, Oct 16, 2007
    #7
  8. isaac2004

    dprody

    Joined:
    Oct 16, 2007
    Messages:
    4
    Likes Received:
    0
    new in side the loop

    once you have the char ** ptr you have to basically treat it like an array of pointers. to arrays. so you declare you inital array of char*, the size of this array is your first demension. then loop through each element in that array and create arrays of chars. you will use new for the array of char * and new for each array of char's.

    Best i can explain without just doing it for you.
     
    dprody, Oct 16, 2007
    #8
  9. isaac2004

    Jim Langston Guest

    char ** in_array; is a pointer to a pointer of char. Now,pointers can be
    used as arrays.
    If we wanted an array of characters, we would set it up as a pointer to a
    char. if we wanted an array of ints, a pointer to an int, and so on. a
    pointer to whatever you want an array of. In this case you want an array of
    an array of chars, so a pointer to a pointer of char. char ** in_array,
    however, is not a two dimentational array.

    A two dimentional array is like
    char foo[10][10];
    A pointer to get to this data is simply
    char* bar;
    and you have to manually calculate the rows and columns.

    what char ** in_array sets up is an array of char pointers.
    each char pointer can point ot memory for a char array. This is differnt
    from a 2 dmentational array in that a 2 dimentional array has it's memory
    continuous. So, basically, you're going ot need to figoure out how many
    lines you are going to need, and set enough memory aside for that many
    pointers.
    Then point each pointer to some memory

    Which is a pain in the behind, which is why it's better to use a vector of
    vector or vector of string. Or even a vector ofy our own class for each
    line.
     
    Jim Langston, Oct 17, 2007
    #9
  10. isaac2004

    Jim Langston Guest

    Incidently,ifyou knew before hand that you had 10 lines each with 10
    characters you could just declare it

    char in_array[10][10];
     
    Jim Langston, Oct 17, 2007
    #10
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.