CS1121_Spring_2011_Lecture9_BB

CS1121_Spring_2011_Lecture9_BB - CS1121 Introduction to C...

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

View Full Document Right Arrow Icon
CS1121 Introduction to C Computing Lecture 9 Continue OVERVIEW of C
Background image of page 1

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

View Full DocumentRight Arrow Icon
Outline Command line arguments review Structures in C
Background image of page 2
Command line arguments By convention, main is called with two arguments int main(int argc, char *argv[]) argc (‘argument count’) is the number of command- line arguments, including the program name argv (‘argument vector’) is an array of strings, one for each argument
Background image of page 3

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

View Full DocumentRight Arrow Icon
4 C Interactive Mode /* Converts distances from miles to kilometers. */ #include <stdio.h> /* printf, scanf definitions */ #define KMS_PER_MILE 1.609 /* conversion constant */ Int main(void) { double miles, /* distance in miles */ kms; /* equivalent distance in kilometers*/ scanf("%lf", &miles); /* Get and echo the distance in miles */ printf("The distance in miles is %.2f \n", miles); kms = KMS_PER_MILE * miles; /*Converts Interactive Mode: If you run this progra miles, if you entered The di 112.00 That e kilome
Background image of page 4
5 Program-Controlled Input and Output Files C allows a program to name an input data file First, declare a file pointer variable of type FILE * FILE *inp, /* pointer to input file */ *outp; /* pointer to output file */ Open the required file by its name, use “ r “ mode to read from it, and use “ w “ mode to write to it. Note on location of files and executable program. inp = fopen(“distance.dat", "r"); outp = fopen(“distance.out", "w"); Read data from the opened input file, and write output to the opened output file fscanf(inp, "%lf", &miles); fprintf(outp, "The distance in miles is %.2f.\n", miles); Close the opened file(s) fclose(inp); fclose(outp);
Background image of page 5

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

View Full DocumentRight Arrow Icon
#include <stdio.h> #define KMS_PER_MILE 1.609 /* conversion constant */ int main(void) { double miles, /* distance in miles */ kms; /* equivalent distance in kilometers */ FILE *inp, /* pointer to input file */ *outp; /* pointer to output file */ inp = fopen("distance.dat", "r"); outp = fopen("distance.out", "w"); fscanf(inp, "%lf", &miles); fprintf(outp, "The distance in miles is %.2f.\n", miles); kms = KMS_PER_MILE * miles; /* Convert distance to kms */ /* Display the distance in kilometers. */ fprintf(outp, "That equals %.2f kilometers.\n", kms); /* distance in kms */ /* Close files. */ fclose(inp); fclose(outp); return (0); } Program-Controlled Input a
Background image of page 6
#define KMS_PER_MILE 1.609 /* conversion constant */ int main(void) { double miles, /* distance in miles */ kms; /* equivalent distance in kms */ FILE *inp, /* pointer to input file */ *outp; /* pointer to output file */ inp = fopen("distance.dat", "r"); outp = fopen("distance.out", "w"); fscanf(inp, "%lf", &miles); fprintf(outp, "The distance in miles is %.2f.\n", miles); kms = KMS_PER_MILE * miles; /* Convert distance to kms */ /* Display the distance in kilometers. */ fprintf(outp, "That equals %.2f kilometers.\n", kms); /* distance in kms */ /* Close files. */ fclose(inp); fclose(outp); return (0);
Background image of page 7

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

View Full DocumentRight Arrow Icon
Image of page 8
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 11/14/2011 for the course CSCI 1121 taught by Professor Haya during the Fall '11 term at GWU.

Page1 / 37

CS1121_Spring_2011_Lecture9_BB - CS1121 Introduction to C...

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

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