read a file and write it in array

I've a file ashii with a sequence of 65536 zeros and ones (that should represent a binary image). I have to put it in a int[256][256] array so i will be able to analyse it with morphological filters. Unluckyly at my university nobody explained us how to read a file, so i just googled for the solution. Now, every time i try to image=fopen(filename,r) and i "cout<<image;" as result I get only a (very short) alphanumeric sequence.
I tried the command (int)image; but it just transforn the aplhanumeric sequence in a numeric sequence.. So, no way to read a "0" or "1" from a file containing only zeros and ones. I need that shortly for my thesis (image analysis).
use ios.get(); to get individual characters or numbers.
i googles "ios.get()" and "c++ ios.get()" and i received no answers. Could you please be more specific?
I'm a mathematic students, i've a decent understanding about logic and algoritm but not so much knowledge.
ya sure.

here's a simple example of how to read a file char by char, and store them in a vector.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <fstream>
#include <vector>

using namespace std;

int main(){

vector<char> fromFile; // to store all the chars from the file.

fstream file;
file.open("myFile.txt",ios::in);

char buf = file.get(); // get a single char from the file, and store it in buf

while(!file.eof){ // loop until we hit the end of file
  fromFile.push_back(buf);  // add buf to our vector (dynamic array basically)
  buf = file.get(); //get the next letter from the file
}

//you now have the whole file, stored char by char in fromFile.

}
Last edited on
Topic archived. No new replies allowed.