Reading multiple bytes from a file.
I can read a singular byte from a file using:
1 2 3 4 5 6
|
FILE *f = fopen(nfn, "rb");
int n;
if(f)
{
n = fgetc(f);
}
|
But im not quite sure how to loop this so i can get every single byte in a vector :)
How would i do this?
I was trying this:
1 2 3
|
vector<int> n;
while(f)
n.push_back(fgetc(f));
|
[EDIT]
Think i've found a solution, not sure if it's good though:
1 2 3 4 5 6 7 8 9 10 11
|
FILE *f = fopen(nfn, "rb");
vector<int> n;
int thisn;
while(f)
{
thisn=fgetc(f);
if(thisn!=-1)
n.push_back(thisn);
else
break;
}
|
Last edited on
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
std::vector<int> n ;
{ // cstdio
std::FILE* f = std::fopen( "path", "r" ) ;
int temp ;
while( ( temp = std::fgetc(f) ) != EOF ) n.push_back(temp) ;
}
{ // fstream
std::ifstream f( "path" ) ;
char temp ;
while( f.get(temp) ) n.push_back(temp) ;
}
|
Topic archived. No new replies allowed.