Skip to content

Instantly share code, notes, and snippets.

@isRuslan
Created August 16, 2016 20:56
Show Gist options
  • Save isRuslan/12d8c87957f54c0786470bda0d29e91a to your computer and use it in GitHub Desktop.
Save isRuslan/12d8c87957f54c0786470bda0d29e91a to your computer and use it in GitHub Desktop.
diff(1) first diff line
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#define MAXLINE 100
char *fgets(char *line, int maxline, FILE *fp);
void diff_line(char *a, char *b){
if(strcmp(a, b) < 0 || strcmp(a, b) > 0){
fprintf(stderr, "%s%s\n", a, b);
}
}
int main(int argc, char *argv[])
{
FILE *fa, *fb;
char fa_line[MAXLINE], fb_line[MAXLINE];
if(argc < 3){
fprintf(stderr, "invalid arguments %s \n", *argv);
exit(1);
}
if((fa = fopen(argv[1], "r")) == NULL){
fprintf(stderr, "cannot open %s", argv[1]);
exit(1);
}
if((fb = fopen(argv[2], "r")) == NULL){
fprintf(stderr, "cannot open %s", argv[2]);
exit(1);
}
while((fgets(fa_line, MAXLINE, fa) != NULL)
&& (fgets(fb_line, MAXLINE, fb) != NULL)){
diff_line(fa_line, fb_line);
}
}
char *fgets(char *line, int maxline, FILE *fp)
{
register int c;
register char *cs;
cs = line;
while(--maxline > 0 && (c = getc(fp)) != EOF){
if((*cs++ = c) == '\n'){
break;
}
}
*cs = '\0';
return (c == EOF && cs == line) ? NULL : line;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment