Access the Customized Data out of a Stream:
Here, we take data from the stream and save it in the locations specified by the different parameters, as per the argument specification. The subsequent parameters may relate to the items of the category provided by their appropriate formatting identifier in the format string that has already been created.
In this instance, we’re creating a specific file and retrieving the fruit’s name and color. We have a file called “file.txt” that we will have formed, as shown below:
int main()
{
FILE *fPointer;
char fn[20];
char clr[20];
fPointer = fopen("file.txt", "w+");
if (fPointer == NULL)
{
printf("file not found. \n");
return 0;
}
printf("Fruit name \n");
scanf("%s", fn);
fprintf(fPointer, "Fruit name= %s\n", fn);
printf("Fruit color \n");
scanf("%s", clr);
fprintf(fPointer, "Fruit color= %s\n", clr);
fclose(fPointer);
}
We have to integrate the header file #include <stdio.h> despite working with fscanf() function. If we don’t include this header file, we get the error. Now, we initialize the main() method. We can start the programming inside the body of this function.
First, we create a constructor “fpointer” for the file. Further, we declare two variables, “fn” and “clr”, having data type characters. We utilize the fopen() function to open the defined file. Next, we apply the if condition. If the defined file is not present in the system, the printf() function prints “file not found”.
In addition to this, we applied different functions for different purposes. We employ scanf() function. This function reads the name of the fruit from the specified file. Then, the fprintf() function is used to print the “Fruit name” and stored in the variable “fn”. Similarly, we again utilized the scanf() method to get the color of the fruit from the file. This fprintf() function is also applied to print the “Fruit color”. Now, the value of color is stored in its variable “clr”. In the end, we declare the fclose() function to close the defined file.
Convert Hexadecimal Number to Decimal Number:
The fscanf() method provides the number of accurately matched and allocated input elements. Transitions that were done but perhaps not delegated are never included in the resulting value. A hexadecimal integer is used as the input for a %x transformation identifier, as provided below:
After introducing the header file <stdio.h>, we define the main() function. In the next step, we initialize a variable named “num”, which has a data type integer inside the body of the main() function. The printf() function is applied to get any hexadecimal number from the user. Then, we utilize the while loop. This loop terminates if the entered number is not a hexadecimal number. Meanwhile, we employ the scanf() method, which converts the hexadecimal number into a decimal number. The percent sign (%) and the entered number are passed as parameters to the scanf() method.
Here, the percent (%) symbol provides conversion requirements. This conversion identifier instructs the scanf() function on how to read and transform characters from the input into converting format string data. In the parameter list, the value is allocated to one of the variables. The Scanf() method reads the specified file from left to right. Words outside the conversion parameters are anticipated to adjust the input stream’s string of characters; while the relevant letters in the input file are analyzed but not saved.
The function terminates with an “identical” error if a word in the input stream contradicts the file. If the contradictory word may not have been read, it is left in the data stream. We again apply the printf() method to print a hexadecimal and a decimal number. These numbers are stored in the variable “num”. as shown below:
Get the Name and Status of the Employee:
In this case, we will read the employee data from a file. Then, get the name and status of the employee:
int main()
{
FILE *fPointer;
char empname[30];
char status[30];
fPointer = fopen("file.txt", "w+");
if (fPointer == NULL)
{
printf("file is not found. \n");
return 0;
}
printf("Employee name \n");
scanf("%s", empname);
fprintf(fPointer, "Name= %s\n", empname);
printf("Status of employee \n");
scanf("%s", status);
fprintf(fPointer, "Status= %s\n", status);
fclose(fPointer);
}
Before writing the code in the body of the main() function, we must include the header file <stdio.h>. This header file is required for both input and output. The main() function is now initialized, and we make a pointer for the file.
In addition, we define the two-character data type variables, “empname” and “status”. To access the required file, we are using the fopen() method. Meanwhile, the file’s path and mode are provided as arguments to the fopen() function. Then, we use the if condition. The printf() command prints “file not found” if the defined file is unavailable.
Moreover, the scanf() method is applied. The employee’s name is retrieved from that specified file using this function. Next, The fprintf() function is used to show the “Employee name”, which is saved in the variable “empname”. Likewise, we have been using the scanf() function to acquire the employee’s status from the file. The fprintf() method will also be utilized to display the “Employee Status”. This will be saved in the “status” variable.
Finally, we define the fclose() method, which will shut the definite file.
Conclusion:
In this article, we discussed the process of the fscanf() method in C. We utilized the fscanf() method to extract information from a file. It usually holds two arguments streams and formats. We also explore ways to implement this function. This feature is especially useful when we only have to read particular information from a file and not the entire stream. We can convert the hexadecimal value into decimal value by using this function. We hope you found this article helpful. Check the other Linux Hint articles for more tips and tutorials.