C++ Lecture 1 - C++ Lectures

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

View Full Document Right Arrow Icon
C++ Lecture 1 http://www.cs.mun.ca/~gstarkes/3710/c++/lecture1.html 1 of 5 4/13/2009 5:27 PM C++ Lecture 1 [Lecture Index] [Next Lecture] grow-c.c #include <stdio.h> #include <stdlib.h> #include <errno.h> #include <string.h> struct growing_buffer { char *buf; int n_used; int size; }; int buffer_init(struct growing_buffer *gb) { gb->size = 16; gb->n_used = 0; gb->buf = (char *) malloc(gb->size); if (!gb->buf) return 0; return 1; } int buffer_addc(struct growing_buffer *gb, char c) { if (!gb->buf) /* previous failure */ return 0; if (gb->n_used >= gb->size) { gb->size *= 2; gb->buf = (char *) realloc(gb->buf, gb->size); if (!gb->buf) { gb->n_used = 0; gb->size = 0; return 0; } } gb->buf[gb->n_used++] = c; return 1; } void buffer_reset(struct growing_buffer *gb) { gb->n_used = 0; } void buffer_release(struct growing_buffer *gb) { free(gb->buf); } int readline(FILE *fp, struct growing_buffer *gb) { int c; buffer_reset(gb); while ((c = fgetc(fp)) != EOF) { if (!buffer_addc(gb, c)) return -1;
Background image of page 1

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

View Full DocumentRight Arrow Icon
C++ Lecture 1 http://www.cs.mun.ca/~gstarkes/3710/c++/lecture1.html 2 of 5 4/13/2009 5:27 PM if (c == '\n') break; } if (!buffer_addc(gb, '\0')) return -1; return gb->n_used - 1;
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 12/24/2009 for the course PROGRAMMIN C++ Progra taught by Professor Showman during the Spring '09 term at Zagazig University.

Page1 / 6

C++ Lecture 1 - C++ Lectures

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

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