C++ :: Pointer Of Union And Fwrite Operation

Jul 5, 2013

I have union of pointer.

union {
short *two_int;
int *four_int;
double *eight_real;
char *one_ascii;
// void *v;
};

We have write function which write into file.

fwrite (r.one_ascii, 1, i, outstr);

I found one thing,When we write function, we fill only four int in following way.

r.four_int[0] = x + xoff;
r.four_int[1] = y + yoff;

So my question,we fill four_int but write one_ascii only.As is it union of pointer. So it does not matter. I am using 64bit machine and do not have any issue in 32 bit machine.

For more information: [URL] ....

View 7 Replies


ADVERTISEMENT

C++ :: Using Pointer In Union And Assigning Value

Jul 2, 2013

In the current code,We are using pointer of union and assigning value.

class sample {
union {
short *two_int;
int *four_int;
double *eight_real;
char *one_ascii;
// void *v;
}; }

Than we assign value in following way.

sample.four_int[0] = (x + xoff); ( x and xoff and y and yoff all are integer)
sample.four_int[1] = (y + yoff);

Than we write data into file. it was working fine into 32 bit machine but it is not working 64bit machine. When I compare data and found that data is divided by 4. For Ex The File generating from 32 bit machine contain 80 than 64 bit . File contain 20.

View 7 Replies View Related

C/C++ :: Multiplication And Division Operation Not Allowed With Pointer Arithmetic?

Jul 28, 2013

C++ only allow addition and subtraction operation with pointer .why multiplication and division is not allowed? Then how to perform multiplication and division with pointer

View 3 Replies View Related

C :: Using Fwrite With Text Output

Mar 9, 2013

I have the following code:

