helps files in C

0

I need to create a program that writes a file of bytes , with the name that is entered, with a number and ask if that number is Mb or Gb and I already have the following code developed, although I think that is not the best way to do it because the file that is generated must contain "0xAA" which are 01, I really do not know how to write this type of data ...

with the for I have what string I print write "x" times to get to the desired way

for (i=0; i<=((529000)*numero*1000);i++)

instead of printing

fprintf(fichero, "%s","01");

but I would like to know how to print something similar to this:

  memset(*buffer, 0xaa, 1024);

or that is my question is the best way to print the "x" times the string so that the file that is created has the specified size?

    
asked by Edú Arias 17.11.2016 в 22:15
source

1 answer

1

What to write in the file, I do not understand what you want to say, but what if is simple:

In C, a string is a pointer to a string; by doing so

if( nombre1 == "mb" ) { ...

you're actually comparing 2 pointers , which will not match you.

You have to use the strcmp( ) function, which does just that, compare strings. It would be something like

if( ! strcmp( nombre1, "mb" ) )

or

if( strcmp( nombre1, "mb" ) == 0 )

strcmp( ) returns 0 if the strings are the same.

EDITO

I did not notice. The above will give you failure. You have done

char nombre1[2];

In C, strings are terminated with a byte 0 (which you do not see). Then, you need nombre1[3]; scanf () 'add the 0 automatically to the end of the string.

EDIT 2

To print, go well. You need to tune:

char Buffer[1001]; // Fíjate en que dejo espacio para el 0 final.
Buffer[1000] = 0; // Ponemos nosotros el 0. Los índices empiezan en 0 también.

memset( Buffer, 0xAA, 1000 ); // Descontamos el espacio extra para el 0.
// NO *Buffer. Solo Buffer.

fprintf( fichero, "%s", Buffer );

With fprintf( ... "%s" ... ) you indicate that you are going to print a text string ending in 0 . 0xAA no is zero, so we put a 0 at the end, and send it to print.

You can print the same buffer as many times as you want.

    
answered by 17.11.2016 / 22:24
source