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

The oddest of incidents (streams)

Status
Not open for further replies.

Sherak

Programmer
Sep 1, 2003
83
GB
I have written a file conversion program and it worked fine until I came across a file it would not convert, on investigation it is the oddest of things, I need every 32nd byte of data in each file so I use:

for(int i = 32; i != 0; i--){InputFile.get(TempChar);}

This is fine but with one file, on reading in the bytes from the file it decides to skip 80 Bytes of data, the section of the file in hex is:

44 45 53 43 00 00 00 00 00 00 00 43 CD 01 00 00
1A 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
57 45 49 47 48 54 00 00 00 00 00 4E E7 01 00 00
0B 03 00 00 00 00 00 00 00 00 00 00 00 00 00 00
53 50 45 43 49 41 4C 00 00 00 00 43 F2 01 00 00
01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
53 54 41 4E 44 41 52 44 00 00 00 43 F3 01 00 00
44 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
42 45 46 4F 52 45 00 00 00 00 00 4C 37 02 00 00

After reading the last byte on line 1 '00' it jumps to the first byte on the last line '42'

in ASCII:

DESC.......C....
................
WEIGHT.....N....
................
SPECIAL....C....
................
STANDARD...C....
D...............
BEFORE.....L7...

So to summarise if I use InputFile.get(TempChar); on the last '.' on the first line, then use InputFile.get(TempChar); again it reads the 'B' from the last line and then continues on from there. Completely ignoring the 7 lines of data inbetween.

Any help on this would be greatly appricaited.......
 
As i see it the "for(int i = 32; i != 0; i--){InputFile.get(TempChar);}" is not responsible for the skipping part but i'm a bit confused why you're fetching 32 times from the file to get every 32'nd char, why not position your pointer and do one read?

Well, anyway, what are you doung with InputFile in between the times you're calling the 1-of-32-pick??

I do believe that your problem lies there.

Just a thought: InputFile.get(TempChar).....
is TempChar in the correct format? Should'nt it be InputFile.get(&TempChar)???

What is the declaration on TempChar???

Totte
 
I havent really used streams that much and Im in a bit of a rush with this program I just used an instruction I am familar with, how do I postion the file pointer, can you manually traverse it backwards and forwards in the stream?

With regard to the problem I have solved it, it is a general C++ issue. I was using fstreams and there are certain ASCII chars which cause problems when read in, one of these chars is 0x1A (EOF or SUB). If you look at the first byte on line 2 its is 1A, this was the culprit. The way to avoid it is to use a FILE* instread of a fstream and open the file with "rb"

Any additional info on maniupulating streams would be much appriciated though..

Thanks,
 
If you open the file with a HANDLE (CreateFile(...)) you can use the "SetFilePointer" and "GetFilePointer".

The function exists in streams too as far as i know but i never uses streams and does therefor not know what it's called but i guess it's something with 'Pointer' in it.

Totte
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top