-
-
Save untodesu/5c0d60a7f7f622dbbe9bdb3660f11254 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#include <stdio.h> | |
#include <string.h> | |
int main(int argc, char **argv) | |
{ | |
FILE *ifp = NULL; | |
FILE *ofp = NULL; | |
char wrd[40] = { 0 }; | |
char line[256] = { 0 }; | |
if(argc > 1 && !strcmp(argv[1], "stdio")) { | |
/* Use standard input/output streams | |
* to read/write information. This is | |
* useful when we want to use redirections | |
* that are commonly used in UNIX environments. */ | |
ifp = stdin; | |
ofp = stdout; | |
} | |
else { | |
/* Open files for reading and writing. If one | |
* of them fails to open, we return 1 indicating | |
* an unexpected end of the program's execution. */ | |
ifp = fopen("in.txt", "r"); | |
ofp = fopen("out.txt", "w"); | |
if(!ifp || !ofp) | |
return 1; | |
} | |
/* Go through the input file line by line */ | |
while(fgets(line, sizeof(line), ifp)) { | |
/* This sscanf call is just to ensure that a string | |
* has less than five words. The sscanf function returns | |
* the amount of items it has filled; we can abuse this | |
* mechanic and test whether there are five words or not. */ | |
if(sscanf(line, "%39s %39s %39s %39s %39s", wrd, wrd, wrd, wrd, wrd) < 5) { | |
/* Print the line to the output file | |
* and go on with the next one. */ | |
fputs(line, ofp); | |
continue; | |
} | |
} | |
/* Close files. */ | |
fclose(ofp); | |
fclose(ifp); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment