Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

reading from a file

Status
Not open for further replies.

jeffrod

Programmer
Dec 3, 2001
6
US
im using vc++ 6.0 on windows98.
im getting this problem when im reading from a file using ifstream. when reading from some files, eof() turns to one before the end of the file is actually reached. the characters i need to read are all 256 of the ascii values, so at first i thought that maybe one of the characters was an "end of file" character that was at the end of all files. i havent been able to confirm that though because i dont know much about how that stuff works. or maybe the error is something else.
 
To read any file, You can use code like this:
FILE* inifile = fopen("programm.ini", "rb");
long length_file = 0;
if(inifile) {
//File exists and is readable - read it in this buffer
long length_buffer =1024; //
char *buf = (char*)malloc(length_buffer); //Alloc memory
if(buf == NULL) {
//Memory error
fclose(inifile);
goto ERROR;
}
char * buf_wrk = buf;
char *buf_new = buf;
// long fsize = _filelength( inifile); //If You wish

//Read all File
long length_readed;
while(1) {
length_readed = fread( buf_wrk, 1, length_buffer, inifile );
length_file += length_readed;
if(length_readed < length_buffer) {
//All readed
break;
}
//Alloc more Memory
buf_new = (char*)realloc( buf, length_file + length_buffer);
if(buf_new) {
buf = buf_new;
buf_wrk = buf + length_file;
}
else
break; //No memory
}
fclose(inifile);
//use the info in buf
...
//Free Memory
free(buf);
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top