Programming C

Tasks studies - laboratory


Project maintained by dawidolko Hosted on GitHub Pages — Theme by dawidolko

Exam 5

Create a MY_BIKE object that contains the model name (use dynamic memory allocation), the year of production and the price. The object handling (my_bike.h and my_bike.c files) should include saving the MY_BIKE object to a binary file and reading it. In order to avoid saving a pointer to the model name, we create an auxiliary structure MY_BIKE_SERIALIZE, in which we place all the components intended for writing and reading. On the other hand, we place the remaining components (for example, a pointer to a text line) directly in the definition field of the MY_BIKE structure:

struct MY_BIKE
{
MY_BIKE_SERIALIZE data_bike_ser; //Data intended for writing and reading
wchar_t *model_name; //Other data
}; ```

For a text line, we use the wchar_t type and use the functions listed in Table 1.

Table 1 (#include <string.h>) Text line handling functions for char and wchar_t types

char type wchar_t


Constant text line declaration

char str[] = ”row”; wchar_t wstr[] = L”row”;


How many characters does a text line contain?

size_t len ​​= strlen(str); rsize_t wlen = wcslen(wstr);


Copying a text line

#pragma warning (disable : 4996) char *strcpy( char *strDestination, const char *strSource ); wchar_t *wcscpy( wchar_t *strDestination, const wchar_t *strSource ); Output of a text line char text[] = ”abcdef”; printf(“\n%s\n”, text); wchar_t text[] = L”abcdef”; wprintf(L”\n%s\n”, text);


Only `data_bike_ser` participates in write-read. This way we bypass write-read
of components whose values ​​may be invalid after reading. We write and read
data_bike_ser as a whole object (we avoid writing component-by-component). The MY_BIKE object
is created dynamically and does not know how it will be used, so it should anticipate use in
any mode. This means that the deallocator of this object should free all memory,
allocated dynamically.

### In the main function:

1. Create two MY_BIKE objects.

2. Output them to the monitor.

3. Save them to a binary file.

4. Free up memory for these objects.

5. Read data from the binary file. Two MY_BIKE objects should be created.

6. Output them to the monitor.

7. Assign one object to the other.

8. Output them to the monitor.

9. Free up memory