170 likes | 684 Views
Command Line arguments. Main function: int main(argc, char *argv[]) argc is the count of command line arguments argv is an array of strings argv[0] is the name of the program argv[1] is the first argument from the command line argv[k] is the k th argument from the command line
E N D
Command Line arguments • Main function: int main(argc, char *argv[]) • argc is the count of command line arguments • argv is an array of strings • argv[0] is the name of the program • argv[1] is the first argument from the command line • argv[k] is the kth argument from the command line • Command line example: >prog 123 abc • argc = 3 • argv[0] = "prog", argv[1] = "123", argv[2] = "abc" • If you expect two arguments:if (argc!=3) { printf("usage: prog <arg1> <arg2>); exit(1); }
Inputting Problem • scanf – User can crash the program by typing too much • fgets – User cannot crash the program • Example #include<stdio.h> int main(void) { char input[10+1]; char *result; printf("Enter some text:\n"); result = fgets(input, sizeof(input), stdin); if (result != NULL) printf("You entered: %s", result); else printf("Error processing fgets()\n"); return 0; }
typedef • typedef assigns a label to a declaration • Syntax: typedef <some type> label; • Then you can declare using the label • Examples: • Declare a new typetypedef char * String;typedef int Boolean; • Declare an instance of that typevoid myFunc(String data, Boolean flag)
struct • struct is a collection of data types • Example:struct{ int age; char name[100 + 1]; float salary;} • Useful to create a new complex typetypedef struct { int age; char name[100+1]; float salary; } Person • Instantiate: Person bill; Note: A complex type is like Java class, but without methods
Accessing struct data • Declare: Person person[100]; • Access fields: JLJprintf("His name is %s\n", person[0].name);for (int i=0; i<100; i++) printf("%s is %d years old with salary %f\n" , person[i].name , person[i].age , person[i].salary);
Using pointers for functions void myFunc(const Person first, Person *second) { second->name = first.name; second->age = first.age; second->salary = first.salary; } • Note: The arrow indirectly addresses access data in structs • Note: Pass by value copies the entire structure to the stack. This can be inefficient, so most programmers always use pointers in functions
Reading a text file #include <stdio.h> int main(void) { char data[256]; FILE *file; file = fopen("someFile.txt","r"); if (file==NULL) return 2; while (!feof(file)) { fgets(data,sizeof(data),file); printf("%s",data); } fclose(file); return 0; }
Writing to a text file #include <stdio.h> int main() { FILE *file; char data[256+1]; fgets(data, sizeof(data), stdin); file = fopen("someFile.txt", "w"); if(file==NULL) { fprintf(stderr, "Can't create output file\n"); return 2; } fprintf(file, "%s\n", data); fclose(file); return 0; } Question: Why not use printf instead of fprintf with stderr
fscanf and fgets to read files fscanf(File *fp, const char *temp, args … ) • Works just like scanf but is insecure • Stops on white space • No protection for buffer size • Better approach • Use fgets: reads till newline or n-1 characters read • Automatically puts the null at the end of the string • Returns null if error or end of file • If you have to parse, use sscanf after the data is in memory
sscanf • Example:char str[] = "30 20 10";int a, b, c;if (sscanf(str, "%d %d %d", &a, &b, &c) != 3) fprintf(stderr, "What happened?"); • sscanf is a very useful tool for parsing input or data read from files • Note: the strstr function is useful for finding the first occurrence of a little string in a big one. See lab 10.char *strstr (const char *haystack, const char *needle);
Buffered Output • Data does NOT go dirctly to a file • Why? It is more efficient for the operating system to decide when to write data. • What happens? Data goes to an operating system buffer • When does the data write? When the buffer is full or manually flushed • How does it manually get flushed? Use the call fflush(file); • What about stdout? call fflush(stdout)
Final Notes Portability • Operating Systems end lines differently • C always uses '\n' to end a line • stdio functions translate for the OS in question EOF: stdio functions use this constant #include < stdio.h> void main() { int c, nc = 0; while ( (c = getchar()) != EOF ) nc++; printf("Characters in file = %d\n",nc); } • GDB: The set args command is useful before running the program. It sets the command line arguments as if you typed them from the command line