Question

I need help using this given program and making it not create a hole in the...

I need help using this given program and making it not create a hole in the file. Once compiled and executed this program creates a file that is 16394 bytes but has a hole in it. I need to make a file WITHOUT A HOLE that is the same size (16394 bytes) from this program. I know I need to remove/change the lseek portion. I just dont know what to replace it with. A write function?

Given Program:

#include "apue.h"
#include <fcntl.h>

char    buf1[] = "abcdefghij";
char    buf2[] = "ABCDEFGHIJ";

int
main(void)
{
        int             fd;

        if ((fd = creat("file.hole", FILE_MODE)) < 0)
                printf("creat error");

        if (write(fd, buf1, 10) != 10)
                printf("buf1 write error");
        /* offset now = 10 */

        if (lseek(fd, 16384, SEEK_SET) == -1)
                printf("lseek error");
        /* offset now = 16384 */

        if (write(fd, buf2, 10) != 10)
                printf("buf2 write error");
        /* offset now = 16394 */

        exit(0);
}
0 0
Add a comment Improve this question Transcribed image text
Answer #1
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>

char buf1[] = "abcdefghij";
char buf2[] = "ABCDEFGHIJ";

intmain(void)
{
        int fd;
        if ((fd = create("file.hole", FILE_MODE)) < 0)
                printf("creat error");

        if (write(fd, buf1, 10) != 10)
                printf("buf1 write error");
        /* offset now = 10 */

        if (lseek(fd, 16383, SEEK_SET) == -1)
                printf("lseek error");
        /* offset now = 16383 */
        
       /* write just one byte at the end, here its null char */
       result = write(fd, "", 1);
       if (result < 0) {
                close(fd);
                perror("Error writing a byte at the end of the file");
            return 1;
       }
       /* offset now = 16384 */
        if (write(fd, buf2, 10) != 10)
                printf("buf2 write error");
        /* offset now = 16394 */
        
        close(fd);
        exit(0);
}

--End of code--

Adding at least one byte of data (here I used NULL character), in the end, so that the hole doesn't get created in the file. The 'lseek' has been changed to 16383 and a NULL char '\0' has been written to pos 16384. Then buf2[] has been written next. OS will fill the next bytes (from 10 to 16383) with all zeroes, that will hopefully remove the whole. The file size also did not increase.

Please leave a "Thumbs Up", it will be of great help. Thanks.

Happy Studying.

