Download code

Jump to: navigation, search

Back to Basic_constructs_(C)

Download for Windows: single file, zip

Download for UNIX: single file, zip, tar.gz, tar.bz2

build.log

1 /tmp/litprog7946784/basic_constructs.c: In function 'io':
2 /tmp/litprog7946784/basic_constructs.c:63:7: warning: ignoring return value of 'fgets', declared with attribute warn_unused_result [-Wunused-result]


basic_constructs.c

  1 /* The authors of this work have released all rights to it and placed it
  2 in the public domain under the Creative Commons CC0 1.0 waiver
  3 (http://creativecommons.org/publicdomain/zero/1.0/).
  4 
  5 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  6 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  7 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  8 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  9 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 10 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 11 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 12 
 13 Retrieved from: http://en.literateprograms.org/Basic_constructs_(C)?oldid=18641
 14 */
 15 
 16 #include<stdio.h>
 17 
 18 #include<stdio.h>
 19 #include<string.h>
 20 #include<stdlib.h>
 21 
 22 void loops()
 23 {
 24 	int n;
 25 
 26 	for(n=0; n<10; ++n) {
 27 		printf("%d\n", n);
 28 	}
 29 
 30 
 31 	n=0;
 32 	while(n<10) {
 33 		printf("%d\n", n);
 34 		++n;
 35 	}
 36 
 37 	n=0;
 38 	do {
 39 		printf("%d\n", n);
 40 		++n;
 41 	} while(n<10);
 42 
 43 }
 44 
 45 
 46 void io()
 47 {
 48 	FILE *fp;
 49 	char line[80];
 50 	int ch;
 51 
 52 	while((ch=getchar())!=EOF) {
 53 		putchar(ch);
 54 	}
 55 
 56 
 57 	if((fp=fopen("input.txt", "r"))==NULL) {
 58 		fprintf(stderr, "Cannot read \"input.txt\"\n");
 59 		return;
 60 	}
 61 
 62 	line[79]='\0';
 63 	fgets(line, 80, fp);
 64 
 65 	fclose(fp);
 66 
 67 	if((fp=fopen("output.txt", "w"))==NULL) {
 68 		fprintf(stderr, "Cannot write \"output.txt\"\n");
 69 		return;
 70 	}
 71 
 72 	fputs(line, fp);
 73 
 74 	fclose(fp);
 75 
 76 }
 77 
 78 
 79 void usage(const char *cmd)
 80 {
 81 	fprintf(stderr, "Usage %s loops|io\n", cmd);
 82 	exit(1);
 83 }
 84 
 85 int main(int argc, char *argv[])
 86 {
 87 	if(argc<2) {
 88 		fprintf(stderr, "ERROR: Wrong number of arguments\n");
 89 		usage(argv[0]);
 90 		return 1;
 91 	}
 92 
 93 	if(!strcmp(argv[1], "loops")) loops();
 94 	else if(!strcmp(argv[1], "io")) io();
 95 	else {
 96 		fprintf(stderr, "ERROR: Unknown command: \"%s\"\n", argv[1]);
 97 		usage(argv[0]);
 98 	}
 99 
100 	return 0;
101 }


hijacker
hijacker
hijacker
hijacker