Skip to content

Instantly share code, notes, and snippets.

@newsomc
Last active December 12, 2015 12:39
Show Gist options
  • Save newsomc/4773189 to your computer and use it in GitHub Desktop.
Save newsomc/4773189 to your computer and use it in GitHub Desktop.
# include <stdio.h>
# define MAXLINE 1000
int cgetl(char line[], int maxline, FILE * file);
void copy(char to[], char from[]);
int main () {
int len;
int max;
char line[MAXLINE];
char longest[MAXLINE];
FILE * pFile;
pFile=fopen("t.txt", "r");
max = 0;
while((len = cgetl(line, MAXLINE, pFile)) > 0){
if (len > max){
max = len;
copy(longest, line);
}
}
if (max > 0) {
printf("%s", longest);
}
return 0;
}
int cgetl (char s[], int lim, FILE * f) {
int c, i;
for (i=0; i < lim - 1 && (c=fgetc(f)) != EOF && c!='\n'; ++i) {
s[i] = c;
}
if (c == '\n'){
s[i] = c;
++i;
}
s[i] = '\0';
return i;
}
void copy (char to[], char from[]) {
int i;
i = 0;
while((to[i] = from[i]) != '\0' ){
++i;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment