|
This is another good way, and it is used in the "real world".
What makes it not portable is the fact that some compilers will store the bit field with the first data element starting at the LSB while another will place the first data element starting at the MSB.
So you would perhaps need header information to indicate the structure so that your code, no matter what platform it is compiled on, will properly interpret data that you read from a file.
BTW, I would use this method for simplicity before the method I suggested above.
|
|
|
|
|
Thx, this is starting alot of ideas.
Just have a few questions.
As far as i understand, my struct :
struct MyStruct{
int b:3;
}
only allokates 3 bits of memory for this.
I use this command to write ->
if( (m_stream = fopen(m_fileName,"ab")) != NULL )
{
fwrite(dataPtr, sizeof(short), m_dataBlokSize, m_stream);
fclose(m_stream);
}
else
cerr <<"File "<
|
|
|
|
|
You do not have to explicitly 0-pad the structure. It will essentialy contain garbage data or if you initialize the structure like
MyStruct data = {0};
data.b = 7;
data.b = 8; // this will result in a 0 for b because you only have
// 3 bits available
Also, b must be an unsigned int. If you need it to be signed, you will need to cast it to an int and apply sign extention rules to the unsigned value.
|
|
|
|
|
Well you could use a union if you wanted to access the bytes seperately
union stuff {
struct {
short x;
short y;
...
}
struct {
short a :3;
short b :6;
...
}
}
(look this up in a book, it has been a long time since I've used a union so I might have something wrong here)
What I would do though is not worry the individual bytes.
struct myStruct {
short a :3;
short b :6;
}
struct myStruct data;
data.a = ...
fd = open(...);
write(fd,&data,sizeof(data));
...
which writes all the data without you having to worry about how it is stored.
|
|
|
|
|
Thx for all the answers. I've learned alot.
I ended up writing a function that would feed the odd bit sizes to an array of unsigned shorts. Then i could just write them to a file later on, and extract them the reverse way.
it looked like this:
rawData = data to write to file
numberOfBits = the amount of bits from rawdata to be stored.
void ReadWriteFrame::placeBit( unsigned short rawData, unsigned short numberOfBits)
{
unsigned short Data = dataToWrite[dataPtr];
unsigned short dataBit = 0;
unsigned short bitToWrite = 0x0001;
unsigned short bitToWrite2 = 0;
unsigned short bitToRead = 0x0001;
unsigned short bitToRead2 = 0;
while(numberOfBits > 0)
{
bitToWrite2 = bitToWrite << bitPointer;
bitToRead2 = bitToRead << (numberOfBits - 1);
dataBit = rawData & bitToRead2;
if( dataBit > 0 )
dataToWrite[dataPtr] |= bitToWrite2;
numberOfBits--;
bitPointer++;
if(bitPointer > 15)
{
dataPtr++;
bitPointer = 0;
}
}
}
Thanks for the help
|
|
|
|
|
Hello, I'm currently trying to learn c++ and stuck on an exercise from my book. I don't have an answer guide, nor can I find the answer online.
Here is the exercise:
Design a structure called car that holds the following information about an automobile: its make as a string in a character array and the year it was built as an integer. Write a program that asks the user how many cars to catalog. The program then should use new to create a dynamic array of that many car structures. Next, it should prompt the user to input the make (which might consist of more than one word) and year information for each structure. Note that this requires some care, for it alternates reading strings with numeric data (see Chapter 4). Finally, it should display the contents of each structure. A sample run should look something like the following:
How many cars do you wish to catalog? 2
Car #1:
Please enter the make: Hudson Hornet
Please enter the year made: 1952
Car #2:
Please enter the make: Kaiser
Please enter the year made: 1951
Here is your collection:
1952 Hudson Hornet
1951 Kaiser
And here is my code:
#include <iostream>
using namespace std;
struct car
{
char make[30];
int year;
};
int main()
{
int carsToLog, i;
cout << "How many cars do you wish to catalog? ";
cin >> carsToLog;
car * pNum = new car[carsToLog];
for (i = 0;i<carstolog;i++)
{
="" cout="" <<="" endl="" "car="" #"="" i+1="" ":="" "="" endl;
="" "please="" enter="" the="" make:="" ";
="" cin.get(pnum[i]-="">make,29);
cout << endl << "Please enter the year made: ";
cin >> pNum[i]->year;
}
cout << endl << "Here is your collection: " << endl;
for(i=0;i<carstolog;i++)
cout="" <<="" pnum[i]-="">year << " " << pNum[i]->make << endl;
delete[] pNum;
return 0;
}
I am getting the following errors from VS.Net:
error C2819: type 'car' does not have an overloaded member 'operator ->'
error C2227: left of '->make' must point to class/struct/union.
When I take the array index out of the cin lines, it works, but will only access the first index of the dynamic array..
Thanks for the help in advance!
|
|
|
|
|
You have a pointer named pNum. That can be used with the '->' operator, and also the indexing operator '[]'.
Either of pNum->make or pNum[0].make refer to the same object.
When you use the index operation, you don't have a pointer any more, you have an indexed instance of the thing the pointer points to.
So you can say:
pNum[i].make
or even
&pNum[i]->make
but the latter is just perverse
Hope this helps, and welcome to the wonderful world of C++
Steve S
Developer for hire
|
|
|
|
|
Steve S wrote:
&pNum[i]->make
but the latter is just perverse
or (pNum + i)->make ?!
TOXCCT >>> GEII power
|
|
|
|
|
This is a student project, not the obfiscated C++ coding contest. Any student who turns in either of those two perverse options had better have a good explination as to why they did that, and not the simple pNum[i].make! Otherwise they will lose points.
I'm sure there is a good reason to use either of those forms. I can't think of one offhand though.
|
|
|
|
|
yes, of course, but we're also here to share knowledges...
TOXCCT >>> GEII power
|
|
|
|
|
Sick people . . . teaching pointer arithmetic to beginners.
The question "Do computers think?" is the same as "Can submarines swim?"
|
|
|
|
|
|
Hi
I am working in a program that needs to store some data in a database, but this database must be encrypted. Using Access mdb files is not an option, because programs like "Access Password" find file's password in no time.
So I need a database system that can read files from memory buffer and make queries on its data. Any hint about this problem?
Thank you
|
|
|
|
|
How much data is involved, and why does it have to be a database?
Could you not use a simple binary file, and use a strong encryption technique yourself?
As I understand it, encrypting an Access database causes each 'page' to be encrypted using a block cipher (RC4?), but the same key is used for each page. I'd be happy to be corrected if I'm wrong.
Steve S
Developer for hire
|
|
|
|
|
Hi, thanks for answering
Yes, this is the very same that I told my boss: "Why not use encrypted XML files?"
He wants to use SQL queries, because data requeriments could grow sometime in future
All I need is a library that can get the database from a memory buffer and execute SQL queries on database's data.
|
|
|
|
|
You need a database that supports encryption of stored data.
You *could* use SQLite, and modify the pager functions so that they encrypt before writing and decrypt after reading, which would give you roughly what you want. Is the app going to have 'exclusive' access to the database?
Steve S
Developer for hire
|
|
|
|
|
Hi again
I don't know why SQLite works. I need that database file would not be exposed ever. Does your solution contemplate this feature? Here in my job I heard suggestions like "encrypt mdb file, and to load it desencrypt to a temporal file and load from this file... and later delete it" Not a viable solution, isn't it?
Well, I'll look more on SQLite. Thanks again
|
|
|
|
|
If you change the sections of SQLite that do disk I/O so that they encrypt before writing and decrypt after reading, the db isn't exposed except via that version of SQLite. It's similar to what you've had suggested, but the functionality is built into the database itself.
Depending on the encryption method, it could slow things down a bit, but it should be more secure.
Using temporary files is definitely not a viable solution for security
Steve S
Developer for hire
|
|
|
|
|
Before we start, have you read "Applied Cryptography" By Bruce Schneier? If not you and your boss need to stop everything and start reading. Cryptography is very subtile, there are many places to make a mistake that allows an attacker to get your data, without you knowing! Check out his other books too.
Did you try google? I quickly found this link to a company that claims to do what you want. (maybe, I don't know your requirement)
http://www.netlib.com/
My favorite databse, postgresql, has some encryption modules. Access is a toy database, though it might work I would not reccomend it for anything important.
http://www.postgresql.org/
My google search also found links to many other databases, and I didn't spend much time on it.
|
|
|
|
|
Gammenon wrote:
...but this database must be encrypted.
Why?
Gammenon wrote:
Using Access mdb files is not an option, because programs like "Access Password" find file's password in no time.
Who will have access to the database outside of your application?
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
|
|
|
|
|
Hi!
Database files must be encrypted, because we don't want that anybody can see what is inside. And these files will be visible, perhaps in the application's folder.
|
|
|
|
|
Gammenon wrote:
Database files must be encrypted, because we don't want that anybody can see what is inside.
I know what encryption is for. I was trying to ascertain your particular need. You can protect the file at the OS level, guaranteeing that unauthorized users/groups cannot access the file. On top of that, both Microsoft SQL and Access have internal security. I'm not seeing a need to complicate matters with encryption.
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
|
|
|
|
|
I've been trying to create an BMP file, with information generated by Pixel Font Maker[^]
Now this program generated a file with 128 char arrays, where each array is a charactor in the fontset.
What i would love to be able to do, is convert this file into a bitmap of all those fonts, and for this i'll need a bit of help. Like where to begin, if there are classes i can use that has the 'saving' feature buildin and so forth.
I use MFC and VC++6.
Here is a sample of the output of the letter 'A' from pixel font maker
const unsigned char uc2_0041[] = {<br />
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x1c,0x00,0x0c,0x00,0x12,0x00,0x12,0x00,0x12,0x00,0x3f,0x00,0x21,0x00,0x40,0x80,0xe1,0xc0,0x00,0x00,0x00,0x00}
The programs syntax is explained here[^]
Hoping for some help..
PS: I've totally new to this kind of programming, so please don't assume i know anything specific.. Thanks again.
|
|
|
|
|
I think you can use fwrite to write the color of each pixel.
Search some on the structure of a bitmap.
there are two parts:
- a heading containing some info (don't know which by heart)
- RGB of each pixel (but bakwards written so BGR!!)
that's about it what I remember of it, but maybe it will get you started.
good luck.
"If I don't see you in this world, I'll see you in the next one... and don't be late." ~ Jimi Hendrix
|
|
|
|
|
hi,
anyone know how to define a script variable in Visual Studio for path setting in Project Properties page?
i can see library path setting like this:
$(PUBLIC_PATH)\debug\userapi.lib
how can i define such $(PUBLIC_PATH) by myself?
any help?
thanks,
jim
|
|
|
|
|