Thread: print lines

  1. #1
    Registered User
    Join Date
    May 2004
    Posts
    37

    print lines

    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

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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:
    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
    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.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Try this
    Code:
    #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;
    }
    Once you have first and last, then go and print those lines using some other function
    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.

  4. #4
    Registered User
    Join Date
    May 2004
    Posts
    37
    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));

  5. #5
    Registered User
    Join Date
    May 2004
    Posts
    37
    actually the printing line im not so sure

  6. #6
    Registered User
    Join Date
    May 2004
    Posts
    37
    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);

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    This prints all lines
    Code:
    while ( fgets( buff, sizeof buff, fp ) {
      fputs( buff, stdout )
    }
    Now you
    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.

  8. #8
    Registered User
    Join Date
    May 2004
    Posts
    37
    this is what i hav now
    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);
      }
    }
    hav some errors..

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    If you want a loop to do something, you need to do it one of two ways:
    Code:
    while( something_is_happening )
        do_this_one_thing( );
    Or...
    Code:
    while( something_is_happening )
    {
        do_this_thing( );
        and_this_thing( );
        ...
        and_all_these_things( );
    }
    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.

    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Print out first N lines
    By YoYayYo in forum C Programming
    Replies: 1
    Last Post: 02-21-2008, 12:58 AM
  2. Blank lines "\n"
    By Coding in forum C++ Programming
    Replies: 15
    Last Post: 02-18-2008, 08:56 PM
  3. Retail Outlet Managment System - the 4th
    By Presidentofusa in forum C Programming
    Replies: 3
    Last Post: 11-10-2007, 10:44 PM
  4. What kind of programs should I start writing?
    By Macabre in forum C++ Programming
    Replies: 23
    Last Post: 04-12-2003, 08:13 PM
  5. Replies: 0
    Last Post: 03-28-2003, 08:20 AM