Hello, help write with the help of streams a file into an array ... Something I can't do ... Here is an example code:

ifstream stream; stream.open("67.txt",ios::in); int s=0; stream >> f[i][j]; for(i=0; i<2;i++) for(j=0; j<8;j++) { if(t[i][j]==1) s=1; } 

    2 answers 2

    For example, so (if zeros and ones are separated by spaces; if not, say, I will write)

     ifstream fs ("file"); if (! fs.is_open()) return 1; int aa [2][8], n = 0; while (n < 2*8 && fs >> *((int*) aa + n++)); 

    PS In connection with the clarification of conditions, I propose such a code

     ifstream fs ("file"); if (! fs.is_open()) {cerr << "Bad file\n"; return 1;} int a[2][8]; size_t n = 0; char c; while (n < 2*8 && fs.read (&c, 1) ) { if (c == '0') ((int*) a)[n++] = 0; else if ( c == '1') ((int*) a)[n++] = 1; } 
    • Please explain the lines: int aa [2] [8], n = 0; while (n <2 * 8 && fs >> ((int ) aa + n ++)); - Alerr
    • Something does not fill the array, and the element [2] [8] is initialized to zero .. - Alerr
    • show what file you have - skegg
    • I have there 101010101111100010101010101 .... something like this - Alerr
    • those. only zeros and ones in one line? - skegg

    Here is the implementation, although it reads a file into a one-dimensional array, but it will not be difficult to convert it into a two-dimensional array =)

     #include <iostream> #include <fstream> using namespace std; const int n = 80; int main() { ifstream f("halo.txt"); int mass[16]; int c = 0; while (!f.eof()) { char s[80]; f.getline(s, n); char *tok = strtok(s, " n"); while (tok) { c++; mass[c] = atoi(tok); tok = strtok(NULL, " n"); } } for (int i = 1; i <= 16; i++) cout << mass[i] << endl; system("Pause"); } 
    • For some reason, your program goes in cycles .. - Alerr