1

I want to write my own code for tail Unix command but I am having a lot of trouble doing that. I am completely new to C language and apparently lost on how to fix my code. I am having number of problems regarding my code:

  1. I am unable to read and print lines from text file in the if statements it is not printing any string from file when I run it don't know why?
  2. Unable to print specific lines in if statement by taking user input as starting line and then printing till the End of File.
  3. I am having trouble figuring out the right solution to my problems and debugging what problems there are in code.

I would really appreciate your help in figuring how to do all the above in my code. If someone can help make changes and get my code to work right.

#include <stdio.h>// for fopen, fscanf, fclose, fprintf
#include <stdlib.h>// for exit
#include <string.h>

int main(int argc, const char * argv[]){

    printf("Opening file\n");
    char filename[64]; // file attribute
    strcpy(filename, argv[1]); //copy string from argv[1] to filename
    printf("FILENAME: %s \n", filename);
    FILE* fp; // file pointer
    int ch, linestotal = 0, user;
    char c[10000];

    if(argv[2]){       //checking input argv[2]
     user = atoi(argv[2]);      // char to int
    }

    fp = fopen( filename, "r"); // file read

    if(fp == NULL){     // verify file is opened
        printf("Error opening file");
        exit(1);
    }

    while(!feof(fp)) // check end of file
    {
        ch = fgetc(fp);
        if(ch == '\n')
        {
            linestotal++;    //Checking total lines inside file
        }
    }

    printf("Total no. of lines: %d\n", linestotal );
    printf("User input: %d\n", user );

    printf("**********************\n");
    if (!user && linestotal<= 10)
    {
        while ( (ch = fgetc(fp) ) != EOF)
            printf("%c", ch);

        fclose(fp);
        printf("********************\n");
    }if(!user && linestotal>10) {  // to print 10 lines
        for(int i = (linestotal-10); i <= (linestotal); i++)
        {   c[i] = fgetc(fp);
            printf("%c", c[i]);
        }
        fclose(fp);
        printf("********************\n");

    }if(user && user<linestotal) {
        for(int i = (linestotal-user); i <= (linestotal); i++)
        {   c[i] = fgetc(fp);
            printf("%c", c[i]);
        }
        fclose(fp);
        printf("********************\n");
    }if(user && user>linestotal){
        while ( (ch = fgetc(fp) ) != EOF)
            printf("%c", ch);

        fclose(fp);
        printf("********************\n");
    }else{
        printf("Unable to read and print file \n");
    }

    printf("End of file");
    return 0;
}
bmalhi
  • 162
  • 4
  • 20
  • 6
    Ask one question at a time. And make it specific. "I am unable to..." is not a good description of what you need help with. Explain exactly what the problem is with the code for that part or why you are "unable to". – kaylum Jun 27 '16 at 06:50
  • 1
    Well, you read until EOF first and then try and read through the same file handle, either checking or forgetting to check for EOF, depending on user input and length of file. [Don't do `while(!feof(fp))`](http://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong) followed by `fgetc`. You have it correct later: `while ( (ch = fgetc(fp) ) != EOF)`. – Ilja Everilä Jun 27 '16 at 06:51
  • 1
    You could look at gnu or bsd version of `tail` as an example of what could be done. Just google... – Serge Ballesta Jun 27 '16 at 07:18
  • @kaylum I have edited my question sorry for miscommunication I new on stack overflow. I need help with printing the file text inside the If loop it would not allow me to print anything I don't know why can u help? – bmalhi Jun 27 '16 at 19:55
  • `while(!feof(fp))` after that loop your `fp` file pointer is at the end of the file. You need to [`rewind`](http://linux.die.net/man/3/rewind) the file pointer back to the beginning of the file or [`fseek`](http://linux.die.net/man/3/fseek) to where you want to start reading from. Otherwise you won't be able to read anything from that point on. – kaylum Jun 27 '16 at 22:36
  • See [`while (!feof(file))` is always wrong](http://stackoverflow.com/questions/5431941/while-feof-file-is-always-wrong) for a discussion of why you don't write your loops like that. You don't check that you've got an `argv[1]` value; you don't check that you've got an `argv[2]` value. You never write code with `}if(!user && linestotal>10) {` all on one line if you want to survive a code review. You don't rewind the file; you don't always ensure you get a character from `fgetc()` — other processes can change a file while you have it open. – Jonathan Leffler Jun 28 '16 at 06:43

0 Answers0