Parent directory
acceder au document
/*******************************************************************************
author: Pierre-Emmanuel Périllon
date: 30/05/2007
encoding: utf8
level: beginner+
langage: C++ (without class), comments in french
licence: Creative common by+nc+sa. see http://creativecommons.org/
website: http://tutorat.univ-lyon1.fr/
build: gcc -Wall extract.c -o extract
********************************************************************************

this program extract a string from the file at wanted location

*/

#include <unistd.h>
#include <stdio.h>


int reader(int in, int out, int from, int to)
{
char c[4097];
int r;
int cpt = 0;
int nb = 1;
while (1)
{
cpt++;
r = read(in, &c, nb );
if ( r != nb )
{
/* finish*/
c[0]='\n';
write( out, &c, 1);
return 0;
}
else if ( cpt <= to )
{
if ( from <= cpt )
{
write( out, &c, 1);
}

}
else
{
nb = 4096;
}

}
}


int main ( int argc, char* argv[] )
{
int from;
int to;
int r = 0;

/* write( 1, "start up!\n",12);
*/ if (argc != 3 )
{
write( 2, "pouettt !",9);
return 2;
}

r = sscanf(argv[1],"%d",&from);
if ( r != 1 )
{
return 2;
}
r = sscanf(argv[2],"%d",&to);
if ( r != 1 )
{
return 2;
}
return reader(0, 1, from, to);
}



acceder au document