Re: Reading Files and ASCII Code
Dustin Ventin <DustinVentin@discussions.microsoft.com> wrote:
Here's another example of my issues:
I find code like this:
void DisplayFile(fstream & InFile)
{
StringType Line;
InFile.getline(Line, MaxString);
while (! InFile.fail())
{
cout << Line << endl;
InFile.getline(Line, MaxString);
}
}
And everyone acts like this is supposed to be the end-all be-all of text
file reading.
No, they don't. You haven't heard from NEARLY everyone yet, and even those
you have replied don't agree. ;)
...The trouble is, there is no such thing as a StringType
variable. The getline function seems to require a pointer to a variable of
type char to work. So exactly how am I supposed to get this to work, and
what exactly am I going to use to select very specific numbers of characters
from the strings returned?
I know this is probably amazingly frustrating and I'm asking for a lot of
explaination about a very basic subject, but it would really be a great help.
In my opinion, the easiest way to handle your first task is to use a
memory-mapped file. When you do so, you'll end up with a pointer that you
can use as if the file were just memory (which, in fact, it is). You can
use the pointer to navigate arbitrarily through the file.
#include <stdio.h>
#include <atlfile.h>
int main(void)
{
CAtlFile f;
f.Create( "myfile.txt", GENERIC_READ, FILE_SHARE_READ, OPEN_ALWAYS );
CAtlFileMapping<unsigned char> map;
map.MapFile( f );
printf( "File is %d bytes long\n", map.GetMappingSize() );
unsigned char * pj = map;
printf( "The 913th character is %02x\n", pj[914] );
printf( "The 100th character is %02x\n", pj[101] );
printf( "%s\n", pj );
return 1;
}
It isn't STL, but it's
--
- Tim Roberts, timr@probo.com
Providenza & Boekelheide, Inc.