• Tidak ada hasil yang ditemukan

command line parsing

N/A
N/A
Protected

Academic year: 2024

Membagikan "command line parsing"

Copied!
2
0
0

Teks penuh

(1)

/*

* This is a free program sample that may be reproduced in any form.

* The author's information should be retained to preserve its identity.

*

* Date written: December 18, 2003 * Written by: Peraphon Sophatsathit

* Department of Mathematics, Faculty of Science, Chulalongkorn University.

* email: [email protected]

* http://pioneer.netserv.chula.ac.th/~sperapho *

* Computer Systems (2301274) class supplement.

* Description: This program demonstrates how to read input parameters * from the command line and issue usage information if

* need be. The code may not compile/run on DOS platform.

*

* syntax of invocation:

* program_name infile_name outfile_name */

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <unistd.h>

#define rmode "r"

#define wmode "w"

#define Normal 0

#define Fail_file 1

#define Fail_usage 2

#define Bsize BUFSIZ

/*

* function prototype */

int process_io(char **);

int help_msg(char *);

/*

* limit input parameters to two, i.e. input and output files.

* Other options can be extracted by means of 'getopt'.

*/

int

main(int argc, char *argv[]) {

int rt_code = Normal;

switch (argc) {

case 3:

rt_code = process_io(argv);

break;

case 1:

perror("missing input parameters");

default:

rt_code = help_msg(argv[0]);

break;

}

return rt_code;

}

(2)

/*

* If the input file is missing, 'fopen' will fail.

*/

int

process_io(char *av[]) {

FILE *fi, *fo;

int len;

char tmp[Bsize-1];

if ((fi = fopen(av[1], rmode)) == NULL ||

(fo = fopen(av[2], wmode)) == NULL) {

printf("Unable to open files %s and/or %s\n", av[1], av[2]);

return Fail_file;

}

while (fgets(tmp, Bsize, fi) != NULL) {

/*

* just to demonstrate how to get rid of the newline * character from the read buffer, but is put back in * subsequent transfer (fprintf) to the output file * which could be done in much simpler and faster * approaches.

*/

len = strlen(tmp);

tmp[len-1] = '\0';

fprintf(fo, "%s\n", tmp);

}

fclose(fi);

fclose(fo);

/*

* This is a quick and dirty way to execute a system command

* and delete a file within the program via 'system' and 'unlink'.

* There is a better approach to do this on UNIX.

*/

sprintf(tmp, "cat %s", av[2]);

(void)system(tmp);

(void)unlink(av[2]);

return Normal;

}

/*

* inform the user of the program usage syntax.

*/

int

help_msg(char *pname) {

printf("Usage: %s input_filename output_filename\n\n", pname);

return Fail_usage;

}

Referensi

Dokumen terkait

2 Department of Chemistry, Faculty of Mathematics and Science, University of Pattimura, Ambon, Indonesia 3 Department of Parasitology, Faculty of Medicine, Airlangga

Program in Petroleum Geoscience, Faculty of Science, Chulalongkorn University, Bangkok, Thailand 2Basin Analysis and Structural Evolution BASE Research Unit, Department of Geology,

Qualification Date Degree University Collage Scientific Department Specialization General Specific 1 PhD 9/2014 Tanta Science Mathematics Mathematics Applied Mathematics 2

CENTER FOR ECOLOGICAL GENOMICS & WILDLIFE CONSERVATION DEPARTMENT OF ZOOLOGY FACULTY OF SCIENCE 18 December 2020 Dr Andrew Kaniki ED: Knowledge Fields Development Dear Dr Kaniki

Department of Mathematics & Computer Science Chulalongkorn University First Semester 2018 2301216 Linear Algebra and Differential Equations ISE Example of Final Examination 1.. Find a

J Psychiatr Assoc Thailand 2009; 542: 197-208 * Master of Science, Mental Health, Department of Psychiatry Faculty of Medicine, Chulalongkorn University ** Department of Psychiatry

Aliaa Burqan Faculty of Science Lecture’s time: 1- 2 S, T, T Department: Mathematics Semester: First Course title: Abstract Algebra 1 Office Hours: 10 - 11 S, T, T Course

Frasin2 1 Department of Mathematics, College of Science, University of Al-Qadisiyah, Iraq e-mail: [email protected] 2 Department of Mathematics, Faculty of Science, Al