reading into array/2d array from mixed text file..
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11

Thread: reading into array/2d array from mixed text file..

  1. #1
    Join Date
    Jan 2014
    Posts
    12

    reading into array/2d array from mixed text file..

    I know to read a strings into array and tables.. what is they are mixed up?? strings are just names ( 3 characters) and there are bunch of table.. the max size was set to 60

    ex. text file

    JES
    DAN
    JEN
    .
    .
    .
    01010101
    10010101
    RAM
    JET
    01010010
    10100101
    .... and so on

    should i use a while loop?

  2. #2
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,381

    Re: reading into array/2d array from mixed text file..

    Do you want the names to be read into one array and the binary numbers into another - or do you want them all read into just the one array? It would be better to use a vector rather than a fixed sized array. If you just want to read the data into a vector then something like this would do

    Code:
    open file and test for error
    while (data extraction is good) {
         add data to vector
    }
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  3. #3
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,097

    Re: reading into array/2d array from mixed text file..

    Pretend you're us, with no idea what you're trying to do. Read your post and see if it makes sense. Kudos to 2kaud for taking a shot at it, but I have no idea what you're asking.

  4. #4
    Join Date
    Jan 2014
    Posts
    12

    Re: reading into array/2d array from mixed text file..

    Quote Originally Posted by 2kaud View Post
    Do you want the names to be read into one array and the binary numbers into another - or do you want them all read into just the one array? It would be better to use a vector rather than a fixed sized array. If you just want to read the data into a vector then something like this would do

    Code:
    open file and test for error
    while (data extraction is good) {
         add data to vector
    }
    yeah there are 2 different arrays.. string names[60] and table[60][60].. but they are mixed up.. how do i make sure.. names get read into string and numbers into table??

    Code:
    for (int i = 0; i < size; i++)
            {
            
                myfile >> students[size];
                myfile >> table[size][size];
                
            }
    this isnt working

  5. #5
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,381

    Re: reading into array/2d array from mixed text file..

    This will always extract the data into the array one element past its end!

    Is there a pattern to how the names and numbers appear in the file? Or is the arrangemen of names/nunbers somewhat random? If there is a known pattern, then the extraction code can make use of this. If there's not then you will need to read each item from the file as a string and then determine whether it is a name or a number and assign to the appropriate array element.
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  6. #6
    Join Date
    Jan 2014
    Posts
    12

    Re: reading into array/2d array from mixed text file..

    the names are only 3 letters, but the teacher said array should be able to hold up to 60 names.. she gave us a new file.. so now the file has 15 names and table is also 15 x 15 but the table should also be able to go 60 x 60.

    file.txt

    {15 names}
    JAN
    DAN
    .
    .
    .
    { 15 X 15 } tabele

    101010101010101
    101010101010101
    101010101010101
    ...
    ...

  7. #7
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,381

    Re: reading into array/2d array from mixed text file..

    and your c++ problem is? Design wise, it's a simple matter of reading first the names and then the digits and storing them in the array.

    See http://forums.codeguru.com/showthrea...ork-assignment
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  8. #8
    Join Date
    Jan 2014
    Posts
    12

    Re: reading into array/2d array from mixed text file..

    Quote Originally Posted by 2kaud View Post
    and your c++ problem is? Design wise, it's a simple matter of reading first the names and then the digits and storing them in the array.

    See http://forums.codeguru.com/showthrea...ork-assignment
    my hw is wayyy different.. im just trying to learn how to read more than one different data types from a file.. until now we only learned reading int..

  9. #9
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,381

    Re: reading into array/2d array from mixed text file..

    To read different of kinds of data types from a file, there are basically two ways. If it is a text file and you know the format of the data then you just extract the data using stream extraction to variables of the required type. eg

    Code:
    int i;
    string s;
    
      infile >> i >> s;
    assuming the file contains
    Code:
    123
    abc
    then i will contain 123 and s will contain abc.

    If you don't know in advance the format of the data in the file, then you need to extract all data as a type string and then check the format of the data read (eg is the first char of the string a letter or a digit etc) to find out what type it should be and do an appropriate conversion.
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  10. #10
    Join Date
    Jan 2014
    Posts
    12

    Re: reading into array/2d array from mixed text file..

    Quote Originally Posted by 2kaud View Post
    To read different of kinds of data types from a file, there are basically two ways. If it is a text file and you know the format of the data then you just extract the data using stream extraction to variables of the required type. eg

    Code:
    int i;
    string s;
    
      infile >> i >> s;

    assuming the file contains
    Code:
    123
    abc
    then i will contain 123 and s will contain abc.

    If you don't know in advance the format of the data in the file, then you need to extract all data as a type string and then check the format of the data read (eg is the first char of the string a letter or a digit etc) to find out what type it should be and do an appropriate conversion.
    Right now all the data is stored as string.. How do I extract? Because 1's and 0's are also stored as lines in string using get line... Whereas they should be stored in 2D array table. Thank you

  11. #11
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,381

    Re: reading into array/2d array from mixed text file..

    So if the first char of the string read is a '1' or a '0' then you have a binary number. Simply iterate through the string and place each char in the required position in the array. Each row will hold a number with the columns holding the individual elements. So the row number will increase for each string read and the col number will start at 0 and increase for each element of the string.
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

Tags for this Thread

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center