if i want my program to print out lines 2 to 10 by inputing "2-10", what's the best way to do this?
there are other inputs for this program
if i want my program to print out lines 2 to 10 by inputing "2-10", what's the best way to do this?
there are other inputs for this program
Consider taking a look at this FAQ example on how to read input. I'll assume you mean from a file you're opening, otherwise, you'll have to be a bit more clear on where the "lines" are coming from. If I'm assuming correct:
That should get you started. Post your code attempt, specific errors, and all the good stuff like that covered in the Announcement that covers getting help.Code:open the file read input from user while the current line count is less than the starting line read and discard this line from the file while the current line count is less than the ending line read and display this line from the file close the file
Quzah.
Hope is the first step on the road to disappointment.
Try this
Once you have first and last, then go and print those lines using some other functionCode:#include <stdio.h> int main ( int argc, char *argv[] ) { char buff[BUFSIZ]; int max_lines = 10000; while ( fgets( buff, BUFSIZ, stdin ) != NULL ) { int first = 1, last = max_lines; if ( sscanf( buff, "p %d-%d", &first, &last ) == 2 ) { // 2 params indicates a range } else if ( sscanf( buff, "p %d", &first ) == 1 ) { // 1 param means just that line last = first; } else if ( buff[0] == 'p' ) { // no params means print all } else { printf( "I don't understand %s", buff ); continue; } printf( "printing from lines %d to %d\n", first, last ); } return 0; }
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
i hav varibles input1=2 and input2=5 and i want to print lines 2 to 5 from a file
is this the way to do it?
fp = fopen(argv[1],"r")
while (count>input1&&count<input2);
printf(fgets(Input, 1024, fp));
actually the printing line im not so sure
is this the right?
FILE *fp
input1=2;
input2=5;
fp = fopen(argv[1],"r")
while(count<input1);
printf(fgets(Input, 1024, fp));
while(count>input2);
printf(fgets(Input, 1024, fp));
fclose(fp);
This prints all linesNow youCode:while ( fgets( buff, sizeof buff, fp ) { fputs( buff, stdout ) }
1. add a counter which counts the lines
2. compare that counter with your limits
3. print lines which match your criteria
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
this is what i hav now
hav some errors..Code:/*open the file read input from user(input1, input2) while the current line count is less than input1 read and discard this line from the file while the current line count is less than input2 read and display this line from the file close the file*/ #include <stdio.h> int main( int argc, char *argv[]) FILE *fp; int input1=2; int input2=5; int count=0; char Input[2048]; if(( fp = fopen(argv[1],"r")) != NULL);{ fp = fopen(argv[1],"r"); while(count<input1); fgets(Input, 1024, fp); while(count>input2); fgets(Input, 1024, fp); printf("%s",++count,Input); fclose(fp); } }
If you want a loop to do something, you need to do it one of two ways:
Or...Code:while( something_is_happening ) do_this_one_thing( );
Notice the difference between the first example, and your while loops. If you have a ; at the end of the line where your while loop is, it means that you're doing in effect, nothing. You either need to remove the ; at the end of your loop for it to use the next line only, or use { } around the all the lines you want it to do.Code:while( something_is_happening ) { do_this_thing( ); and_this_thing( ); ... and_all_these_things( ); }
You have another problem, in that, you are not incrementing count in your first loop, so it is going to sit there indefinately.
To get in good practice, I'd suggest you use the { } on all of your loops and if else statements.
Quzah.
Hope is the first step on the road to disappointment.