Introduction to C Programming CE00312-1 Lecture 15 Files and Program Parameters.

Slides:



Advertisements
Similar presentations
Lecture 3 Some commonly used C programming tricks. The system command Project No. 1: A warm-up project.
Advertisements

Chapter 7 Process Environment Chien-Chung Shen CIS, UD
UNIT 12 UNIX I/O Redirection.
The Environment of a UNIX Process. Introduction How is main() called? How are arguments passed? Memory layout? Memory allocation? Environment variables.
1 DOS vs. UNIX files Ending lines with “\r\n” vs. “\n” Reading an entire line at a time getline() To skip white space or not cin >> ch; vs. ch = cin.get();
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]
1 ICS103 Programming in C Lecture 8: Data Files. 2 Outline Why data files? Declaring FILE pointer variables Opening data files for input/output Scanning.
1 CSE1301 Computer Programming: Lecture 21 File I/O.
Introduction to C Programming CE Lecture 13 Strings in C.
C Language Summary HTML version. I/O Data Types Expressions Functions Loops and Decisions Preprocessor Statements.
CSE1301 Computer Programming: Lecture 19 File I/O
Command line arguments. – main can take two arguments conventionally called argc and argv. – Information regarding command line arguments are passed to.
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students C File Input and Output Checking input for errors.
Introduction to Programming 3D Applications Lecture 8 Files and Program Parameters.
1 CSE1301 Computer Programming: Lecture 19 File I/O.
20/03/01 Sudeshna Sarkar, CSE, IIT Kharagpur1 File Handling in C Lecture 17c 20/3/01.
C for Java Programmers Tomasz Müldner Copyright:  Addison-Wesley Publishing Company, 2000 Introduction to C Muldner, Chapters 1, 2.
CPT: Arrays of Pointers/ Computer Programming Techniques Semester 1, 1998 Objectives of these slides: –to illustrate the use of arrays.
File Handling Spring 2013Programming and Data Structure1.
#include int main(void) { printf("Hello, world!\n"); return 0; } entry point called on program start only one main( ) in any program # for preprocessor.
Command Line Arguments plus Variable-Length Arrays Systems Programming.
C Programming Lecture 10 Instructor: Wen, Chih-Yu Department of Electrical Engineering National Chung Hsing University.
1 Operating Systems Lecture 2 UNIX and Shell Scripts.
Chapter 3: Formatted Input/Output Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 3 Formatted Input/Output.
File IO and command line input CSE 2451 Rong Shi.
System calls for Process management
Lecture 1 cis208 January 14 rd, Compiling %> gcc helloworld.c returns a.out %> gcc –o helloworld helloworld.c returns helloworld.
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:
Lecture 11: Files & Arrays B Burlingame 18 November 2015.
24-2 Perform File I/O using file pointers FILE * data-type Opening and closing files Character Input and Output String Input and Output Related Chapter:
Arrays, Strings, and Memory. Command Line Arguments #include int main(int argc, char *argv[]) { int i; printf("Arg# Contents\n"); for (i = 0; i < argc;
System calls for Process management Process creation, termination, waiting.
Arrays and Pointers (part 2) CSE 2031 Fall March 2016.
1 Computer Programming Lecture 15 Text File I/O Assist. Prof Dr. Nükhet ÖZBEK Ege University Department of Electrical&Electronics Engineering
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 Homework Continue with K&R Chapter 5 –Skipping sections for now –Not covering section 5.12 Continue on HW5.
Chapter 7 Process Environment Chien-Chung Shen CIS/UD
ECE Application Programming
Chapter 22 – part a Stream refer to any source of input or any destination for output. Many small programs, obtain all their input from one stream usually.
Command Line Arguments
Command line arguments
Command Line Arguments
CS1010 Programming Methodology
Day 02 Introduction to C.
Command Line Arguments
Command-Line Arguments
Computer Science 210 Computer Organization
CS111 Computer Programming
Computer Programming Lecture 15 Text File I/O
Programming in C Input / Output.
More Examples of argc and argv
Command-line Arguments
Computer Science 210 Computer Organization
Programming in C Input / Output.
Command Line Arguments
CSE1320 Strings Dr. Sajib Datta
Programming and Data Structure
Introduction to C Topics Compilation Using the gcc Compiler
Command Line Parameters
File Input and Output.
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
Programming in C Input / Output.
Introduction to C Topics Compilation Using the gcc Compiler
ECE 103 Engineering Programming Chapter 46 argc, argv, envp
Arrays and Pointers (part 2)
CS1100 Computational Engineering
Arrays and Pointers (part 2)
Introduction to C CSE 2031 Fall /15/2019 8:26 AM.
Files Chapter 8.
Presentation transcript:

