Presentation is loading. Please wait.

Presentation is loading. Please wait.

A file reminder Files are used to store data and information They are manipulated through a pointer to FILE (FILE *) This pointer is returned when opening.

Similar presentations


Presentation on theme: "A file reminder Files are used to store data and information They are manipulated through a pointer to FILE (FILE *) This pointer is returned when opening."— Presentation transcript:

1 A file reminder Files are used to store data and information They are manipulated through a pointer to FILE (FILE *) This pointer is returned when opening the file – FILE *fopen(file_name, mode);

2 A file reminder A file must be closed when we don’t need it anymore void fclose(FILE *fp); Several functions exist that allow input from and output to file – fprintf/fscanf fgetc/fputc

3 Reading entire lines char *fgets(char s[], int n, FILE *fp); Sometimes its desired to read from a file whole lines at a time fgets reads at most n-1 letters into s[], stopping if a newline (‘\n’) is encountered (the newline is also read into s) The return value of fgets is either s, or NULL if there are no more lines to read

4 Important string functions char *strchr(char *s, char c) Returns a pointer to the first occurrence of c in s char *strrchr(char *s, char c) Returns a pointer to the last occurrence of c in s char *strstr(char *s1, char *s2) Returns a pointer to the first occurrence of s2 in s1

5 Example discreet_grades.c

6 Command line arguments Command line arguments are arguments for the main function Recall that main is basically a function It can receive arguments like other functions The ‘calling function’ in this case is the operating system, or another program

7 ‘main’ prototype int main(int argc, char *argv[]) When we want main to accept command line arguments, we must define it like this argc holds the number of arguments that were entered by the caller argv is an array of pointers to char – an array of strings – holding the text values of the arguments The first argument is always the program’s name

8 Example /* This program displays its command-line arguments */ #include int main(int argc, char *argv[]) { int i; printf("The program's command line arguments are: \n"); for (i=0; i<argc; i++) printf("%s\n", argv[i]); return 0; }

9 Specifying the arguments We can specify to the Visual Studio compiler what command line arguments we want to pass to our program Project->settings->debug, in the ‘program arguments’ field

10 Helper functions – atoi/atof int atoi(char s[]); double atof(char s[]); Command line arguments are received in the form of strings These functions are used when we want to transform them into numbers For example – atof(“13.5”) returns the number 13.5. Must #include

11 Yet another example nth_line.c

12 Exercise Write a program that accepts two numbers as command line arguments, representing a rectangle’s height and width. The program should display the rectangle’s area and circumference

13 Random numbers Some applications require using random numbers Computer games - Card games, raffles, adventure and strategy games etc. Some cryptography stuff… int rand(void); Returns a pseudo-random number from 0 to RAND_MAX, which is at least 32,767 Declared in stdlib.h

14 The seed of violence The numbers returns by rand are determined by the random-number generator’s ‘seed’ To change the seed, use - void srand(unsigned int seed); (defined in stdlib.h) To truly randomize things, call – srand(time(NULL)); (and don’t forget to #include )

15 Example Die_throw.c

16 Exercise Write a function that accepts as parameter an integer N, and returns the number of heads in a simulated toss of N coins. Write a program that accepts the number of throws to simulate from the user, then uses this function to display the distribution of N- coin tosses Comment: N should be defined as a constant

17 solution n_coin_toss.c


Download ppt "A file reminder Files are used to store data and information They are manipulated through a pointer to FILE (FILE *) This pointer is returned when opening."

Similar presentations


Ads by Google