Stream and Reader help

M

mag

Let's start off by saying I'm a noob to .Net but here's my problem.


I have many files, all with the same structure.
I know the structure, It was written in some version of C++.
Because it seem to be a null terminated or serialized file
structure.....
using text readers reads only until the first null and thinks it has
reached the EOF.
Using the BinaryReader and the BaseStream.Position I know where I'm at
in the file. However, most of the file is numeric and presenting no
dificulties, but one field is text and I cannot get it to display in a
Datagrid other than numeric. The numeric values of the text field do
not corrolate to ansi, ascii, or anything else I can determine.


Here is a small sampling of the code that wrote the file:


in.open(filename, ios::binary);
if (in.fail())
return false;


in.read((char*)&Version, sizeof(unsigned int));
if (Version != DB_VERSION) { // fixme
in.close();
return false;
}


Id << in;
in.read((char*)&totalX, sizeof(unsigned int));
in.read((char*)&totalY, sizeof(unsigned int));
in.read((char*)&totalZ, sizeof(unsigned int));
in.read((char*)&total1, sizeof(unsigned int));


through position 580 then the text part is added


const bool Namespace::Write(const char* path, const char* name)
{
const unsigned int currentVersion = DB_VERSION;
ofstream out;
ifstream in;
char filename[MAX_PATH + MAX_ID_STRING+1];
int i, j, n;
char namesUsed[3][MAX_NAME+1];
unsigned int nameCount[3];
bool done, newFile;
int mostIdx[3] = {-1,-1,-1};
unsigned int topNum;
int topIdx;
unsigned int Version;


n = (unsigned)strlen(path);
if (n > MAX_PATH)
n = MAX_PATH;


strncpy(filename, path, n);
steamId.ToStr(filename + n);
strcat(filename, ".some extension");


// replace ':' with '-' in filename
for (i=n, n=strlen(filename); i < n; i++) {
if (filename == ':')
filename = '-';
}


// Read file and store the namesUsed and nameCount variables
in.open(filename, ios::binary);
newFile = (in.fail() || in.peek() == EOF);


for(i=0; i<3; i++) {
namesUsed[0] = 0;
nameCount = 0;
}


Any Help would be Greatly appreciated. I have been working on this
issue for two weeks, using many google searches and samples.
Thanks
 
S

Scott Allen

It's been quite some time since I've done C++, but I dont see where
the ofstream is used at all. There are too many gaps in the code to
make sense of it all.
 
M

mag

Thanks Scott for the reply,
The Out.write is exactly as the in.read.
Looking at the file with notepad, it's just a bunch of garbage
characters then a text string (human readable). That human readable
part is at postion in basestream of 584. 584 = number of integer
fields times size of integer (4) or position 584/4=148 -1 for zero
base. It's at that piont I cannont get anything to read the ascii
characters.
Using the binary reader gets me past all the null terminated field
serparators, but a text or byte or char read gets stopped at the first
null piont thinking it's at the end of the file.
Hence my problem.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top