P.Lecture - 2.28.08 - Announcements Lecture 2/28 Assignment...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
1 Announcements Lecture 2/28 • Assignment 4 – Designs due on Tuesday in class • Topics for today – More File IO (Chapter 22) • Formatted file IO • Binary file operations • Random access – Command line arguments – Introduction to Recursion (time permitting) Your DecMack Emulator Your Real Computer DecMack Emulator Program Emulation Result and Execution Profile table DecMack Machine language program (from a text file on disk) Input file: 1 299 492 495 399 492 495 399 283 279 689 078 100 000 000 000 Command-Line Arguments • The main function may have parameters named argc and argv, which allow access to the command line when the program is executed: int main( int argc , char * argv [ ] ) { … } argc is a count of the number of command line arguments (including the name of the program itself). argv is an array of pointers to the command line arguments, each of which is a string. – argv[0] contains a pointer to the name of the program. – argv[1] through argv[argc-1] point to the remaining command line arguments Command Line Arguments /* myProg.c */ int main ( int argc , char *argv[ ] ) { . .. } > myProg one two three argc = 4 argv[0] = "myProg" argv[1] = "one" argv[2] = "two" argv[3] = "three” Example #include <stdio.h> #include <stdlib.h> int main(int argc, char *argv[ ]) { FILE *fp; if (argc < 2) { printf("usage: filename not given\n"); exit(EXIT_FAILURE); } fp = fopen(argv[1], "r"); if ((fp == NULL) { printf(”file %s cannot be opened\n", argv[1]); exit(EXIT_FAILURE); } else { printf(”the file %s exists\n", argv[1]); /* do stuff with the file */ fclose(fp); } return 0; } The following program (exist.c) obtains a file name from the command line and checks whether the file can be opened: > exist example.txt Output Many Lines to a File #include <stdio.h> #include <stdlib.h> #include <string.h> int main(int argc, char *argv[]) { FILE *fp; char str[80]; /* check for command line arg in here */ /* open file for output */ fp = fopen(argv[1], "w")); if((fp == NULL) { printf("Cannot open file.\n"); exit(1); } printf("Enter a blank line to stop.\n"); do { printf(": "); gets(str); strcat(str, "\n"); /* add newline */ if(*str != '\n') { fputs(str, fp); } } while(*str != '\n'); /* Now read them back in */ fclose(fp); /* open the same file for input */ fp = fopen(argv[1], "r")); if( fp == NULL) { printf("Cannot open file.\n"); exit(1); } /* read back the lines in the file */ do { fgets(str, 79, fp); if(!feof(fp)) { printf(str); } } while(!feof(fp)); fclose(fp); return 0; }
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 Formatted File IO #include <stdio.h> #include <stdlib.h> #include <string.h> int main( ) { FILE *fp; float ld;
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 03/22/2008 for the course EE 312 taught by Professor Shafer during the Spring '08 term at University of Texas at Austin.

Page1 / 4

P.Lecture - 2.28.08 - Announcements Lecture 2/28 Assignment...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online