{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

C++ Lecture 1 - C Lectures

Info icon This 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;
Image of page 1

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

View Full Document Right 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; } int main(int argc, char **argv) { int i; int lineno;
Image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern