ive "sort of" written a function that prints lines from a file with line numbers
i want modify this program so that when i input "ln2,5" it prints
out lines 2 to 5..
i know that i need sscanf to do this, but im not sure how it works, can anyone give me some hints.....
Code:
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
/*void linenumbers(Input){
while( fgets (Input, 1024, fp) != NULL )
{
printf("%6d: %s", count++, Input);
}
printf("\n");
fclose (fp);
return 0;
}*/
int main( int argc, char *argv[])
{
char szKey[] = "q\n";
char input[1000];
static char array [10];
FILE *fp;
if(argc != 2) {
printf("Usage: 1021edlin filename\n");
exit(0);
}
if(( fp = fopen(argv[1],"r")) == NULL) { //if file does not exist
printf("New File\n"); //print "New File"
do {
printf ("> "); //print >
fgets(input,100,stdin);
}
while (strcmp (szKey,input) != 0); //if q is entered
return 0; //exit
}
/* Seek to end of file*/
if(fseek(fp,0L ,SEEK_END)) {
perror("Cannot seek to end of file... \n");
exit(0);
}
while(fgets(array,sizeof(array),fp)!=NULL)
printf("%s\n",array);
/* Display current position of SPI */
printf("%ld\n",ftell (fp));
do {
printf ("> ");
fgets(input,100,stdin);
}
while (strcmp (szKey,input) != 0);
return 0;
}