[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: gcc stuff



Seth,

	Based on your question, I am going to guess that you are not
familiar with C, but are familiar with an object-oriented language (e.g. lisp)
or a stream language (basic).  If you will be using C or C++, I recommend
reading a copy of

	The C Programming Language by Kerninghan and Ritchie
		ISBN 0-13-110362-8
	The C++ Programming Language by Stroustrop
		???
	C++ IOStreams Handbook by Steve Teale
		ISBN 0-201-59641-5

	C has no intrinsic i/o, but depends on functions provided in
standard libraries to read files.  These books will provide the concepts
necessary.

	In the meantime, I enclose an example program in C and the same
program in C++.

			---- to use copy.c ----
	1. Copy the program below into files named copy.c.
	2. Type "gcc -o copy copy.c" to compile the program.
	3. Type "cp copy.c copy.in" to create sample input.
	4. Type "./copy" to execute the program.
	5. Type "cat copy.out" to view the result.
			---- to use copy.c ----

			---- copy.c ----
/*
 * Project: C Programming
 * File:    Trivial file example
 *          Copy a file from input to output
 * Author:  Dr. Robert J. Meier
 * History: 95-10-24 -rjm- file creation
 */

/* io functions defined in the standard library are declared in stdio.h */
#include <stdio.h>              /* FILE */

/* Nominal c programs are executed by evaluating the special function, main */
main() {

  /* Create a data structure for the input and output file */
  FILE *in = fopen("copy.in", "r");
  FILE *out = fopen("copy.out", "w");

  /* Report an error if the files cannot be openned */
  if (!in) {
    perror("Unable to read copy.in");
  } else if (!out) {
    perror("Unable to write copy.out");
  } else {

    /* If everything is ready copy each character in a loop until EOF */
    int c;
    for (; EOF != (c = fgetc(in)); fputc(c, out));
  }

  /* Reclaim memory space from the file data structures */
  fclose(in);
  fclose(out);
  return 0;
}
			---- copy.c ----

			---- to use copy.C ----
	1. Copy the program below into files named copy.C.
	2. Type "g++ -o copy copy.C" to compile the program.
	3. Type "cp copy.C copy.in" to create sample input.
	4. Type "./copy" to execute the program.
	5. Type "cat copy.out" to view the result.
			---- to use copy.C ----

			---- copy.C ----
// Project: C++ Programming
// File:    Trivial file example
//          Copy a file from input to output
// Author:  Dr. Robert J. Meier
// History: 95-10-24 -rjm- file creation
//          97-03-24 -rjm- converted to c++

// io classes defined in the standard library are declared in streamio.h
#include <fstream.h>           // ifstream

// Nominal c++ programs are executed by evaluating the special function, main
main() {

  // Open the input and output streams
  ifstream in("copy.in");
  ofstream out("copy.out");

  // Report an error if the files cannot be openned
  if (!in) {
    cerr << "Unable to read copy.in" << endl;
  } else if (!out) {
    cerr << "Unable to read copy.out" << endl;
  } else {

    // If everything is ready copy each character in a loop until EOF
    for (char c; c = in.get(), !in.eof(); out << c);
  }
  return 0;
}
			---- copy.C ----

						With life on loan from God,
-- 
						Robert Meier

FANUC Robotics North America, Inc.	Internet: meierrj@frc.com
Voice: 1-810-377-7469			Fax:      1-810-377-7363


Reply to: