Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 CSSE 332 Structures, Command Line Arguments. 2 Multi-dimensional arrays Multi-dimensional arrays int points[3][4]; points [1][3] = 12; /* NOT points[3,4]

Similar presentations


Presentation on theme: "1 CSSE 332 Structures, Command Line Arguments. 2 Multi-dimensional arrays Multi-dimensional arrays int points[3][4]; points [1][3] = 12; /* NOT points[3,4]"— Presentation transcript:

1 1 CSSE 332 Structures, Command Line Arguments

2 2 Multi-dimensional arrays Multi-dimensional arrays int points[3][4]; points [1][3] = 12; /* NOT points[3,4] */ printf(“%d”, points[1][3]); Multi-dimensional Arrays

3 3 Example 6: #include struct birthday{ int month; int day; int year; }; //Note the semi-colon int main() { struct birthday mybday;/* - ‘new’ not needed ! */ /* otherwise, it’s just like Java ! */ mybday.day=1; mybday.month=1; mybday.year=1977; printf(“I was born on %d/%d/%d\n”, mybday.day, mybday.month, mybday.year); } Structures Approximation of Java’s classes with only data encapsulation (no methods) Approximation of Java’s classes with only data encapsulation (no methods)

4 4 More on Structures struct person{ char name[41]; int age; float height; struct { /* embedded structure */ int month; int day; int year; } birth; }; struct person me; me.birth.year=1977;……… /* array of info about everyone in class */ struct person class[60]; class[0].height=5.5; class[0].birth.year=1971;……

5 5 typedef typedef struct person myPerson typedef struct person myPerson –Defines a new type name myPerson as a synonym for type struct person int main(){ myPerson me; me.age = 6; …}

6 6 User-defined header files Structures and other data structures may be defined in a header file, for better organization of the code. Structures and other data structures may be defined in a header file, for better organization of the code. These are user-defined header files e.g. person.h These are user-defined header files e.g. person.h To include it: To include it: #include “person.h’’ at the start of the C source file.

7 7 Command line arguments Accept inputs through the command line. Accept inputs through the command line. main(int argc, char* argv[]) main(int argc, char* argv[]) –argc – argument count –argv[] – value of each argument

8 8 Example 7 #include #include int main(int argc, char *argv[]){ int count = 0; int count = 0; if(argc < 2){ if(argc < 2){ printf("Must enter at least one argument\n"); printf("Must enter at least one argument\n"); printf("Example:./a.out this is program 7\n"); printf("Example:./a.out this is program 7\n"); exit(1); exit(1); } printf(" The number of arguments is %d\n", argc); printf(" The number of arguments is %d\n", argc); printf("And they are :\n"); printf("And they are :\n"); while(count < argc){ while(count < argc){ printf("argv[%d]: %s\n",count,argv[count] ); printf("argv[%d]: %s\n",count,argv[count] ); count++; count++; } printf("\n"); printf("\n"); return 0; return 0;}

9 9 File handling Open a file using “fopen” Open a file using “fopen” Returns a FILE pointer which is used to access the file. Returns a FILE pointer which is used to access the file. Modes: Modes: –Read(r) – Opens file for reading. Returns error (NULL) if file does not exist. –Write(w) – create a new file for writing (overwrite old one). File pointer at the beginning of file. – Append(a) – create a new file if file does not exist. Preserve the contents if file does exist and pointer at the end of the file. fprintf, fscanf, fclose fprintf, fscanf, fclose

10 10 Example 8 #include int main(int argc, char *argv[]){ /* Declare a file pointer */ FILE *inFile=NULL; /* open file for writing*/ inFile = fopen(“test.txt”, “w”); /* need to do explicit ERROR CHECKING */ if(inFile == NULL){ exit(1); } /* write some data into the file */ fprintf(inFile, “Hello there\n”); /* don’t forget to release file pointer */ fclose(inFile); return 0; }

11 11 Reading till end of file int feof(FILE *) – The function is defined in stdio.h int feof(FILE *) – The function is defined in stdio.h –Returns a non-zero value (TRUE) if end of file has been reached, and zero otherwise. Sample code: fscanf(inFile, "%d", &int1); // Try to read while (feof(inFile) == 0 ){ //If there is data, enter loop while (feof(inFile) == 0 ){ //If there is data, enter loop printf("%d \n", int1); //Do something with the data printf("%d \n", int1); //Do something with the data fscanf(inFile, "%d", &int1); //Try reading again } //go back to while to test if data was read

12 Homework 3 Another exercise to get started on C. Another exercise to get started on C. Available on class’s Angel page Available on class’s Angel page – Homework3.html 12


Download ppt "1 CSSE 332 Structures, Command Line Arguments. 2 Multi-dimensional arrays Multi-dimensional arrays int points[3][4]; points [1][3] = 12; /* NOT points[3,4]"

Similar presentations


Ads by Google