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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

reading files

Status
Not open for further replies.

Pyramus

Programmer
Dec 19, 2001
237
GB
I'm trying to read a file using:

Code:
TCHAR buf[1024];
String sFile;

while (_fgetts(buf,1024,f) != NULL)
{
sFile += String(buf);
}

The file I am reading is an encrypted file, (which I have encrypted), so it contains lots of weird ascii characters.

The code above is not reading in the entire file, but stopping before the end. Any ideas why?
 
If you are trying to put your file into a buffer it might be better to read it caracter by caracter.

Here is an example:

#include<stdio.h>
#include<stdlib.h>
#include<iostream.h>
#define _MAXBUFF_ 4096

int main(int argc, char* argv[])
{
FILE *fp;
if(( fp = fopen( &quot;filename&quot;, &quot;r&quot; )) == NULL )
{
fputs(&quot; can't open the file \&quot;filename\&quot;&quot;, stderr);
exit(0);
}
char Buffer[_MAXBUFF_];
char c;
int i = 0;
while(( c = getc( fp )) != EOF && i < _MAXBUFF_ )
{
Buffer = c;
i++;
}
for( int j = 0; j < i; j++ )
cout<<Buffer[j];
fclose(fp);
return 0;
}

In order to make it work you need to repalce &quot;filename&quot; with the name of any valid file.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top