LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-13-2011, 04:12 AM   #1
mango7
LQ Newbie
 
Registered: Oct 2011
Posts: 2

Rep: Reputation: Disabled
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)

Please help me .....
 
Old 10-13-2011, 04:14 AM   #2
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,139

Rep: Reputation: 4122Reputation: 4122Reputation: 4122Reputation: 4122Reputation: 4122Reputation: 4122Reputation: 4122Reputation: 4122Reputation: 4122Reputation: 4122Reputation: 4122
Well, after you show us some code that you wrote, maybe we can offer some commentary.
 
Old 10-13-2011, 04:25 AM   #3
mango7
LQ Newbie
 
Registered: Oct 2011
Posts: 2

Original Poster
Rep: Reputation: Disabled
I have this code which i have to modify

#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#define BLOCK_SIZE 4096

int main(int argc, char **argv)
{
int fd=0;
int i;
void *buf;
int sector_per_block=BLOCK_SIZE/512;
int block_count=4;

if(argc !=2 ){
printf("Wrong usage\n USAGE: program absolute_path_to_write\n");
_exit(-1);
}
fd=open(argv[1],O_WRONLY|O_TRUNC|O_CREAT,0666);
if(fd <= 0){
printf("file open failed\n");
_exit(0);
}

while(block_count > 0){
lseek(fd,BLOCK_SIZE,SEEK_CUR);
block_count--;
for(i=0;i<sector_per_block;i++)
write(fd,buf,512);
block_count--;
}
close(fd);
return 0;
}


right now it is writing it from buffer I want a known pattern should be written.
thanks in advance
 
Old 10-13-2011, 07:30 AM   #4
16pide
Member
 
Registered: Jan 2010
Posts: 418

Rep: Reputation: 83
This is on Mac OS, but probably would be just the same on Linux:

http://stackoverflow.com/questions/1...-c-on-mac-os-x

Or you can use the dd command in Linux. Option to use is seek=
 
Old 10-13-2011, 08:02 AM   #5
16pide
Member
 
Registered: Jan 2010
Posts: 418

Rep: Reputation: 83
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
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
sparse file vs. ramfs Skaperen Linux - Kernel 0 05-17-2011 05:02 PM
alternate data stream sulekha Linux - General 1 08-10-2010 02:17 AM
How to remove some blocks from a sparse file hpsmouse Programming 10 08-16-2009 08:32 AM
Create File Shell Syntax and pass data to the file FirstBorn Linux - General 22 07-31-2008 09:48 PM
use script to create a data file of mulitple users RajRed Linux - Newbie 4 11-27-2007 06:05 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:50 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration