![]() |
#1 | ||
Join Date: Oct 2004
Location: Opole, Poland
Posts: 14,276
|
![]() Next assignment list will contain tasks for file input and output. Since the list isn't online yet, I started practicing with something else entirely.
Code:
#include <iostream> #include <fstream> #include <string> #include <math.h> #include <iomanip> using namespace std; ifstream::pos_type size; char * cache; unsigned int readint(unsigned int o=0) { unsigned int out=0; for (int i=0; i<=3; i++) { out+=cache[i+o]*int(pow(256, i)); } return out; } main () { ifstream infile ("in.scb", ios::in|ios::binary|ios::ate); if (infile.is_open()) { size = infile.tellg(); string brk; brk=0x0D0A; cache = new char [size]; infile.seekg (0, ios::beg); infile.read (cache, size); infile.close(); cout << "SCB File read to memory:\n"; unsigned int version=readint(0); cout << "SCB version " << setw(4) << setfill('0') << hex << version << "\n"; unsigned int lines=readint(4); cout << setw(4) << setfill('0') << hex << lines << " lines\n"; unsigned int bytes=readint(8); cout << setw(4) << setfill('0') << hex << bytes << " bytes\n"; for (int i=0; i<12; i++) cout << setw(2) << setfill('0') << hex << int(char(cache[i])) << ""; /*ofstream outfile ("out.txt", ios::out|ios::app|ios::binary); if (outfile.is_open()) { cout << "\nSCB file is being transcribed. Contents following:\n\n"; for (int i=0xC; i<bytes+0xC-1; i++) { if (cache[i]==0) { outfile << brk; cout << brk; } else { outfile << cache[i]; cout << cache[i]; } } outfile.close(); } else cout << "Unable to open output file!\n"; cout << "\n\n";*/ delete[] cache; } else cout << "Unable to open input file\n"; cout << "\n"; system("PAUSE"); } In practice, it's not: something is messing with the input, as can be evidenced if you try running it with the SCB file from the attached archive. It should report version 1, 0954 lines, cd43 bytes, then follow with 01 00 00 00 54 09 00 00 43 CD 00 00 (a hexdump of first twelve bytes from the file). For some reason it's inserting garbage into the tenth byte. Obviously, this means the program will not work as intended. What is the cause of this behavior and how can I fix it? |
||
![]() ![]() |
|
![]() |
#2 | ||
Join Date: Oct 2004
Location: Opole, Poland
Posts: 14,276
|
![]() Solved. I didn't know the char type existed in signed and unsigned forms.
Knowing that, I only needed to add a function converting signed char to unsigned char. Code:
#include <iostream> #include <fstream> #include <string> #include <math.h> #include <iomanip> using namespace std; ifstream::pos_type size; char * cache; unsigned char unsign(char in) { if (in>=0) return in; else return in+256; } int readint(unsigned __int64 o=0) { unsigned int out=0; for (int i=0; i<=3; i++) { out+=unsign(cache[i+o])*pow(256, i);} return out; } main () { ifstream infile ("in.scb", ios::in|ios::binary|ios::ate); if (infile.is_open()) { size = infile.tellg(); string brk; brk+=char(0x0D); brk+=char(0x0A); cache = new char [size]; infile.seekg (0, ios::beg); infile.read (cache, size); infile.close(); cout << "SCB File read to memory:\n"; unsigned int version=readint(0), lines=readint(4), bytes=readint(8); cout << setw(4) << setfill('0') << hex << "SCB version " << version << "\n" << lines << " lines\n" << bytes << " bytes\n"; ofstream outfile ("out.txt", ios::out|ios::trunc|ios::binary); if (outfile.is_open()) { for (int i=0xC, l=0; i<bytes+0xC && l<lines; i++) { if (cache[i]==0) { outfile << brk; l++;} else { outfile << cache[i];} } outfile.close(); cout << "Transcription complete!\n"; } else cout << "Unable to open output file!\n"; delete[] cache; } else cout << "Unable to open input file\n"; cout << "\n"; system("PAUSE"); } |
||
![]() ![]() |
|
![]() |
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
.ISO file | sgtboat | Troubleshooting | 3 | 15-10-2008 10:15 AM |
Need Help For A Zip File | Cold | Tech Corner | 11 | 19-09-2007 12:59 PM |
Need Help With Jar File | Ioncannon | Programming | 4 | 22-03-2007 05:25 AM |
Bin But No Cue File? | Guest_matt | Troubleshooting | 8 | 09-08-2005 08:05 PM |
Pif File | Guest | Troubleshooting | 8 | 28-04-2005 09:08 PM |
|
|
||
  |