Thread: Evaluate me...

  1. #1
    Its not rocket science vasanth's Avatar
    Join Date
    Jan 2002
    Posts
    1,683

    Evaluate me...

    Hi Guys...
    I have this question

    1) Write a function that will list a text file, with each line preceded by a number, similar to the unix cat utility. The function has a parameter of FILE * ( the file should be opened and checked before entering the line number function). Watch for any extra line number at the end.



    2) Write a different version of the function to take a second parameter which is the number of lines to be displayed at a time ( e.g. 25) . You will need a user dialogue (along the lines of “press enter to continue”). If the value of the integer parameter is zero then the entire file is listed , as in part (1)

    3) Write a third version of the function, with FILE * and two integer parameters: the first int parameter is as in part (2) above, the second int is the line number after which the file will be displayed and line numbered. If this value is 0 then the entire file displayed. If the value is greater that the number of lines in the file, then nothing is displayed!


    Ok below is the solution for second and third question.. The program works perfect.. Please tell me wasy to improve it and the problems it has..(even if it is the most simplest..)..

    (Actually out group has to evaluate this code of another group. we have found some stuff.. but the code looks so perfect that there seems to be no bug)...


    code for question 2
    Code:
    #include <stdio.h>
    
    void fprintfile(FILE *, int);
    
    int main()
    {
            int lines;
            char fname[20];
            FILE * fptr;
    
            printf("Enter file name > ");
            scanf("%s", fname);
    
            if((fptr = fopen(fname, "r")))  /* Open file and check success */
            {
                    printf("Enter screen size > ");
    
                    scanf("%d", &lines);
                    getchar();
    
                    fprintfile(fptr, lines);
    
                    fclose(fptr);
            }
            else
            {
                    fprintf(stderr, "Error opening file %s\n", fname);
            }
    
            return(0);
    }
    
    void fprintfile(FILE * fptr, int screenfulSize)
    {
            int lineNum = 1, ch;
            char previousCh = ' ';
    
            if((ch = fgetc(fptr)) != EOF)   /* Get first char and check for empty file */
            {
                    printf("\t%d  %c", lineNum, ch);        /* Print first line number and char */
    
                    while((ch = fgetc(fptr)) != EOF)
                    {
                            if(previousCh == '\n')
                                    if(screenfulSize != 0)  /* Prevent division by zero */
                                            if(lineNum % screenfulSize == 0)
                                            {
                                                    printf("\tPress enter to continue ");
                                                    while((getchar()) != '\n');    /* Skip passed any char's preceding the enter key */
                                            }
    
                                    printf("\t%d  ", ++lineNum);
    
                            putchar(ch);
    
                            previousCh = ch;
                    }
            }
    }





    code for question 3


    Code:
    * Task 3, sheet 1 */
    /* This opens a file, then prints each line starting with a line number */
    /* It will also display a certain number of lines at a time */
    /* And it can start at a certain line number */
    
    #include
    
    void make_line(FILE*, int, int);
    
    int main()
    {
            FILE * fptr;
            char filename[80];                                                        /*For the filename to go in*/
            int screenful, startline;
    
    
            printf("please input name of file. \n");
            scanf("%s", filename);
    
            if((fptr = fopen(filename, "r")) == NULL)
                    printf("error opening file");
            else
            {
                    printf("how many lines would you like displayed\t");
                    scanf("%d", &screenful);
                    getchar();        
                                                                                    /*Prompt and accept user input, and get rid of the return character*/
    
                    printf("what line number would you like the data to be displayed after\t");
                    scanf("%d", &startline);
                    getchar();
                                                                                    /*Prompt and accept user input, and get rid of the return character*/
    
                    make_line(fptr, screenful, startline);
                                                                                    /*calls the function*/
    
                    printf("\n");                                                        /*Puts the command line on the next line*/
    
                    fclose(fptr);                                                        /*closes the file*/
            }
    
    
    return 0;
    }

  2. #2
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    Code:
    char fname[20];
    
    printf("Enter file name > ");
    scanf("%s", fname);

    Enter file name > c:\file\from\a\location\longer\than\twenty.chars
    (enter)


    Solution: use MAX_PATH or some constant, not only 20 chars.
    Use fgets to limit the amount of chars that can be entered to this constant
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  3. #3
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,212
    I'm assuming this is c and not c++

    int main(void)

    and there is a case where you haven't put brackets around if blocks, therefore only the first statement will be ran, and by the indentation it appears that is not the desired result.
    Last edited by Brian; 10-21-2003 at 09:29 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 10-20-2008, 10:23 AM
  2. Need function to evaluate sequence content
    By gkoenig in forum C Programming
    Replies: 5
    Last Post: 03-30-2008, 05:18 PM
  3. Evaluate !(1 && !(0 || 1)).
    By Grumpy_Old_Man in forum C++ Programming
    Replies: 7
    Last Post: 08-18-2003, 01:28 PM
  4. Help! Program to evaluate expression...
    By Unregistered in forum C++ Programming
    Replies: 7
    Last Post: 02-19-2002, 06:20 AM
  5. does not evaluate to a function - anyone know why?
    By rc7j in forum C++ Programming
    Replies: 5
    Last Post: 11-05-2001, 10:34 AM