Forums

Forums (http://www.abandonia.com/vbullet/index.php)
-   Programming (http://www.abandonia.com/vbullet/forumdisplay.php?f=25)
-   -   Need some help with file i/o (http://www.abandonia.com/vbullet/showthread.php?t=22784)

The Fifth Horseman 01-12-2009 08:58 PM

Need some help with file i/o
 
1 Attachment(s)
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");
}

On paper, it should be working fine.
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?

The Fifth Horseman 02-12-2009 04:46 PM

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");
}



The current time is 12:02 AM (GMT)

Powered by vBulletin® Version 3.7.1
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.