Reading from array into the file - with encoding ascii characters

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • scylla
    New Member
    • Nov 2011
    • 2

    Reading from array into the file - with encoding ascii characters

    Dear members,
    I'm a newbie and I created a simple program, but I have one problem I can't deal with. I should work on Win CE.

    Ok: I want this program to read 'array of arrays' and save encoded data into file.txt. Every 3 bytes from there should be encoded from ASCII (function below).

    The biggest problem is with loop (or some other way to fix it) to read all array (without any skips).

    Now it's like:
    "qwer", "asdf" and it saves:
    1 qwe
    2 asd

    / the end of every line/array is lost /

    it should be:
    1 qwe
    2 ras
    3 df

    I need it to be very simple - when the first array is done and some bytes are left, they are used to complete another 3-byte data with characters from the next array.

    I would be very grateful for your help. I was looking it everywhere but I cannot find suitable answer.


    Code:
    #include <windows.h>
    #include <tchar.h>
    #include <stdio.h>
    #include <iostream>
    #include <string>
    #include <fstream>
    using namespace std;
    
    int encode(const char code[], int byte);
    
    int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, int nCmdShow)
    
    {
    	
    int j, y, n;
    int odl;
    int k=0;
    int w=1;
    
    char data [4][66]={ 
    "00i00i00i00i00k00k00n01101101101101101101100o00m00o00o0130130140",
    "1401201201401501701701701601701701601601501501501401401401401501",
    "501801<01<01?01D01D01D01F01F01L01O01R01T01V01W01X01X01X01Z01Z01Z",
    "01\01b01j02;02`09H09H09Z09Z09_0:90:90:@0:@0:@0:;0:@0:;0:;0:90:90",
    };
    
    ofstream myFile;
    myFile.open ("file.txt");
    
    for (j=0; j<3; ++j)
    {
    for (k=0; k<63; k=k+3)
    {
    
    odl = encode(&data[j][k], 3);
    
    myFile << w;
    myFile << "	";
    myFile << odl;
    myFile << endl;
    w++;
    
    }
    }
    
    myFile.close();
    
    return 3;
    }
    int encode(const char tab[], int byte) 
    {
      int value = 0;
      int i;
    
      for (i = 0; i < byte; ++i) 
      {
        value <<= 6;
        value &= ~0x3f;
        value |= tab[i] - 0x30;
      }
    return value;
    
    
    }
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    I have not closely looked at your code but I do notice that you are doing bit operations on an int and returning the result as an int. Not good. You data is mapped into an int format: 32 bits with negative values in 2's complement. Your value variable inside the function is also an int. The first thing I would do is change both of those to unsigned int. Then re-post and tell me what happened.

    Comment

    • scylla
      New Member
      • Nov 2011
      • 2

      #3
      Dear weaknessforcats ,

      Changing this variables from 'int' to 'unsigned int' doesn't bring anything. The code works perfect as it's written but not as I'd like it to work. I hope you understand.
      It's because the loop 'if' - when the first array is done and some characters are left, they are missed and all starts from another array (without adding previous bytes to complete 3 bytes).

      I have no idea how to fix it :-(

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        Your array is [4][66] yet your loops run <3 and <63. Shouldn't the loops run <3 and <65 ?

        Comment

        Working...