How to Create sparse file with alternate data and hole.
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
How to Create sparse file with alternate data and hole.
Hi All,
I have to write a program in c where I have to create a file with known pattern data and hole in it, and it should be such that a 4k data and then it will skip 12K and then again it will write the same 4k data .
It should be such that
data hole data hole data
(4k) (12K) (4k) (12K) (4k)
here is the solution for 400 chars of data and 1200 holes.
you need to turn this into a loop though
Say hello to your teacher for me (guessing it's homework)
Code:
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
int main()
{
char buf[400];
int fd = open("file.hole", O_WRONLY|O_TRUNC|O_CREAT, 0600);
memset(buf, 'a', 400);
write(fd, buf, 400);
lseek(fd, 1200, SEEK_CUR); // Make a hole
write(fd, buf, 400);
lseek(fd, 1200, SEEK_CUR); // Make a hole
write(fd, buf, 400);
close(fd);
return 0;
}
Last edited by 16pide; 10-13-2011 at 08:13 AM.
Reason: correct missunderstanding of lseek
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.