Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

StackOverflow Point

StackOverflow Point Navigation

  • Web Stories
  • Badges
  • Tags
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Web Stories
  • Badges
  • Tags
Home/ Questions/Q 1525
Alex Hales
  • 0
Alex HalesTeacher
Asked: May 30, 20222022-05-30T18:57:22+00:00 2022-05-30T18:57:22+00:00

c – the return value of file operations

  • 0

[ad_1]

I tried to use the lseek function in the next program to read from any position.
Although the return value of close () is specifically ignored in the event of an error (where we should probably just omit closing the descriptor and let the operating system handle it), we implicitly ignore it in the “success” path as well. I think I should have a more robust program and always check the return value of file operations (with a possible exception for printf () for user messages, where failure does not affect the correctness of the primary function of the program).
How can I do that?

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>

#define FILE    "test"

int
main(void)
{
   int fd;
   char buf[10] = { 0 };
   
   printf("lseek(2) test for RW file with append flag\n\n");

   /* open the file */
   printf("open(2)...");
   if( (fd = open(FILE, O_RDWR | O_APPEND | O_CREAT | O_TRUNC, S_IRWXU)) == -1 ) {
      printf(" KO\n");
      return EXIT_FAILURE;
   }
   printf("OK\n");

   /* write something */
   printf("write(2)...");
   if( write(fd, "{first write call}", 18) != 18 ) {
      printf(" KO\n");
      (void)close(fd);
      return EXIT_FAILURE;
   }
   printf(" OK\n");

   printf("\nTrying to seek 5 bytes from the beginning of the file..\n\n");

   /* move somewhere */
   printf("lseek(2)...");
   if( lseek(fd, 5, SEEK_SET) < 0 ) {
      printf(" KO\n");
      (void)close(fd);
      return EXIT_FAILURE;
   }
   printf(" OK\n");

   /* read something */
   printf("read(2)...");
   if( read(fd, buf, 5) == -1 ) {
      printf(" KO\n");
      (void)close(fd);
      return EXIT_FAILURE;
   }
   printf(" OK\n");

   /* write something, again */
   printf("write(2)...");
   if( write(fd, "[2nd write]", 11) != 11 ) {
      printf(" KO\n");
      (void)close(fd);
      return EXIT_FAILURE;
   }
   printf(" OK\n");

   close(fd);

   printf("\nTest finished. Check the '%s' file.\n", FILE);

   return EXIT_SUCCESS;
  }

[ad_2]

  • 0 0 Answers
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report
Leave an answer

Leave an answer
Cancel reply

Browse

Sidebar

Ask A Question

Related Questions

  • xcode - Can you build dynamic libraries for iOS and ...

    • 0 Answers
  • bash - How to check if a process id (PID) ...

    • 4993 Answers
  • database - Oracle: Changing VARCHAR2 column to CLOB

    • 1084 Answers
  • What's the difference between HEAD, working tree and index, in ...

    • 1028 Answers
  • Amazon EC2 Free tier - how many instances can I ...

    • 0 Answers

Stats

  • Questions : 43k

Subscribe

Login

Forgot Password?

Footer

Follow

© 2022 Stackoverflow Point. All Rights Reserved.

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.