Print out first N lines

This is a discussion on Print out first N lines within the C Programming forums, part of the General Programming Boards category; Hey, this is my first post, sorry if my format is off. Also, this is my first C program I've ...

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    1

    Unhappy Print out first N lines

    Hey, this is my first post, sorry if my format is off. Also, this is my first C program I've written EVER; I apologize if my code sucks as well. Here goes:

    I have to basically duplicate the functionality of the head command, as the command 'myhead'. This means its usage is: myhead [-nN] [FILE]. If no FILE is specified, the command must read from standard input. If no option [-nN] is specifies, the command prints the default first ten lines. For example, myhead -n5 would print back the first 5 lines of code from standard in, myhead text.txt would print the first 10 lines of that file, and so on (just as the normal 'head' command would.

    1.My problem lies mostly within the portions where you have to read from standard input; for some reason, it only loops through once, rather than the standard 10 or the specified number of times from the option -nN.

    2. My second problem is when I specify a file name, the loop goes through the correct number of times, but instead of printing out the first ten lines, it prints out the first line ten times.

    My code is posted below, if anyone can offer a suggestion or help with the code, please let me know, it would be greatly appreciated. Remember, I am extremely new to C, so please go easy on me and explain things so an idiot could understand them Thank you so much in advance.
    Code:
    // Will either print the first ten lines of a file or of standatd input,
    // or print the first N lines of a file or standard input.
    //
    // Compile using:
    //   gcc -Wall -o myhead myhead.c
    // Call as:
    //   myhead [-nN] [FILE]
    
    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    #include <errno.h>
    
    #define MAX_LINE_LENGTH
    
    //Prototypes:
    char *get_next_line(FILE *fpntr);
    
    int main(int argc, char *argv[]){
    
    // static char buff[100];
    FILE *fpntr;
    char *line;
    // char *next_line;
    int i;
    char *file_pathname;
    
    //Check for proper number of arguments
    if(argc > 4)
        {fprintf(stderr, "Usage: myhead [-nN] [FILE]\n");
            return EXIT_FAILURE;}
    
    //ONE ARGUMENT
    
    //If there is only one argument, print first ten lines of standard in
    if(argc == 1){
        for(i=0; i<10; i++){
            if((line = get_next_line(stdin)) == NULL){
                    (ferror(stdin));
                    perror("Error reading line");
                    exit(EXIT_FAILURE);}
            else{printf("%c\n", *line);
                     exit(EXIT_SUCCESS);}}}
    //TWO ARGUMENTS
    
    //If there are two arguments, compare if the first two characters are equal to '-n', if so print the first N lines of standard in
    //If not, print the first ten lines of the FILE (argv[1])
    if(argc == 2){
        if(strncmp(argv[1], "-n", 2) == 0){  //print N lines from stdin
            int cnt = atoi(argv[1]+2);
            for(i=0; i<cnt; i++){
                 if((line = get_next_line(stdin)) == NULL){
                    (ferror(stdin));
                    perror("Error reading line");
                    exit(EXIT_FAILURE);}
                else{printf("%c\n", *line);
                     exit(EXIT_SUCCESS);}}}
        else{                               // print 10 lines from file
            file_pathname = argv[1];
            for(i=0; i<10; i++){
                if((fpntr = fopen(file_pathname, "r")) == NULL){
                    fprintf(stderr, "Error opening file %s: %s/n", file_pathname, strerror(errno));
                    return EXIT_FAILURE;}
                else{
                    if((line = get_next_line(fpntr)) == NULL) {
                        perror("Error reading line");
                        exit(EXIT_FAILURE);}
                    else{
                        printf("%s: \n", line);
                        fclose(fpntr);}}}}
        //print first ten lines of FILE
    }
    
    //THREE ARGUMENTS
    
    //If there are three arguments and the second argument is three characters, store third character as int 'line' and print the first 'line' lines of the file(argv[3])
    if(argc == 3){
        file_pathname = argv[2];
        int cnt = atoi(argv[1]+2);
        for(i=0; i<cnt; i++){
                        if((fpntr = fopen(file_pathname, "r")) == NULL){
                    fprintf(stderr, "Error opening file %s: %s/n", file_pathname, strerror(errno));
                    return EXIT_FAILURE;}
                else{
                    if((line = get_next_line(fpntr)) == NULL) {
                        perror("Error reading line");
                        exit(EXIT_FAILURE);}
                    else{
                        printf("%s: \n", line);
                        fclose(fpntr);}}}}
    
        //get third character and store as int line
        //print first line lines of stdin
    
    
    /*
    
    // ERROR CHECK
    if(argc == 2){
        if ((fpntr = fopen(argv[2], "r" == NULL)) {
            fprintf(stderr, "%s: Error opening file %s: %s\n", argv[0], file_pathname, strerror (errno));
        return EXIT_FAILURE;}
        else
            while (next_line = get_next_line(fprntr)) != NULL)
    }
    */
    
    return EXIT_SUCCESS;}
    
    char *get_next_line(FILE *fpntr){
        static char line_buff[MAX_LINE_LENGTH+1];
        int buff_pos, next_char;
    
        buff_pos = 0;
        while ((next_char = fgetc(fpntr)) != '\n' && next_char != EOF)
            line_buff[buff_pos++] = next_char;
    
    line_buff[buff_pos] = '\0';
    
    if (next_char == EOF && (buff_pos == 0 || ferror(fpntr)))
        return NULL;
    else
        return line_buff;
    }

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,471
    1. work on your indentation - currrent style is unreadable
    2. use fgets to read a line
    3. printf("&#37;c\n", *line) - prints first character of the buffer

    printf("%s: \n", line); - prints the whole line

    I'm really tiered of reading your code futher...
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Blank lines "\n"
    By Coding in forum C++ Programming
    Replies: 15
    Last Post: 02-18-2008, 07:56 PM
  2. Retail Outlet Managment System - the 4th
    By Presidentofusa in forum C Programming
    Replies: 3
    Last Post: 11-10-2007, 09:44 PM
  3. print lines
    By 8ball in forum C Programming
    Replies: 8
    Last Post: 05-14-2004, 01:45 AM
  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, 07:20 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21