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.

Slides:



Advertisements
Similar presentations
The debugger Some programs may compile correctly, yet not produce the desirable results. These programs are valid and correct C programs, yet not the programs.
Advertisements

1 Chapter 10 Strings and Pointers. 2 Introduction  String Constant  Example: printf(“Hello”); “Hello” : a string constant oA string constant is a series.
Lecture 20 Arrays and Strings
Chapter 7 Process Environment Chien-Chung Shen CIS, UD
Exercise 4 1. Write a program that simulates coin tossing. For each toss of the coin the program should print Heads or Tails. Let the program toss the.
Chapter 10.
1 Homework Turn in HW2 at start of next class. Starting Chapter 2 K&R. Read ahead. HW3 is on line. –Due: class 9, but a lot to do! –You may want to get.
Command-line arguments CS 201 Fundamental Structures of Computer Science.
Programming Review: Functions, pointers and strings.
What does this program do ? #include int main(int argc, char* argv[]) { int i; printf("%d arguments\n", argc); for(i = 0; i < argc; i++) printf(" %d: %s\n",
C Lecture Notes Functions (Cont...). C Lecture Notes 5.8Calling Functions: Call by Value and Call by Reference Used when invoking functions Call by value.
Exercise 10 Review: pointers, strings and recursion.
C Programming Strings. Array of characters – most common type of array in C  Let’s make them easier for use Denote the end of array using a special character.
CS1061 C Programming Lecture 15: More on Characters and Strings A. O’Riordan, 2004.
C Programming Day 2 based upon Practical C Programming by Steve Oualline CS550 Operating Systems.
Command line arguments. – main can take two arguments conventionally called argc and argv. – Information regarding command line arguments are passed to.
CMSC 104, Version 8/061L18Functions1.ppt Functions, Part 1 of 4 Topics Using Predefined Functions Programmer-Defined Functions Using Input Parameters Function.
Craps!. Example: A Game of Chance Craps simulator Rules – Roll two dice 7 or 11 on first throw, player wins 2, 3, or 12 on first throw, player loses 4,
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 15: Linked data structures.
A First Book of ANSI C Fourth Edition Chapter 6 Modularity Using Functions: Part I.
CNG 140 C Programming (Lecture set 9) Spring Chapter 9 Character Strings.
File Handling Spring 2013Programming and Data Structure1.
0 Chap. 5 Pointers and Arrays 5.3Pointers and Arrays 5.4Address Arithmetic 5.5Character Pointers and Functions 5.6Pointer Arrays; Pointers to Pointers.
CMSC 1041 Functions II Functions that return a value.
Characters and Strings File Processing Exercise C Programming:Part 3.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Command Line Arguments.
File IO and command line input CSE 2451 Rong Shi.
CSE 232: C++ debugging in Visual Studio and emacs C++ Debugging (in Visual Studio and emacs) We’ve looked at programs from a text-based mode –Shell commands.
 2008 Pearson Education, Inc. All rights reserved Case Study: Random Number Generation C++ Standard Library function rand – Introduces the element.
File I/O, Project 1: List ADT Bryce Boe 2013/07/02 CS24, Summer 2013 C.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Functions (Header files and Library Functions) Outline.
1 Command-Line Processing In many operating systems, command-line options are allowed to input parameters to the program SomeProgram Param1 Param2 Param3.
CS221 Random Numbers. Random numbers are often very important in programming Suppose you are writing a program to play the game of roulette The numbers.
1 File Handling. 2 Storage seen so far All variables stored in memory Problem: the contents of memory are wiped out when the computer is powered off Example:
Pointers A pointer is a variable that contains a memory address as it’s value. The memory address points to the actual data. –A pointer is an indirect.
Weeks 5-6 Pointers and Arrays Basic pointer type Pointers and Arrays Address arithmetic Pointer Arrays User-defined data types Structures Unions Pointers.
Memory Layout, File I/O Bryce Boe 2013/06/27 CS24, Summer 2013 C.
Introduction to C Programming Lecture 6. Functions – Call by value – Call by reference Arrays Today's Lecture Includes.
CSE 232: C++ memory management Overview of Arrays Arrays are the simplest kind of data structure –One item right after another in memory (“contiguous range”
Strings Programming Applications. Strings in C C stores a string in a block of memory. The string is terminated by the \0 character:
Cryptography.
A First Book of ANSI C Fourth Edition Chapter 6 Modularity Using Functions: Part I.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Recursion.
Computer Programming for Engineers
Today’s Material Strings Definition Representation Initialization
Function Call Stack and Activation Frame Stack Just like a pile of dishes Support Two operations push() pop() LIFO (Last-In, First-Out) data structure.
CSE202: Lecture 13The Ohio State University1 Function Scope.
Connecting to Files In order to read or write to a file, we need to make a connection to it. There are several functions for doing this. fopen() – makes.
1 Chapter 7 Pointers and C-Strings. 2 Objectives  To describe what a pointer is (§7.1).  To learn how to declare a pointer and assign a value to it.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 230 Characters and Strings Dale Roberts, Lecturer Computer Science,
C Programming Day 2. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Union –mechanism to create user defined data types.
13. Strings. String Literals String literals are enclosed in double quotes: "Put a disk in drive A, then press any key to continue\n“ A string literal.
Chapter 5 Pointers and Arrays Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh.
MR. CRONE Generating Random Numbers. Random Numbers Many programs require the computer to generate random numbers Random numbers are used in many applications.
Chapter 7 Process Environment Chien-Chung Shen CIS/UD
LINKED LISTS.
Functions, Part 2 of 2 Topics Functions That Return a Value
Command Line Arguments
Command line arguments
Command Line Arguments
Number guessing game Pick a random number between 1 and 10
Programmazione I a.a. 2017/2018.
Computer Science 210 Computer Organization
CS111 Computer Programming
Chapter 5 - Functions Outline 5.1 Introduction
Computer Science 210 Computer Organization
Pointers Department of Computer Science-BGU יום רביעי 21 נובמבר 2018.
sscanf()- string scan function
File Handling.
A First Book of ANSI C Fourth Edition
Presentation transcript:

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);

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

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

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

Example discreet_grades.c

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

‘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

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; }

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

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 Must #include

Yet another example nth_line.c

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

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

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 )

Example Die_throw.c

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

solution n_coin_toss.c