C Language , What are these extra useless zeros ?

Programming languages, Coding, Executables, Package Creation, and Scripting.
Post Reply
Message
Author
User avatar
Hadi_Lovelorn
Posts: 139
Joined: 2022-05-02 23:12
Been thanked: 1 time

C Language , What are these extra useless zeros ?

#1 Post by Hadi_Lovelorn »

This is the code :

Code: Select all

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <gmp.h>

int main(void)
{

FILE * fr, * fw;
char filename[201];
unsigned long int size = 0;
unsigned long int rt = 999999;
unsigned char ch[1] = {112};

printf("Enter filename : ");
fgets(filename, 200, stdin);
filename[strlen (filename) - 1] = '\0';
fr = fopen(filename, "rb+");

if (fr == NULL)
{
    fprintf(stderr, "Error during opening of file %s\n", filename);
    perror("");
    return 1;
}

fseek(fr, 0L, SEEK_END);
size = ftell(fr);
fseek(fr, 0L, SEEK_SET);

if (size == -1)
{

fprintf(stderr, "Size doesn't match\n'");
perror("");
return 1;

}

if ( size > SIZE_MAX)
{

fprintf(stderr, "The size of file is greater than acceptable\n");
perror("");
return 1;

}

unsigned char * buf = NULL;
buf = malloc((size_t)size);
if(buf == NULL)
{

	fprintf(stderr, "Buffer failure\n");
	perror("");
	return 1;

}

int r;
r = fread(buf, size, 1, fr);
printf("%d\n", r);
if (ferror(fr))
{
	fprintf(stderr, "Error during reading from file %s\n", filename);
	perror("");
	fclose(fr);
	return 1;
}
fclose(fr);
fr = NULL;

mpz_t x;
mpz_init(x);
mpz_t y;
mpz_init(y);

mpz_import(x, size, 1, (size_t)1u, 0, 0, buf);

mpz_root (y, x, rt);

int rootnum = 0;
unsigned long int num = rt;

do
{

	num = num/10;
	++rootnum;

}while(num != 0);

memset (buf, 0u, size);

mpz_export(buf, NULL, 1, (size_t)1u, 0, 0, y);

printf("\nEnter copied filename : ");
fgets(filename, 200, stdin);
filename[strlen (filename) - 1] = '\0';

fw = fopen(filename, "wb");
if (fw == NULL) {
	fprintf(stderr, "Error during opening of file %s\n", filename);
	perror("");
	return 1;
}

fwrite(buf, mpz_sizeinbase(y, 10), 1, fw);
if (ferror(fw)) {
	fprintf(stderr, "Error during writing to file %s\n", filename);
	perror("");
	fclose(fw);
	return 1;
}

fclose(fw);

fw = fopen(filename, "ab+");
if(fw == NULL)
printf("File can't be written\n'");
if(ferror(fw))
{

	fprintf(stderr, "Error during writing to file %s\n", filename);
	perror("");
	fclose(fw);
	return 1;
	
}
fwrite(ch, 1, 1, fw);

fclose(fw);

fw = fopen(filename, "ab+");
if(fw == NULL)
printf("File can't be written\n'");
if(ferror(fw))
{

fprintf(stderr, "Error during writing to file %s\n", filename);
	perror("");
	fclose(fw);
	return 1;
	
}
int w;
w = fwrite(&rt, rootnum, 1, fw);
printf("%d\n", w);

fclose(fw);

free(buf);
mpz_clear(x);
mpz_clear(y);

return 0;

}
When You compile it , and execute on 78 MB and 37 MB files , You'll get these :

https://postimg.cc/qt7vnzhm

and

https://postimg.cc/ygM1wjf5

Even rt with value 999,999 isn't true ..... I doubt about the root

Thanks for Your Help

User avatar
blackbird
Posts: 90
Joined: 2023-08-17 04:42
Has thanked: 3 times
Been thanked: 19 times

Re: C Language , What are these extra useless zeros ?

#2 Post by blackbird »

You are working with numbers that are represented as binary data, not as decimal numbers. So if you calculate your sizes with base 10, the sizes will always be too large.
Also it would be an improvement to use the second parameter of mpz_export, countp, to get the size of the number written. There is no need to calculate it.

User avatar
Hadi_Lovelorn
Posts: 139
Joined: 2022-05-02 23:12
Been thanked: 1 time

Re: C Language , What are these extra useless zeros ?

#3 Post by Hadi_Lovelorn »

blackbird

You mean those zeros are part of the root number ? And when You calculate 2^5 as binary and get the result as binary , You can convert it to decimal ....... It's true that they are calculated as binary , but we can convert them to decimal and they must match to each other ........ But thank You ........

User avatar
blackbird
Posts: 90
Joined: 2023-08-17 04:42
Has thanked: 3 times
Been thanked: 19 times

Re: C Language , What are these extra useless zeros ?

#4 Post by blackbird »

But you don't convert it to decimal numbers. You write the file in binary format. You write the result and the root number in binary format but use the size of the corresponding decimal number.
E.G. 255 decimal is 0xFF hexadecimal which has size 3 in decimal and size 2 in hexadecimal and size 1 as byte. If you write 255 in binary format to a file but with size 3 it will be "0xFF 0x00 0x00".

User avatar
Hadi_Lovelorn
Posts: 139
Joined: 2022-05-02 23:12
Been thanked: 1 time

Re: C Language , What are these extra useless zeros ?

#5 Post by Hadi_Lovelorn »

blackbird

I got it , the fwrite , writes numbers as big-endian ....... But size is ordered as rootnum ( the number of digits as root ) ....... Why it makes larger files and fill them with zeros ?

User avatar
blackbird
Posts: 90
Joined: 2023-08-17 04:42
Has thanked: 3 times
Been thanked: 19 times

Re: C Language , What are these extra useless zeros ?

#6 Post by blackbird »

The zeros are written to the file since you write more bytes than the number has:

1) memset (buf, 0u, size); // fills buffer with zeros
2) mpz_export(buf, NULL, 1, (size_t)1u, 0, 0, y); // overwrites the first zeros with the number y
3) fwrite(buf, mpz_sizeinbase(y, 10), 1, fw); // writes the buffer to a file. nr of bytes written is the size of y in decimal representation
4) e.g. if y=255, it's only one byte, but mpz_sizeinbase(y, 10) is 3, so it writes 255, 0, 0 to the files.

If you want to use mpz_sizeinbase to get the length in bytes, you could use base 256.

For your rootnr you use some different way to calculate the size but also with base 10 so it should be similar.

User avatar
Hadi_Lovelorn
Posts: 139
Joined: 2022-05-02 23:12
Been thanked: 1 time

Re: C Language , What are these extra useless zeros ?

#7 Post by Hadi_Lovelorn »

blackbird

I thought the same about a 3 digit number taking 3 bytes after asking my question ..... But You made it totally clear ..... Thank You ........

Post Reply