file name - "lable.c"
copy
int lable(char lname[], FILE *fa, long long *pass, int size)
{
fflush(fa);
FILE *fn;
if ((fn = fopen(lname, "r")) != 0)
{
fclose(fn);
return -1;
}
else if ((fn = fopen(lname, "wb")) == 0)
{
return -2;
}
fseek(fa, 0, SEEK_END);
fwrite(&size, sizeof(size), 1, fa);
fwrite(lname, sizeof(char) * size, 1, fa);
if (ftell(fa) != sizeof(long long) + sizeof(int) + sizeof(size) + (sizeof(char) * size))
{
char temp_name[size];
strcpy(temp_name, " ");
fseek(fa, sizeof(long long) + sizeof(int), SEEK_SET);
fread(&size, sizeof(size), 1, fa);
char read_name[size];
fread(read_name, sizeof(read_name), 1, fa);
FILE *ftemp;
if ((ftemp = fopen(read_name, "rb")) == 0)
{
fclose(fn);
remove(lname);
return -3;
}
fseek(ftemp, 0, SEEK_END);
int temp1 = (ftell(ftemp) / size);
fclose(ftemp);
rewind(fn);
while (temp1 != 0)
{
fwrite(temp_name, sizeof(temp_name), 1, fn);
temp1--;
}
fclose(fn);
}
return 0;
}
The lable() function is an important part of DBMS written in C language. In this article, we will discuss this function in detail, including its purpose, code, and usage.
The purpose of the lable() function is to create a file and write some data into it. Let's look at the code of this function and understand it line by line. The first line of the function is "int lable(char lname[], FILE *fa, long long *pass, int size)". This line declares the function lable() and takes four arguments. The arguments are: lname[]: A character array that contains the name of the file to be created *fa: A pointer to the FILE object that contains the data to be written into the file *pass: A pointer to a long long variable that stores the password size: An integer variable that stores the size of the data to be written into the file The next line is "fflush(fa);". This line flushes the buffer of the file pointed to by *fa. The next block of code checks if the file with the name specified in lname[] already exists. If it does, the function returns -1, indicating an error. If the file does not exist, the function creates a new file with the name specified in lname[]. If the file cannot be created, the function returns -2, indicating an error. After the file is created, the function writes the size of the data to be written into the file using the "fwrite()" function. It then writes the name of the file to be created into the file using the same function. The next block of code checks if the data has been written correctly into the file. If the data has not been written correctly, the function reads the name of the file from the data in the file and checks if the file with that name exists. If the file does not exist, the function returns -3, indicating an error. If the file exists, the function calculates the number of records that can be written into the file based on the size of the data. It then writes records with empty data into the file until the number of records is equal to the calculated value. Finally, the function closes the file and returns 0, indicating that the function has executed successfully. In conclusion, the lable() function in DBMS using C language is a useful function for creating a file and writing data into it. It can be used in various applications where file handling is required. This article has provided a detailed explanation of the function, along with its code and usage.
Click on the next button for source code of next file