473,513 Members | 2,492 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Reading from array into the file - with encoding ascii characters

2 New Member
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.


Expand|Select|Wrap|Line Numbers
  1. #include <windows.h>
  2. #include <tchar.h>
  3. #include <stdio.h>
  4. #include <iostream>
  5. #include <string>
  6. #include <fstream>
  7. using namespace std;
  8.  
  9. int encode(const char code[], int byte);
  10.  
  11. int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, int nCmdShow)
  12.  
  13. {
  14.  
  15. int j, y, n;
  16. int odl;
  17. int k=0;
  18. int w=1;
  19.  
  20. char data [4][66]={ 
  21. "00i00i00i00i00k00k00n01101101101101101101100o00m00o00o0130130140",
  22. "1401201201401501701701701601701701601601501501501401401401401501",
  23. "501801<01<01?01D01D01D01F01F01L01O01R01T01V01W01X01X01X01Z01Z01Z",
  24. "01\01b01j02;02`09H09H09Z09Z09_0:90:90:@0:@0:@0:;0:@0:;0:;0:90:90",
  25. };
  26.  
  27. ofstream myFile;
  28. myFile.open ("file.txt");
  29.  
  30. for (j=0; j<3; ++j)
  31. {
  32. for (k=0; k<63; k=k+3)
  33. {
  34.  
  35. odl = encode(&data[j][k], 3);
  36.  
  37. myFile << w;
  38. myFile << "    ";
  39. myFile << odl;
  40. myFile << endl;
  41. w++;
  42.  
  43. }
  44. }
  45.  
  46. myFile.close();
  47.  
  48. return 3;
  49. }
  50. int encode(const char tab[], int byte) 
  51. {
  52.   int value = 0;
  53.   int i;
  54.  
  55.   for (i = 0; i < byte; ++i) 
  56.   {
  57.     value <<= 6;
  58.     value &= ~0x3f;
  59.     value |= tab[i] - 0x30;
  60.   }
  61. return value;
  62.  
  63.  
Nov 4 '11 #1
3 2374
weaknessforcats
9,208 Recognized Expert Moderator Expert
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.
Nov 4 '11 #2
scylla
2 New Member
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 :-(
Nov 5 '11 #3
weaknessforcats
9,208 Recognized Expert Moderator Expert
Your array is [4][66] yet your loops run <3 and <63. Shouldn't the loops run <3 and <65 ?
Nov 5 '11 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

3
24483
by: JSM | last post by:
Hi, I am just trying to port an existing simple encryption routine to C#. this routine simply adds/substracts 10 ascii characters to each character in a text file (except quotes). The routine...
6
33836
by: clintp | last post by:
I have a byte array that contains 8-bit ascii characters. I'm not particular about the codepage used to display them, but I have to preserve the position in the string with something and be able...
2
6840
by: melanieab | last post by:
Hi, I'm trying to store all of my data into one file (there're about 140 things to keep track of). I have no problem reading a specific string from the array file, but I wasn't sure how to...
4
2129
by: Kueishiong Tu | last post by:
I have a text file with wide characters. I use the following C++ code to read them in. However the wide characters are not read in properly. What is wrong? String* path = "C:\\Documents and...
2
6679
by: joakim.hove | last post by:
Hello, I am having great problems writing norwegian characters æøå to file from a python application. My (simplified) scenario is as follows: 1. I have a web form where the user can enter his...
11
3557
by: Freddy Coal | last post by:
Hi, I'm trying to read a binary file of 2411 Bytes, I would like load all the file in a String. I make this function for make that: '-------------------------- Public Shared Function...
3
5435
by: ddtl | last post by:
Hello everybody, I want to create a script which reads files in a current directory and renames them according to some scheme. The file names are in Russian - sometimes the names encoded as...
2
3914
by: nikosk | last post by:
I just spent a whole day trying to read an xml file and I got stuck with the following error: Exception Type: UnicodeEncodeError Exception Value: 'charmap' codec can't encode characters in...
9
4121
by: =?Utf-8?B?RGFu?= | last post by:
I have the following code section that I thought would strip out all the non-ascii characters from a string after decoding it. Unfortunately the non-ascii characters are still in the string....
13
17545
by: Eps | last post by:
Hi there, I believe all strings in .net are unicode by default, I am looking for a way to remove all non ascii characters from a string (or optionally replace them). There is an article on...
0
7384
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7539
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7101
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5686
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5089
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4746
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3234
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3222
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1596
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.