Created
November 9, 2012 14:07
-
-
Save alllex/4045849 to your computer and use it in GitHub Desktop.
Program outputs last lines of input file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Math-Mech 2012-171 | |
Semin A | |
Tail | |
*/ | |
#include <stdio.h> | |
#include <malloc.h> | |
#include "logList.h" | |
#include <stdlib.h> | |
void printHelp() | |
{ | |
fprintf(stderr, "HELP:\nYou can start this program with several parameters\n\ | |
At first, you can set input file else lines will be read from console\n\ | |
For this you need to write file name like 'head.exe input.txt'\n\ | |
And you can chose count of printing lines\n\ | |
In this situation write special pointer \"-n\"\n\ | |
Next input a number - count of reading and printing lines\n\ | |
Example1: head.exe input.txt -n 7\n\ | |
Example2: head.exe -n 20 main.c\n\ | |
Example3: head.exe -n 7\n\ | |
Example4: head.exe somefile.txt\n"); | |
} | |
void alertExit(int const ErrorCode) | |
{ | |
switch (ErrorCode) | |
{ | |
case FILE_NOT_FOUND: | |
{ | |
fprintf(stderr, "Error: file not found...\n"); | |
} | |
break; | |
case INCORRECT_DIRECTIVE: | |
{ | |
fprintf(stderr, "Error: some parameters isn't correct...\n"); | |
} | |
break; | |
case INCORRECT_NUMBER: | |
{ | |
fprintf(stderr, "Error: incorrect number...\n"); | |
} | |
break; | |
default: | |
{ | |
fprintf(stderr, "Unknown error...\n"); | |
} | |
break; | |
} | |
printHelp(); | |
} | |
int getNumber(char *arg) | |
{ | |
int i = 0; | |
int x = 0; | |
int digit = 0; | |
//fprintf(stderr, "number as string = %s\n", arg); | |
while (*(arg + i) != '\0') | |
{ | |
digit = *(arg + i) - '0'; | |
if (digit < 0 || digit > 9) | |
{ | |
//fprintf(stderr, "incorrect digit %d\n", digit); | |
alertExit(INCORRECT_NUMBER); | |
return INCORRECT_NUMBER; | |
} | |
x = x * 10 + digit; | |
i++; | |
} | |
return x; | |
} | |
int setParams(int argc, char** argv, int *countLines, FILE **in, FILE **out) | |
{ | |
int i = 0; | |
char *temp; | |
for (i = 1; i < argc; i++) | |
{ | |
temp = *(argv + i); | |
if (*(temp) == '-') | |
{ | |
//fprintf(stderr, "Some directive\n"); | |
if (*(temp + 1) == 'n') | |
{ | |
//fprintf(stderr, "Is number, try to read\n"); | |
if (++i == argc) | |
{ | |
alertExit(INCORRECT_DIRECTIVE); | |
return false; | |
} | |
temp = *(argv + i); | |
*countLines = getNumber(temp); | |
if (*countLines == INCORRECT_NUMBER) | |
{ | |
return false; | |
} | |
continue; | |
} | |
else | |
{ | |
alertExit(INCORRECT_DIRECTIVE); | |
return false; | |
} | |
} | |
else | |
{ | |
//fprintf(stderr, "File name, try to open\n"); | |
if ((*in = fopen(temp, "r")) == NULL) | |
{ | |
alertExit(FILE_NOT_FOUND); | |
return false; | |
} | |
} | |
} | |
return true; | |
} | |
void printParams(int argc, char **argv) | |
{ | |
int i = 0; | |
char *temp; | |
printf("Params:\n"); | |
for (i = 1; i < argc; i++) | |
{ | |
temp = *(argv + i); | |
printf("%s\n", temp); | |
} | |
printf("End of params\n"); | |
} | |
char *getString(FILE *input) | |
{ | |
int countChars = 0; | |
char *string = (char *) realloc(NULL, sizeof(char)); | |
char c = '0'; | |
while (!feof(input) && ((c = fgetc(input)) != '\n')) | |
{ | |
string = realloc(string, (++countChars) * sizeof(char)); | |
string[countChars - 1] = c; | |
} | |
if (feof(input)) | |
{ | |
string[countChars - 1] = '\0'; | |
} | |
else | |
{ | |
string[countChars] = '\0'; | |
} | |
return string; | |
} | |
void writeLines(int const countLines, FILE *input, FILE *output) | |
{ | |
int countAllLines = 0; | |
char **lines = (char **) malloc(countLines * sizeof(char *)); | |
int pointer = 0; | |
int i = 0; | |
for (countAllLines = 0; !feof(input); countAllLines++) | |
{ | |
lines[pointer] = getString(input); | |
pointer = (pointer + 1) % countLines; | |
} | |
if (countAllLines > countLines) | |
{ | |
countAllLines = countLines; | |
} | |
pointer %= countAllLines; | |
for (i = 1; i <= countAllLines; i++) | |
{ | |
fprintf(output, "%s\n", lines[pointer]); | |
pointer = (pointer + 1) % countAllLines; | |
} | |
free(lines); | |
} | |
int main(int argc, char **argv) | |
{ | |
int const defaultCountLines = 3; | |
const FILE *defaultInput = stdin; | |
const FILE *defaultOutput = stdout; | |
int countLines = defaultCountLines; | |
FILE *input = (FILE *) defaultInput; | |
FILE *output = (FILE *) defaultOutput; | |
if (setParams(argc, argv, &countLines, &input, &output)) | |
{ | |
writeLines(countLines, input, output); | |
fclose(input); | |
return 0; | |
} | |
else | |
{ | |
fclose(input); | |
return 1; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment