Text File Input and Output - C Programming - Lecture Slides, Slides of Computer Science

These are the Lecture Slides of C Programming which includes Sscanf() Function, Snprintf() Function, Converts String, Floating Point Value, Searches for First Occurrence, Number of Characters, Token Separators, Value of Zero Means etc. Key important ponts are: Text File Input and Output, Text File Buffered, Fopen Function, Demo of File Write, Prototypes, Object Controlling Stream, Abridged List, Output Demo Program, Text Read Mode

Typology: Slides

2012/2013

Uploaded on 03/21/2013

dheeraj
dheeraj 🇮🇳

5

(4)

101 documents

1 / 8

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Text File Input and Output
Docsity.com
pf3
pf4
pf5
pf8

Partial preview of the text

Download Text File Input and Output - C Programming - Lecture Slides and more Slides Computer Science in PDF only on Docsity!

Text File Input and Output

Overview

  • Text File Buffered I/O Functions
  • fopen Function
  • Demo of File Write and Read

fopen Function

  • "r" -- to open an existing text file for reading
  • "w" -- to create a text file or to open and truncate an

existing text file, for writing

  • "rb" -- to open an existing binary file for reading
  • "wb" -- to create a binary file or to open and truncate an

existing binary file, for writing

FILE * fopen (const char *filename, const char *mode);

Opens the file with the filename filename , associates it with a stream, and returns

a pointer to the object controlling the stream. If the open fails, it returns a NULL

pointer. The initial characters of mode determine how the program manipulates

the stream and whether it interprets the stream as text or binary. The characters

of mode must be one of the following sequences:

(This is an abridged list) Docsity.com

Demo of File Write and Read

#include <stdio.h> #include <stdlib.h>

#define MAX_NUMBER 50 #define MAX_SIZE 200

int main(void) { const char fileName[] = "numbers.dat"; *FILE fileID; int i; char buffer[MAX_SIZE]; int value; int readCount;

// Open file in text write mode // Write title and values to numbers file // Close file

// Open file in text read mode // Read title and values from numbers file // Close file

return 0; } // End main

Open a File in Text Read Mode

// Open file in text read mode fileID = fopen (fileName, "r"); if (fileID == NULL) { fprintf (stderr, "Error: Could not open %s in read mode\n", fileName); exit(1); } // End if

// Read title and values from numbers file printf("\n=== CONTENTS OF NUMBERS FILE ===\n");

fgets (buffer, MAX_SIZE, fileID); printf("%s\n", buffer);

while (! feof (fileID)) { readCount = fscanf (fileID, "%d", &value); printf("%5d", value); } // End while

printf("\n================================\n");

// Close file fclose (fileID);

Output from Demo Program

=== CONTENTS OF NUMBERS FILE ===

Integers from 1 to 50

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50

================================