Code:
void Plot(int nx, int ny, double x[350], double y[350], double C_new[350][350], int iplot){
int i, j;
char fname[50];
FILE *fp;

[Code]....

I am then taking the generated file and importing it into a plotting program (Techplot360). It would speed up the importing process if I created a binary file instead of a like above. I know that I need to use fwrite but I am unsure how to handle lines like

Code:
fprintf(fp, "TITLE= ABCDEFG_%d
", iplot); and

Code: fprintf(fp, "%f %f %f
", x[i], y[j], T_new[i][j]);

View 6 Replies View Related

C :: Using Fwrite And Fread On Structs

Jul 20, 2014

I'm currently working on a program that writes an array of struct to a file and then read back the data from the file to another array of struct. At the bottom is an image of my result.

My goal is to end up with two identical struct arrays but my program fails to do this. My struct have to members: ID and kind (of animals in this case). I declare my first arraystruct africa[] with "monkey" and "giraffe" with their respectively IDnr: 112 and 555. I stream this data to a file and read read them back to the arraystruct get_animal[]. Simply I want the get_animal[] to be identical with the africa[] when the program is over, but that is not so. According to my result(bottom image) it display:

112, monkey (get_animal[0])
112, monkey (get_animal[1])
meaning that get_animal[0] is identical to africa[0] get_animal[1] is also identical to africa[0]

but why? I want get_animal[1] to be identical with africa[1]. meaning I want the result to look like this:

112, monkey
555, giraffe

I've also made the program to print the parameters of my fwrite/fread calls. Why is the 3rd parameter = 1 meaning that only 1 element will be read/written when my program just read/write 2 elements?

Code:

main(){
FILE *fp;
struct animals
{
int id;
char kind[20];
} africa[1] = {{112,"monkey"}, {555,"giraffe"}};
}

[code]....

View 3 Replies View Related

C++ :: Fwrite - Calculating Elapsed Time

Jan 24, 2013

I have a question. I would like to actually have some measure of roughly how long it takes to do a fwrite to a drive. When I do the following:

clock_t begin = clock();
unsigned long long size_t = fwrite(send, 1, transfer_size*sizeof(unsigned long long), wpFile);
clock_t end = clock();
double long elapsed_secs = double long(end - begin) / CLOCKS_PER_SEC;

Unfortunately, I don't get any different result for different transfer size!!!

My guess is that the clock_t , once it issues a fwrite command, some how stops its measurement, and it comes back again, when I am already done with fwrite. I do get the almost same measure, whether my transfer size is 32KB Byte or 16MB ! Which I was indeed expecting to see a huge difference. I wouldn't really want the exact real timing measure (well off course it will be nice to know); and all I care about is to see some difference in time whether I am doing KB transfer vs MB transfer.

Some rough measurement required of the actual time being elapsed for fwrite function?

View 10 Replies View Related

C++ :: Fwrite Calculating Elapsed Time

Jan 23, 2013

I would like to actually have some measure of roughly how long it takes to do a fwrite to a drive. When I do the following:

clock_t begin = clock();
unsigned long long size_t = fwrite(send, 1, transfer_size*sizeof(unsigned long long), wpFile);
clock_t end = clock();
double long elapsed_secs = double long(end - begin) / CLOCKS_PER_SEC;

Unfortunately, I don't get any different result for different transfer size!!!

My guess is that the clock_t , once it issues a fwrite command, some how stops its measurement, and it comes back again, when I am already done with fwrite. I do get the almost same measure, whether my transfer size is 32KB Byte or 16MB ! Which I was indeed expecting to see a huge difference.

I wouldn't really want the exact real timing measure (well off course it will be nice to know); and all I care about is to see some difference in time whether I am doing KB transfer vs MB transfer. Any other function that will give me some rough measurement of the actual time being elapsed for fwrite function?

View 4 Replies View Related

C :: Weird Characters Popping Up In Fwrite Function

Mar 13, 2013

Code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main() {

[Code]....

this code asks the user to input words/strings until he enters "end."after that, the program must copy the input to a text file named read.txt...I entered 'j' and then 'end' and after that I looked at the read.txt file and here's what's in it.

Output:

Code:
j
; end

some weird characters appeared!! the characters in the text file should only be.

View 6 Replies View Related

C :: Fread / Fwrite And Fseek (reverse Of WAV File)

Jun 28, 2013

I am trying to write a function to reverse a wav file. The idea is to copy the header as it is from the begening of the input.wav file to the beginning of the output.wav file. After that i have to take count number of bytes(count = numberChannels * bitsPerSample in the wav i use this is 2*16= 32 bits, 32/8 = 4 bytes). With this code i am trying to copy the header( that's working fine) and then copy 10 samples from the end and put them to the output.wav file(after header not at the beginning).

This is the content of the input file:

The last 4 bytes of the header are bolded.

Code:
52 49 46 46 24 bd 01 00 57 41 56 45 66 6d 74 20
10 00 00 00 01 00 02 00 44 ac 00 00 10 b1 02 00
04 00 10 00 64 61 74 61 00 bd 01 00 00 10 00 10
ff 00 10 00 00 10 00 10 00 00 ff 10 10 10 00 10
00 00 00 10 10 10 10 00 00 00 10 00 10 00 10 ff
00 10 00 00 10 00 ff 00

This is the content of the output file it suppose to have in my example

Code:
52 49 46 46 24 bd 01 00 57 41 56 45 66 6d 74 20
10 00 00 00 01 00 02 00 44 ac 00 00 10 b1 02 00
04 00 10 00 64 61 74 61 00 bd 01 00 10 00 ff 00
00 10 00 00 10 00 10 ff 00 00 10 00 10 10 10 00
00 00 00 10 10 10 00 10 00 00 ff 10 00 10 00 10
ff 00 10 00

Code:
void reverse(char **array) {
int i=0;
word numberChannels;//word is unsigned short int , byte is unsigned char
word bitsPerSample;

[Code] .....

The problem is that (having in mind per sample is 4 bytes) instead of copying 40 bytes it just copies 20

View 1 Replies View Related

C :: Crypt File With XOR Instruction - Fread / Fwrite Segmentation Fault

Jan 21, 2015

I try to crypt file with XOR instruction, by always receive segmentation fault. What's wrong with it?

Code:
#include <stdio.h>
main() {
FILE *fin,*fout;
char buff[40];
int a=0x11;
int i=0;

[Code] ....

View 4 Replies View Related

C :: Using Structure In Union

May 27, 2013

I'm trying to use a structure in union in the following format:

Code:
union data
{
unsigned char All[10] ;
struct data_pkt
{
unsigned char ack;
unsigned short status;
unsigned short data_length;
unsigned char Data[5];
}format;
}adb; adb.

All has 10 bytes which is equivalent to the structure bytes. ie 6 bytes if unsigned char and 2 short i.e 4 bytes. Thus total 10 bytes is given to adb.All. When I print the struct size I get 12 bytes. This creates problem in obtaining data in union. According to the program:

adb.format.ack should have the address of adb.All[0]
adb.format.status should have the address of adb.All[1]
adb.format.data_length should have the address of adb.All[3]
adb.format.Data[0] should have the address of adb.All[5]

But in actual case this is how memory is allocated:

adb.format.ack assigned to the address of adb.All[0]
adb.format.status assigned to the address of adb.All[2]
adb.format.data_length assigned to the address of adb.All[4]
adb.format.Data[0] assigned to the address of adb.All[6]

Why this is happening? How can I solve this?

View 9 Replies View Related

C++ :: Using Union For Combining Registers

Nov 16, 2013

How I could use unions to combine registers elegantly. For example I have the 8 bit registers B and C & I have opcodes that work on each independent register such as add b, c, which is simple, but then I also have opcodes that work on both of them as if they're one like ld a, bc. I know I could go about that by just masking them together but I've seen it done with unions before & it made everything so much more simple.

View 4 Replies View Related

C++ :: Changes In Union Typedef Not Mirrored

Sep 1, 2013

I have made VGA emulation with registers and memory in my emulator. But for some reason the writes to the union array with CPU data (register 0-8 of the VGA's Graphics Controller Registers, referenced with <GRAPHREGS>.DATA[index]) don't reflect on the union's data.

typedef union __attribute__((packed)) {
byte DATA[9]; //9 registers present!
struct //Contains the registers itself! {
//Set/Reset Register (index 00h)
union {

[code]...

what's going wrong? Have I made an error in the registers?

(In this case I write to register <GRAPHREGS>.DATA[8] (which should be the <GRAPHREGS>.REGISTERS.BITMASKREGISTER)), but the BITMASKREGISTER stays 0, while DATA[8] gets the correct value.

View 4 Replies View Related

C++ :: Assigning Values To Union?

Sep 10, 2012

I have the following C++ code

typedef union UUID {
unsigned char byte[16]; /**< Array of 16 bytes. */
unsigned int ll[2]; /**< Array of two 64-bit words. */
} UUID;

[Code] ......

The compiler complains thus

$ g++ union.cpp
union.cpp: In function "int main()":
union.cpp:15:17: warning: extended initializer lists only available with -std=c++0x or -std=gnu++0x
union.cpp:15:17: warning: extended initializer lists only available with -std=c++0x or -std=gnu++0x
union.cpp:15:17: error: no match for "operator=" in "entry.EntryHeader::uuid = {0, 0, 0, 2}"
union.cpp:1:20: note: candidate is: UUID& UUID:perator=(const UUID&)

How do I go about assigning values to this union in C++.

View 3 Replies View Related

C++ :: Classes Defined With Struct And Union

Mar 12, 2013

Classes defined with struct and union

Classes can be defined not only with keyword class, but also with keywords struct and union.

The concepts of class and data structure are so similar that both keywords (struct and class) can be used in C++ to declare classes (i.e. structs can also have function members in C++, not only data members). The only difference between both is that members of classes declared with the keyword struct have public access by default, while members of classes declared with the keyword class have private access. For all other purposes both keywords are equivalent.

The concept of unions is different from that of classes declared with struct and class, since unions only store one data member at a time, but nevertheless they are also classes and can thus also hold function members. The default access in union classes is public.

The above is a statement taken from a C++ tutorial. So I understand classes a bit better now but the above quote doesnt make too much sense. Is it saying that you can have a class within a class?

View 1 Replies View Related

C :: Under-allocating Memory For A Tagged Union

Mar 6, 2015

What i'm interested in is the behavour of a struct/union constructed like this:

Code:
typedef struct {
uint64_t num1;
uint64_t num2;
} st_a;

typedef struct {
uint64_t num1;
uint32_t num2;

[Code] .....

What kind of behavour could I expect from object, in the following cases:

1. newsomestruct(0)->u.a.num1 = 2;
2. newsomestruct(1)->u.b.num1 = 2;
3. newsomestruct(0)->u.a.num2 = 2;
4. newsomestruct(1)->u.b.num2 = 2;
5. newsomestruct(0)->u.b.num1 = 2;
6. newsomestruct(1)->u.a.num1 = 2;
7. newsomestruct(0)->u.b.num2 = 2;
8. newsomestruct(1)->u.a.num2 = 2;
9. Code:

somestruct* ss1 = newsomestruct(0);
somestruct* ss2 = newsomestruct(1);
* ss1 = * ss2; 10. Code: somestruct* ss1 = newsomestruct(0);
somestruct* ss2 = newsomestruct(1);
* ss2 = * ss1;

This is what I'd expect, but I can't find any evidence online in C standards or elsewhere:

1. Works as expected, sets the value of a.num1 to 2.
2. Works as expected, sets the value of b.num1 to 2.
3. Works as expected, sets the value of a.num2 to 2.
4. Works as expected, sets the value of b.num2 to 2.
5. Works as expected, sets the value of b.num1 to 2.
6. Works as expected, sets the value of a.num1 to 2.
7. Works as expected, sets the value of b.num1 to 2.
8. Crashes/Memory Corruption, attempted to alter memory outside struct.
9. Works as expected, * ss1 == * ss2
10. Crashes/Memory Corruption, attempted to alter memory outside struct.

I've tested simular code on my machine (Xubuntu 14.04LTS compiled with gcc on -O3) and it appears to be reliable, given that you stick with acessing the type tagged in the struct or the common initial union struct members (in this case num1).

View 6 Replies View Related

C++ :: How To Find Out Which Member Of Union Is Currently Set / Active

Jan 16, 2015

how to find out which member of union is currently set/active.

exm:
union myunion{
int i;
float f;
}

if i set myunion ob{7}, it set ob.i=7;

my q? is how to find out i is set f is not set/ not acrive

View 2 Replies View Related

C++ :: How To Program Boolean Union And Intersection

Jun 19, 2014

It involves some discrete mathematics. Any code snippet to get me started?

View 3 Replies View Related

C++ :: Find Intersection / Union And Difference Of Two Sets

Feb 17, 2014

The program is to find intersection,union and difference of two sets. The program take the input correctly but after it crashes with the message that some exe is not working...

Code:
#include<iostream>
using namespace std;
void Input(int *A, int*B, int size1, int size2)
//input function {

[Code] ....

View 3 Replies View Related

C++ :: Union Of Two Vector ADT Bags - Operator Overload

Apr 30, 2014

I'm trying to come up with the union of two Vector ADT bags, so I have to overload the '+' operator, but I'm getting a bunch of error messages saying:

VectorBag.cpp: In instantiation of ‘VectorBag<ItemType> VectorBag<ItemType>::operator+(VectorBag<ItemType>) [with ItemType = int]’:
proj2.cpp:161:42: required from here
VectorBag.cpp:81:24: error: no match for ‘operator[]’ (operand types are ‘VectorBag<int>’ and ‘int’)
newBag.add(anotherBag[i]);
^
Here is the function to overload the operator:

template<class ItemType>
VectorBag<ItemType>
VectorBag<ItemType>::operator+(VectorBag<ItemType> anotherBag) {
VectorBag<ItemType> newBag;
for (int i = 0; i < anotherBag.getCurrentSize(); i++)
newBag.add(anotherBag[i]);
}

The add() function is pre-defined by me somewhere else in the code. It basically does push_back().

View 4 Replies View Related

C# :: T-SQL Multi Table Union To Call Out Data

Jul 15, 2014

I'm trying to union eleven tables to call out data. Parent table is 'Events', child tables are 'SR1Laptimes', 'SR2Laptimes' and so on (there are ten SR... tables). Primary key in all tables is EventName. Parent/Child relationship is Events.EventName/SR1Laptimes.EventName etc All tables that start with SR have the same Schema. I'm trying to call out MIN(Q1) across all table but first need to Union them I believe. Here is my code.

myCommand.CommandText = "SELECT MIN(Q1), MIN(Q2), MIN(Q3), MIN(Q4), MIN(LaptimesMinutes), MIN(LaptimesSeconds) FROM (SELECT * FROM Events UNION ALL SELECT * FROM SR1Laptimes UNION ALL SELECT * FROM SR2Laptimes) WHERE (Events.Track = @track) AND (Events.Number = @number) AND (Events.Rider = @rider)";
myCommand.Parameters.AddWithValue("@track", analysisTrackComboBox.Text);
myCommand.Parameters.AddWithValue("@number", analysisNumberComboBox.Text);
myCommand.Parameters.AddWithValue("@rider", analysisRiderComboBox.Text);

View 4 Replies View Related

C++ :: Malloc For Union Inside Structure Fails

Dec 27, 2012

I have some problem while allocating memory to a union inside structure. Below is the code i am using

ON SYSTEM1:

This works fine

ON SYSTEM2:

compiler complains saying "need structure or union type" while allocating MYSTRUCT1.

If I change:

shreyas[0].UnionAttr.struct1 = (MYSTRUCT1 *) malloc (sizeof(MYSTRUCT1)
to
shreyas[0].UnionAttr->struct1 = (MYSTRUCT1 *) malloc (sizeof(MYSTRUCT1)

This compiler on SYSTEM2 is happy. but second way does not look correct to me and compiler on system 1 complains about it. Which is the correct way to allocate memory?

If first one is correct then what should i look in for to avoid this error? Could this be an issue with compiler on SYSTEM2? If i use second method on SYSTEM2 code segfaults during malloc.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct mystruct1 {
int a;
int b;

[Code] ....

View 3 Replies View Related

C++ :: Splitting RGBA Into Individual Bytes By Creating Union

Jan 16, 2013

I am currently working with win32 API , for image processing, one of the windows function returns RGBA ( colors ) as unsigned int , I then split it into individual bytes by creating a union ,

Code:
union colour {
unsigned int value;
unsigned char RGBA[4];
}

to spit the int into individual bytes, my Question is there a better or my convenient way of doing this.

View 6 Replies View Related

C :: Bitfield Array - Using Standard Technique Of Creating A Structure Within Union

Mar 27, 2013

Any way that one could create a bitfield using the standard technique of creating a structure within a union, as follows:

Code:
typedef union {
struct {
unsigned b0 : 1;
unsigned b1 : 1;
:
:
unsigned b(n-1) : 1;
} bits;
unsigned int value;
}

BIT_FIELD_TYPE; Except, what I'd like to do is to replace all the single-bit elements in the bits structure with a single statement that creates an array of, say, 32 values. The clear advantage of this is that it could be traversed using an iterator, ...

Code:
main() {
BIT_FIELD_TYPE foo;
unsigned int i;
...
for (i = 0; i < n; i++) {
... (print out foo.bits.b[i]) ...
}

So far, I've not figured out a way to do it, either as an array, or using a pointer to iterate through the individual bits.

View 6 Replies View Related

C++ :: Using Getline And IF Operation?

Mar 13, 2015

I am just trying to get a code going for a mock test and to get use to the getline and IF operations, but it seems I have ran into an issue[URL] is a link to the code I have written, and I can use getline to give a value to my variable, but it seems like it gets lost once I try to use the IF function. Am I not using the right variable type?

View 14 Replies View Related

C :: Simple Increment Operation

Jan 27, 2013

Code:

int i=5,j;
j=++i + ++i + ++i;
printf("%d",j); //22
i=5;
j=i++ + i++ + i++;
printf("%d",j); //19 Shall not it give 21 and 18 respectively?????

View 4 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved