DOC PREVIEW
Princeton COS 217 - C Text File Handling

This preview shows page 1 out of 2 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 2 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 2 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Princeton UniversityCOS 217: Introduction to Programming SystemsOpening a Text File for WritingOpening a Text File for ReadingReading Data from a Text FilePrinceton University COS 217: Introduction to Programming Systems C Text File Handling Opening a Text File for Writing #include <stdio.h> FILE *psFile; psFile = fopen("filename", "w"); Open filename for writing. Return the address of a FILE structure (or NULL). Note: stdout and stderr are predefined variables of type FILE* Writing Data to a Text File Character: iStatus = fputc(iChar, psFile); iStatus = putc(iChar, psFile); /* May be a macro */ iStatus = putchar(iChar); /* May be a macro */ Write iChar to psFile (or stdout). Return iChar (or EOF). String: iStatus = fputs(pcString, psFile); /* Omits ‘\0’ */ iStatus = puts(pcString); /* Replaces ‘\0’ with ‘\n’ */ Write pcString to psFile (or stdout). Return a non-negative number (or EOF). Formatted data: iStatus = fprintf(psFile, "%d", i); iStatus = printf("%d", i); Convert i to a sequence of ASCII digits. Write those digits to psFile (or stdout). Return the number of digits written (or EOF). See Kernighan and Ritchie pp. 243-245 for fprintf() conversion specification for each data type. Page 1 of 2Opening a Text File for Reading #include <stdio.h> FILE *psFile; psFile = fopen("filename", "r"); Open filename for reading. Return a pointer to a FILE structure (or NULL). Note: stdin is a predefined variable of type FILE*. Reading Data from a Text File Character: iChar = fgetc(psFile); iChar = getc(psFile); /* May be a macro */ iChar = getchar(); /* May be a macro */ Read an ASCII code from psFile (or stdin). Return the ASCII code (or EOF). Line: pcStatus = fgets(pcString, iBufferSize, psFile); /* Appends ‘\0’ */ pcStatus = gets(pcString); /* Replaces ‘\n’ with ‘\0’ */ /* Dangerous: May corrupt memory */ Read a line from psFile (or stdin) into the memory at address pcString. Return pcString (or NULL). Formatted data: iStatus = fscanf(psFile, "%d", &i); iStatus = scanf("%d", &i); Read a sequence of ASCII digits from psFile (or stdin); stop at the first non-digit character. Convert the sequence of digits to an integer. Assign the integer to memory at address &i. Return the number of values read (or EOF). See Kernighan and Ritchie pp. 245-246 for fscanf() conversion specifications for each data type. Closing a Text File iStatus = fclose(psFile); Close psFile, and return 0 (or EOF). Copyright © 2005 by Robert M. Dondero, Jr. Page 2 of


View Full Document

Princeton COS 217 - C Text File Handling

Documents in this Course
Summary

Summary

4 pages

Lecture

Lecture

4 pages

Generics

Generics

14 pages

Generics

Generics

16 pages

Lecture

Lecture

20 pages

Debugging

Debugging

35 pages

Types

Types

7 pages

Lecture

Lecture

21 pages

Assembler

Assembler

16 pages

Lecture

Lecture

20 pages

Lecture

Lecture

39 pages

Testing

Testing

44 pages

Pipeline

Pipeline

19 pages

Lecture

Lecture

6 pages

Signals

Signals

67 pages

Building

Building

17 pages

Lecture

Lecture

7 pages

Modules

Modules

12 pages

Generics

Generics

16 pages

Testing

Testing

22 pages

Signals

Signals

34 pages

Lecture

Lecture

19 pages

Load more
Download C Text File Handling
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view C Text File Handling and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view C Text File Handling 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?