Add a comment
Know the answer?
Add Answer to:
I need help using this given program and making it not create a hole in the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I need help fixing this code in C. It keeps giving me an error saying error:...

    I need help fixing this code in C. It keeps giving me an error saying error: ‘O_RDWD’ undeclared (first use in this function) if ((fd = open(fifo, O_RDWD)) < 0) note: each undeclared identifier is reported only once for each function it appears in. Please fix so it compiles properly. //************************************************************************************************************************************************** #include <fcntl.h> #include <stdio.h> #include <errno.h> #include<stdlib.h> #include <string.h> #include<unistd.h> #include <sys/stat.h> #include <sys/types.h> #define MSGSIZE 63 char *fifo = "fifo"; int main(int argc, char **argv){    int fd;...

  • Directions: use only the signal mechanism system calls don’t use ( wait() or pipe() )in this...

    Directions: use only the signal mechanism system calls don’t use ( wait() or pipe() )in this problem. You can still read/write from/to a file. You must use ( kill() and pause() ) system calls. rewrite code below to do kill and pause system calls #include <fcntl.h> #include <unistd.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/wait.h> // Function ptototypes int readX(); void writeX(int); int main() { int pid; // pid: used to keep track of the child process int x...

  • GIVEN CODE- FILL IN THE BLANK! #include <fcntl.h> #include <unistd.h> #include <stdio.h> #include <stdlib.h> #include <string.h>...

    GIVEN CODE- FILL IN THE BLANK! #include <fcntl.h> #include <unistd.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/wait.h> // Function ptototypes int readX(); void writeX(int); int main() /// chi read x ---> divi ---> write x into file ---> par read x --> sub--> write x into file---> chi read x-->etc {    int pid;           // pid: used to keep track of the child process    int x = 19530;       // x: our value as integer   ...

  • C++ HELP I need help with this program. I have done and compiled this program in...

    C++ HELP I need help with this program. I have done and compiled this program in a single file called bill.cpp. It works fine. I am using printf and scanf. Instead of printf and scanf use cin and cout to make the program run. after this please split this program in three files 1. bill.h = contains the class program with methods and variables eg of class file class bill { } 2. bill.cpp = contains the functions from class...

  • C++ HELP I need help with this program. I have done and compiled this program in a single file called bill.cpp. It works fine. But I need to split this program in three files 1. bill.h = contains the...

    C++ HELP I need help with this program. I have done and compiled this program in a single file called bill.cpp. It works fine. But I need to split this program in three files 1. bill.h = contains the class program with methods and variables 2. bill.cpp = contains the functions from class file 3. main.cpp = contains the main program. Please split this program into three files and make the program run. I have posted the code here. #include<iostream>...

  • I JUST NEED HELP WITH DISPLAY PART! please help! thanks in advance // This function saves...

    I JUST NEED HELP WITH DISPLAY PART! please help! thanks in advance // This function saves the array of structures to file. It is already implemented for you. // You should understand how this code works so that you know how to use it for future assignments. void save(char* fileName) { FILE* file; int i; file = fopen(fileName, "wb"); fwrite(&count, sizeof(count), 1, file); for (i = 0; i < count; i++) { fwrite(list[i].name, sizeof(list[i].name), 1, file); fwrite(list[i].class_standing, sizeof(list[i].class_standing), 1, file);...

  • Need help with this C program? I cannot get it to compile. I have to use...

    Need help with this C program? I cannot get it to compile. I have to use Microsoft Studio compiler for my course. It's a word game style program and I can't figure out where the issue is. \* Program *\ // Michael Paul Laessig, 07 / 17 / 2019. /*COP2220 Second Large Program (LargeProg2.c).*/ #define _CRT_SECURE_NO_DEPRECATE //Include the following libraries in the preprocessor directives: stdio.h, string.h, ctype.h #include <stdio.h> /*printf, scanf definitions*/ #include <string.h> /*stings definitions*/ #include <ctype.h> /*toupper, tolower...

  • -I need to write a program in C to store a list of names (the last...

    -I need to write a program in C to store a list of names (the last name first) and age in parallel arrays , and then later to sort them into alphabetical order, keeping the age with the correct names. - Could you please fix this program for me! #include <stdio.h> #include <stdlib.h> #include <string.h> void data_sort(char name[100],int age[100],int size){     int i = 0;     while (i < size){         int j = i+1;         while (j < size){...

  • Please, please help with C program. This is a longer program, so take your time. But...

    Please, please help with C program. This is a longer program, so take your time. But please make sure it meets all the requirements and runs. Here is the assignment: I appreciate any help given. Cannot use goto or system(3) function unless directed to. In this exercise you are to create a database of dogs in a file, using open, close(), read, write(), and Iseek(). Do NOT use the standard library fopen() ... calls! Be sure to follow the directions!...

  • GIVEN CODE. PLEASE FILL IN THE BLANK. // Include Block #include <fcntl.h> #include <unistd.h> // Preprocessor...

    GIVEN CODE. PLEASE FILL IN THE BLANK. // Include Block #include <fcntl.h> #include <unistd.h> // Preprocessor declarations #define BUF_SIZE 10 /* global constant buffer size: Generally this would be much larger, but I want to show you that it works in a loop until the entire file is read.*/ // main function int main(int argc, char *argv[]) {    // Variable declarations    int fd;                       // file descripter    char buffer[BUF_SIZE];       // string for...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT