CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    Join Date
    Feb 2011
    Posts
    7

    Need urgent help about array.

    I need c++ code to copy the contain of a file to an array. the file contains four row and the length of the file is 104. how can i copy the contains of the file to an two dimensional array.

  2. #2
    Join Date
    Apr 2008
    Posts
    725

    Re: Need urgent help about array.

    fstream
    vector

  3. #3
    Join Date
    Dec 2008
    Posts
    144

    Re: Need urgent help about array.


  4. #4
    Join Date
    Feb 2011
    Posts
    7

    Re: Need urgent help about array.

    Hi, I do not know the rows and columns of the file. So, How can I copy the contains of a file to an two dimensional array. please let me know.

  5. #5
    Join Date
    Dec 2008
    Posts
    144

    Re: Need urgent help about array.

    Quote Originally Posted by r_drubo View Post
    Hi, I do not know the rows and columns of the file.
    What do you mean?

  6. #6
    Join Date
    Aug 2006
    Location
    Timisoara, Romania
    Posts
    433

    Re: Need urgent help about array.

    Quote Originally Posted by r_drubo View Post
    Hi, I do not know the rows and columns of the file. So, How can I copy the contains of a file to an two dimensional array. please let me know.
    I think you should tell us how the data is stored in the file first. do you store numbers in it?
    like
    Code:
    3 4 2 5
    4 1 2 3
    1 0 2 4
    ?

  7. #7
    Join Date
    Feb 2002
    Posts
    4,640

    Re: Need urgent help about array.

    Quote Originally Posted by r_drubo View Post
    Hi, I do not know the rows and columns of the file. So, How can I copy the contains of a file to an two dimensional array. please let me know.
    If you don't know the rows and columns, then you cannot read the data into a two dimensional array. Unless you arbitrarily pick a number for the columns.

    Viggy

  8. #8
    Join Date
    Jan 2009
    Posts
    1,689

    Re: Need urgent help about array.

    Just a hunch, but are you in the same class as andre.heller? He posted a suspiciously similar question.

    If your file looks like what Feoggou posted, then you can use a CSV loader.

  9. #9
    Join Date
    Feb 2011
    Posts
    7

    Re: Need urgent help about array.

    The data in the file stores like that:

    ABSDEWGRTERTVSDTERYGETTR7U-TERTRET-RET-ERTER-T
    SADGRYREYGRTYREGTRET-TREWRTERTRETRETTR-ERTER-T
    SDFSDFSDFSDFSSFSFSFS-DSFDSFDSFDSFSDFSD-SDFDA-S
    DFSDFDSFSDFDFSDFDSFF-SDFSDFDFSDFDSFDSF-SDFSDF


    I need to copy this contains from the file to a two dimensional array.

  10. #10
    Join Date
    Aug 2006
    Location
    Timisoara, Romania
    Posts
    433

    Re: Need urgent help about array.

    you can use something like this:
    Code:
    fstream f;
    char s[100];
    //after opening it
    fs.get(s, 100, '-');
    this will read maximum 100 characters, but will not take the '-' characters.
    that is, the first call will read
    ABSDEWGRTERTVSDTERYGETTR7U
    and put the read text into the string s.

    by the way, two dimensional array means that each row must have the same number of elements (if '-' is the delimiter, I see only 3 elements in the last line, and 5 elements in the first!)

    how do you expect the resulting two-dimensional array to look like?

  11. #11
    Join Date
    Feb 2011
    Posts
    7

    Re: Need urgent help about array.

    I want to copy the whole contents of the file to a two dimensional array including the '-' character. the contains of the array should be each line of the file in one row.

  12. #12
    Join Date
    Aug 2006
    Location
    Timisoara, Romania
    Posts
    433

    Re: Need urgent help about array.

    that's a one dimensional array (where each element of the array is a line in the text file).

    Code:
    #include <vector>
    #include <fstream>
    using namespace std;
    ....
    int main()
    {
       ...
       fstream f;
       char s[100];
       vector<string> v;
       ...
      //after opening it
      do
       {
          string str;
          fs.getline(s, 100);
          str = s;
          v.push_back(str);
       } while (!f.eof());
    
       //now you have the one dimensional array filled. do what you want with it here:
       ...
       return 0;
    }
    Last edited by Feoggou; February 16th, 2011 at 07:40 PM.

  13. #13
    Join Date
    Feb 2011
    Posts
    7

    Re: Need urgent help about array.

    I want to fill that in two dimensional array.

  14. #14
    Join Date
    Aug 2006
    Location
    Timisoara, Romania
    Posts
    433

    Re: Need urgent help about array.

    Quote Originally Posted by r_drubo View Post
    I want to fill that in two dimensional array.
    again, and how do you expect that two-dimensional array to look like?

    two dimensional arrays look like this:
    (x11 x12 x13 x14)
    (x21 x22 x23 x24)
    (x31 x32 x33 x34)

    where each x is an element of the two-dimensional array. And, as you can see, there is the same number of elements for each row (in this case, 4) and the same number of elements for each column (in this case, 3).

    anyway, I don't know if your data can be put in that which is called "two dimensional array" because your data doesn't have the same number of elements in each row (as, from the data you have written, I understand the strings in capital letters to be the elements of the 'array').
    Last edited by Feoggou; February 16th, 2011 at 09:02 PM.

  15. #15
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,150

    Re: Need urgent help about array.

    [ removed duplicate threads ]

    Do not start multiple threads on the same problem!
    Marc Gregoire - NuonSoft (http://www.nuonsoft.com)
    My Blog
    Wallpaper Cycler 3.5.0.97

    Author of Professional C++, 4th Edition by Wiley/Wrox (includes C++17 features)
    ISBN: 978-1-119-42130-6
    [ http://www.facebook.com/professionalcpp ]

Page 1 of 2 12 LastLast

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured