Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
It's just a practice task , as "Every file is just a binary file with different set of bytes." , i am reading a .doc file as binary file, let suppose
"this .doc file is actually a binary file with 32-bit unsigned numbers and i want to sort them as ascending within the file."
Now so far my logic is to count total bytes from file divide them by sizeof(int) , and make int array of that size. and start reading bytes . but there is a problem in this logic :
file.doc with size 250 bytes where sizeof(int) = 4 , now 250/4 = 62.5 , i made an array of 63 integers , now how will i handle 63rd integer , as 32 bit binary number ?
Do something like rem = filesize % sizeof(int) to get the number of bytes after the last contiguous block of sizeof(int) bytes, and for the last integer in the array do fread(&array[i], rem, 1, stream).
Usually these problems are handled by adding padding. You can pretend that the original file has been filled with zeros (for example) until the next multiple of 4 bytes.
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
for example
string1='abbbc'
string2='agdee'
they have the same character 'a'
so i have to return 1;
else if like
string1'abbb'
string2'cccd'
they don't have any same character
so i have to return 0;
how to boost this searching process?
how to do this rather than using double for loop in C?
As an alternative algorithm:
Have an array of bool, size big enough to hold the highest character (e.g. ascii z is 122) you will have
Loop over the first string converting each char to its numerical and set the bool in that array index to true
Loop over the second string changing each char to numeric and using it to read the bool in that array position
If you encounter a true, return 1
if you reach the end of the array with no trues encountered return 0
This essentially converts the "wase cpu time" of checking every char against every other char (two strings of length 5, 25 comparisons) into a "burn some memory" one (two strings of length 5, up to 10 comparisons, at the expense of holding maybe 122 bools worth of memory), so there's a trade off point that varies with the length of the strings and the size of the charset.. eg two strings of length 5 that could be any Unicode.. it ain't worth burning 65k bools of memory just to save 15 ish comparisons
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
Im getting 10 bytes of data in an char array like which contains hex value
Data1[0] = 0x00,Data1[1] = 0x00,Data1[0] = 0x9 Data1[2]=0x01and so on...
Now I want to get this different array bytes into single long variable . Like
Long_var = 091...
How can do it any method can be accepted.
Sorry, i forgot to mention, i want to do this in 8051 code
There are generally two ways to do type punning in C, both involving arrays.
The first is to use a plain array of 32-bit integers, and then copy the bytes into that array:
char data[12];
// data is initialized...
uint32_t integers[3];
memcpy(integers, data, 12);
printf("First value is 0x%08x\n", integers[0]);
The other way is to use unions:
union type_punning_union
{
uint32_t integers[3];
char data[12];
};
union type_punning_union u;
// Initialize u.data...
printf("First value is 0x%08x\n", u.integers[0]);
Big important note 1: Your byte array have a size mismatch for matching all data evenly to 32-bit integers.
Big important note 2: The code shown above doesn't care about endianness, meaning the results printed might not be exactly what you expect.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
In c ,How much memory consume an array,That is only one single array
ie,int a[0]; or char a[0];
I want to know it when the program writes on a paper ,not at program running on compiler
Here I cant use sizeof function , my compiler is avrgcc ,
In the part of my program some where I require an array of int a[13];only
or Instead of int a[13]; an int a[3]; along with an integer type additionally ie, int i.
specifically I require
if i require 13 integer array or 4 integer array along with an integer variables.
which is less memory used
The size of an array is the sum total of the size of each element in the array.
For example,
if the array size is 5
the array element (type) size is 4 bytes
The whole array would consume (size * sizeof individual element), i.e., in this case 5 * 4 == 20 bytes.
This is irrespective of the usage, i.e., how many elements you actually plan to use.
FWIW, a 0-size/ 0-length array is non-standard. It's a gcc extension for a particular purpose (before the addition of flexible array member as a standard) that supports a 0-sized array, but you better not reply on it.
An int (integer) type variable has a size of 2 bytes, a char has 1 byte. In an array the size of array multiplied by size of variable according to its type will give you the size of the array.
And you can use the sizeof also.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 5 years ago.
Improve this question
I have a large array of bytes called memory and a uint64_t value called valA. I want each byte in valA to be stored in a position in memory (each position in memory holds one byte). The position in memory I'm starting from is 3832.
This is what I've coded so far:
uint64_t valA = 81985529216486895;
memory[3832] = valA;
When I print out each position in memory:
printf("number in memory - %d%d%d%d%d%d%d%d",
memory[valE+0], memory[valE+1], memory[valE+2], memory[valE+3],
memory[valE+4], memory[valE+5], memory[valE+6], memory[valE+7]);
The output is "number in memory- 2390000000". I want the output to be the original number that was stored in valA. Any suggestions?
I want the output to be the original number that was stored in valA
It is not going to work with decimal output, because decimal representation does not break at byte's boundary. You can get it to work with hexadecimal notation, though:
long long unsigned int valA = 81985529216486895LL;
uint8_t memory[5000];
uint32_t valE = 3832;
memcpy(&memory[valE], &valA, sizeof(valA));
printf(
"%02x%02x%02x%02x%02x%02x%02x%02x\n"
, memory[valE+7]
, memory[valE+6]
, memory[valE+5]
, memory[valE+4]
, memory[valE+3]
, memory[valE+2]
, memory[valE+1]
, memory[valE+0]
);
Demo
Note that the bytes are ordered in reverse to match the ordering on the demo system.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Before I go on a mission to write some code, I was hoping someone could confirm the following is possible (or at least fairly simple) to do.
I am wanting to read a file containing numbers that are in 9 columns (separated by a single space) and many rows. The second column is ordered numerically, from largest to smaller. I want to discard all rows that contain below a specified number in this second column and then generate a new file and output just the top rows that conform to this rule.
Additionally, I would like to add a 10th column that is the result of a calculation between 2 other columns.
Is this done using arrays?
Many thanks in advance.
This is trivial in awk. Suppose $N is a shell variable that contains the minimum value you want from the second column, and you want to make column 10 the sum of columns 3 and 5:
awk '$2 > '$N'{ $10 = $3 + $5 }1' input-file
This outputs all of the rows. Pipe the output to head to reduce the number of lines output, or add a counter in the awk script. If you write C code to do this, you are wasting your time unless it is an exercise to help learn C.
On the other hand, it's pretty straightforward in C. For simplicity, assume you only have 2 columns:
int c[2];
do {
rc = scanf( "%d %d", c, c + 1 );
if( c[1] > N && rc == 2 )
printf( "%d %d %d", c[0], c[1], c[0] + c[1] );
} while( rc > 0 );
The most strait-forward approach is probably to convert each column within the file into an array, then manipulate it as you describe from there.
Keep in mind, the file stores characters not integers, make sure you allocate your arrays accordingly to what you desire to store.
You may also find this question discussing the conversion of characters to integers useful.