How to Read Text File That Begings With a Number Followed by Text in C++
C language has numerous libraries that include predefined functions to make programming easier. In C language, header files incorporate the ready of predefined standard library functions. Your asking to use a header file in your plan past including it with the C preprocessing directive "#include". All the header file have a '.h' an extension. By including a header file, we can utilize its contents in our program.
C++ too offers its users a variety of functions, 1 of which is included in header files. In C++, all the header files may or may not end with the ".h" extension but in C, all the header files must necessarily end with the ".h" extension.
A header file contains:
- Part definitions
- Data type definitions
- Macros
It offers the in a higher place features by importing them into the programme with the help of a preprocessor directive "#include". These preprocessor directives are used for instructing compiler that these files need to be processed before compilation.
In C program should necessarily contain the header file which stands for standard input and output used to take input with the help of scanf() and printf() function respectively.
In C++ program has the header file which stands for input and output stream used to have input with the aid of "cin" and "cout" respectively.
There are of 2 types of header file:
- Pre-existing header files: Files which are already available in C/C++ compiler we just need to import them.
- User-defined header files: These files are defined past the user and can be imported using "#include".
Syntax:
#include <filename.h> or #include "filename.h"
We can include header files in our programme by using one of the above two syntax whether it is pre-defined or user-defined header file. The "#include" preprocessor is responsible for directing the compiler that the header file needs to be processed earlier compilation and includes all the necessary data type and function definitions.
Annotation: We can't include the aforementioned header file twice in any program.
Create your own Header File:
Instead of writing a large and circuitous lawmaking, nosotros can create your ain header files and include them in our program to use it whenever we desire. Information technology enhances code functionality and readability. Beneath are the steps to create our own header file:
- Write your own C/C++ code and salve that file with ".h" extension. Below is the illustration of header file:
CPP
int
sumOfTwoNumbers(
int
a,
int
b)
{
return
(a + b);
}
- Include your header file with "#include" in your C/C++ program as shown beneath:
CPP
#include "iostream"
#include "sum.h"
using
namespace
std;
int
chief()
{
int
a = 13, b = 22;
cout <<
"Sum is: "
<< sumOfTwoNumbers(a, b)
<< endl;
}
Below is the output of the above program:
Including Multiple Header Files:
You tin can utilise various header files in a program. When a header file is included twice within a program, the compiler processes the contents of that header file twice. This leads to an error in the program. To eliminate this fault, conditional preprocessor directives are used.
Syntax:
#ifndef HEADER_FILE_NAME #define HEADER_FILE_NAME the unabridged header file
0 Response to "How to Read Text File That Begings With a Number Followed by Text in C++"
Post a Comment