Introduction to C Programming CE Lecture 15 Files and Program Parameters

File Input and Output Each file must be explicitly opened before reading or writing (using actual file name as a string) and closed before execution finishes. Input:fscanf reads from a file e.g. in_file = fopen(“data1.dat”, “r”); fscanf(in_file, “%d%d”, &a, &b); Output:fprintf writes to a file e.g. out_file = fopen(“data2.dat”, “w”); fprintf(out_file, “%d %d\n”, a, b);

File output example The program on the next slide prompts the user for a file name, reads the file name, prompts for input data, reads 2 numbers from keyboard and writes their sum to the named output file.

#include "stdio.h" int main(void) { int a,b,c; char filename[21];// string file name FILE *out_file; // file pointer for output printf("\ntype name of output file: "); // prompt on screen gets(filename); // input from keyboard

out_file = fopen(filename, "w"); // open file for output if (out_file == NULL) { printf("\ncannot open: %s", filename); return 1; // abnormal program exit } printf("\ntype 2 integers");// prompt scanf("%d%d", &a, &b);// from keyboard c = a+b; fprintf(out_file, "%d\n", c); // output to file fclose (out_file); return 0;// normal program exit }

Program Parameters File names and run-time options can be provided in Unix on the command line when a program is executed. The normal command line a.out could be replaced by, a.out datain dataout where “datain” and “dataout” are the input and output files. These 2 strings must be recognised by the C program so that these files can be used for the input/output. Note that the input file “datain” must already contain data.

Program parameters, conventionally called argc and argv, are used to determine which file names and options were supplied. int main(void)// ANSI convention is replaced by: int main(int argc, char* argv[]) // universal where argc is the number of strings and argv is the array of strings

For the command line:a.out datain dataout argc is 3, andargv is an array of “a.out”, “datain” and “dataout” Execution options may be similarly specified, conventionally preceded with a ‘-’ so as to be distinguished from file names. For example:a.out datain dataout -option

Program Parameter Example Copy all 25 integers from the given input file to the given output file. The input file comprises 25 integers with no formatting but just separated by spaces or new lines. The output file shall comprise 5 rows of 5 integers each separated by a space. There is an option to echo the output on to the screen, where the 2 file names and any “-echo” option are program parameters.

#include "stdio.h" #include "string.h" int main(int argc, char *argv[]) // program parameters on command line, // e.g. a.out datain dataout -echo // argc is 4 and argv is // array of these as 4 strings { int num; // for copying each integer int row, col, option = 0; // no echo on screen, by default FILE *myfile_in, *myfile_out; // for 2 files

// check for FILE names if (argc < 3) { printf("\nMissing file name(s).\n"); printf("Too few parameters %d", argc); return 1; // abnormal exit from program } // the 2 file names cannot be the same if ((strcmp(argv[1], argv[2]) == 0)) { printf("\nsame file names !\n"); return 1; // abnormal exit }

// open first file for input myfile_in = fopen(argv[1], "r"); if (myfile_in == NULL) // check if input file exists { printf("\ncan’t open input file:%s\n", argv[1]); return 1;// abnormal exit } // open second file for output myfile_out = fopen(argv[2], "w"); if (myfile_out == NULL)// playing safe! { printf("\ncan’t open O/P file:%s\n", argv[2]); fclose(myfile_in);// already opened return 1;// abnormal exit }

// check option, should be -echo if (argc == 4) // 4th parameter { if (strcmp(argv[3],"-echo") == 0) { option = 1;// echo on screen } else { printf("illegal %s\n", argv[3]); printf("must be -echo\n"); fclose(myfile_in);// already fclose(myfile_out);// opened return 1;// abnormal exit } // else no 4th parameter specified, // option remains 0

for (row=0; row<5; row++) {// copy each row for (col=0; col<5; col++) {// copy each column fscanf(myfile_in, “%d”, &num); fprintf(myfile_out, “%d ”, num); // after each integer is a space if (option)// option == 1 printf(“%d ”, num);// echo } fprintf(myfile_out, “\n”); // end row if (option) printf(“\n”);// echo } fclose(myfile_in); fclose(myfile_out); return 0;// normal exit }// end main