




Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
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
1 / 8
This page cannot be seen from the preview
Don't miss anything!





existing text file, for writing
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
#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 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);
=== 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
================================