PDA

View Full Version : Evaluate me...



vasanth
10-21-2003, 03:14 AM
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



#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




* 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;
}

nvoigt
10-21-2003, 05:23 AM
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 :)

Brian
10-21-2003, 09:26 AM
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.