HomeLinuxLearn() Operate in C Language

Learn() Operate in C Language


The C language has two fundamental capabilities that can help you learn the recordsdata utilizing the learn() and fread() capabilities. It additionally has strategies with capabilities to learn the recordsdata character-by-character.

It is very important know the file processing capabilities, their name strategies, enter and output arguments, and so forth., as a way to use them fluently as a result of they’re the useful resource out there to the programmer to retailer and eliminate the data that’s saved within the system.

On this Linux Trace article, you’ll learn to use the learn() perform to learn the recordsdata.

We’ll clarify the whole lot about “ella”, its syntax, the tactic name, the enter and output arguments, the kind of information that they every settle for, and the way to declare them correctly.

We then apply what we realized by placing the usage of this function into sensible examples.

So as so that you can be absolutely conscious of the way to use the learn() perform, we added a particular part which describes all of the errors that may happen when utilizing this perform, in addition to their detection and identification, so that you’ve the required methods for a fast answer in case of an error.

Syntax of the Learn() Operate in C Language

size_t learn(int fd, void *buf, size_t depend);

Description of the Learn() Operate in C Language

The learn() perform reads the contents of the open file which is specified by its descriptor within the “fd” enter argument. This perform reads and copies the depend variety of bytes into the buffer that’s pointed to by buf. The “fd” identifier is an integer that’s returned as the results of the open() perform when the file is opened.

If the learn() perform returns efficiently, it returns the variety of bytes learn. A outcome that is the same as 0 signifies that the file is learn to the tip, and -1 signifies that an error happens. The precise error might be recognized by retrieving its code from the errno international variable. Later, you will see that a piece which describes the errors that may happen when utilizing the learn() perform and the methods to detect and establish them.

The learn() perform is asserted within the “unistd.h” header. To make use of it, you will need to embody this file in your code as follows:

The right way to Learn a File with the Learn() Operate in C Language

On this instance, we’ll clarify the way to open and skim a file utilizing the open() and skim() capabilities.

For this objective, we beforehand created a textual content file with the identify, “instance.txt”, by way of the Linux Supervisor and saved it within the “Paperwork” listing. We wrote the primary paragraph of this text in it.

Step one in growing the code to learn the file is to incorporate the required headers and create a principal() perform that returns an empty worth. We outline the “fd” integer in it which serves because the file descriptor, a 1024 character buffer referred to as “buff” the place the data that’s learn by the learn() perform is saved. The array path shops the trail and the identify of the file that we wish to learn.

After defining the required variables, we name the open() perform to open the file. We name this perform by passing the trail array with the trail and identify of the file as the primary enter argument and specifying the O_RDONLY flag because the second argument. We cross the “fd” integer because the output argument the place open() returns the descriptor that we use to learn to the file.

As soon as we’ve the file open, we learn its contents by calling the learn() perform and passing the “fd” descriptor that’s returned by the open() perform as the primary argument. We cross the pointer to the “buff” buffer because the second argument the place we retailer the contents to be learn. Lastly, we cross the scale of the buffer which is 1024 bytes on this case. We then use the printf() perform to show the contents which might be saved in “buff” within the command console.

Right here is the entire code for this instance:

#embody <stdio.h>

#embody <stdlib.h>

#embody <unistd.h>

#embody <fcntl.h>

int principal()
{  
intfd;
char buff[1024];
   char path[] = “Paperwork/instance.txt”;
fd = open(path, O_RDONLY);
   learn(fd, buff, 1024);
printf(nn%snn,buff);
   }

Within the following determine, we see the compilation and execution of this code. As we are able to see, learn() places your entire contents of the “instance.txt” file into “buff” and the printf() perform prints it to the command console:

The right way to Detect and Determine the Errors That Can Happen When Utilizing the Learn() Operate within the C Language

Utilizing learn() can generate varied errors. When this occurs, this perform returns a outcome that is the same as -1.

The simplest technique to decide if an error has occurred is to make use of an “if” situation the place the situation is the return worth of -1. Now, allow us to see how you should utilize this methodology to find out if an error has occurred:

int n;

n = learn(fd, buff , 1024);

if ( n == 1){

printf (“An error occurred whereas attempting to learn the file.”);

 }

If the learn() perform returns with an error, it transitions to the “if” assertion and prints the message, “An error occurred whereas attempting to learn the file”.

When an error happens, a numeric code is routinely saved within the errno international variable which is outlined within the “errno.h” header. This code can be utilized to establish the error that occurred.

The next is an excerpt with the errors that the learn() perform can generate and which might be outlined within the “errno.h” header, together with a quick description of every error and the related integer worth:

Definition Worth in errno Error
EAGAIN 11 Attempt once more.
EBADF 9 Incorrect file quantity.
EDESTADDRREQ 89 Vacation spot tackle required.
EFAULT 14 Incorrect tackle.
EFBIG 27 File too large.
EINTR 4 System name interrupted.
EINVAL 22 Invalid argument.
EIO 5 I/O error.
EPERM 1 Operation not allowed.

The simplest technique to establish an error is to open a swap the place the errno variable is the soar situation and every case is an error definition.

Subsequent, allow us to take a look at an instance the place we attempt to enter a descriptor with a unfavourable signal, leading to an error. To establish an error, we use the “if” situation that we noticed within the earlier snippet. To establish it, we open a swap with the three most typical errors that this perform can produce.

#embody <stdio.h>

#embody <stdlib.h>

#embody <unistd.h>

#embody <fcntl.h>

#embody <errno.h>

int principal()
{  
intfd,n;
char buff[1024];
   char path[] = “Paperwork/instance.txt”;
fd = open(path, O_RDONLY);

   n = learn(0, buff, 1024);
   if (n == 1){
   swap(errno){
    case EBADF:{
printf(“Dangerous file quantity. Error: %in, errno);
break;}
    case EINVAL:{
printf(“Invalid argument. Error: %in, errno);
break;}
    case EIO:{
printf(“I/O error . Error: %in, errno);
break;}
    }
 }
}

As we are able to see within the following determine, the learn() perform returns an error when an invalid descriptor is handed as an enter argument. The worth that’s retrieved from the errno variable is used as a soar situation which permits us to establish the error once we enter the EBADF case.

Conclusion

On this Linux Trace article, we confirmed you the way to use the learn() perform, some of the widespread capabilities that’s applied within the C language to learn the recordsdata.

We checked out its syntax and a piece which describe its theoretical operation, enter and output arguments, and its information varieties. Afterwards, we applied what we realized by a sensible instance with code and pictures that reveals the way to open and skim a file utilizing the open() and skim() capabilities.

To have the required means to troubleshoot a potential error when utilizing this perform, we added a particular part which explains the strategies that the C language gives to detect and establish the errors.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments