File Reading Example-Programming-Lecture Slides, Slides of Computer Programming

This lecture was delivered by Prof. Varun Sahil to explain Programming concepts at Ankit Institute of Technology and Science. It includes: File, Reading, Sum, Character, Similar, Read, Multiple, Declare, Close, Format, Structured

Typology: Slides

2011/2012

Uploaded on 07/23/2012

paravi
paravi 🇮🇳

5

(1)

65 documents

1 / 24

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
File Reading Example
Sum the numbers in a file
#include <stdio.h>
void main () {
int num, sum = 0;
FILE *inptr;
if ((inptr = fopen(“infile”, “r”)) != NULL) {
while(fscanf(inptr, “%d”, &num) != EOF)
sum += num;
fclose(inptr);
printf(“The sum of the numbers is %d\n”,sum);
}
}
docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18

Partial preview of the text

Download File Reading Example-Programming-Lecture Slides and more Slides Computer Programming in PDF only on Docsity!

File Reading Example

Sum the numbers in a file

#include <stdio.h>void main () {

int num, sum = 0;FILE *inptr;if ((inptr = fopen(“infile”, “r”)) != NULL) {

while(fscanf(inptr, “%d”, &num) != EOF)

sum += num;

fclose(inptr);printf(“The sum of the numbers is %d\n”,sum);

}

}

Read from the File

One character at a time

Format: c = getc(fptr)

getc reads from file pointer (fptr) and returns acharacter

Similar to getchar

Example

while ((c = getc(infileptr)) != EOF)

Continues reading characters until it reaches the endof the file

c = fgetc(fptr) does the same thing

Writing to a File

Steps

Declare FILE variable

Same as before

Open the file (write mode)

Write to the file

Close the file

Same as before

Open the File

Format: fptr = fopen(“file”,”w”);

E.g, infileptr = fopen(“my_file”,”w”);

Creates new file

Overwrites file if it exists

Starts writing at the beginning of the file

fopen(“file”, “a”) appends a file

Creates new file if it doesn’t exist

Starts writing from the beginning

Starts writing at the end of an existing file

File Writing Example

Write the sum of the numbers to a file

#include <stdio.h>void main () {

int num, sum = 0;FILE *inptr, *outptr;if ((inptr = fopen(“infile”, “r”)) != NULL) {

while(fscanf(inptr, “%d”, &num) != EOF)

sum += num;

fclose(inptr);if ((outptr = fopen(“outfile”, “w”)) != NULL) {

fprintf(outptr,“The sum of the numbers is %d\n”,sum);fclose(outptr);

}

}

}

Write to the File

One character at a time

Format: putc(char, fptr)

putc writes a character to file pointer (fptr)Similar to putchar

Example

putc(‘\n’, outfileptr);

Writes a newline to the file

fputc(char, fptr) does the same thing

Pointers

MainMemory(RAM)

Input

Devices

Output Devices Auxiliary

Storage

CPU

ControlUnit (CU) Arithmetic-Logic Unit

(ALU)

Memory

Addresses

0 - 34 - 7

100 -107108 -

110 -

..

instr instr

- 254

.

...

101.

Memory Organization

data

program

Declaring Variables

Declaration allocates memory

The variable name is associated with thisblock

E.g. char mychar = ‘z’;

mychar

Address = 0x0defa

z

value = value – increment;

increment value

MemoryAddresses

VariableNames

Internal Memory

c

d

int

x

=

5,

y

=

10;

float

f

=

12.5,

g

=

9.8;

char

c

=

‘c’,

d

=

‘d’;

Memory layout and addresses

docsity.com

Pointer

Pointer is a variable that keeps anaddress of a variable

Need to learn four things aboutpointers

Declaration

Initialization

Assignment

Pointer’s arithmetic

Initialization

Before using a pointer variable it MUSTbe initialized means it must point to avalid address

Example

int *xptr;

int marks;

xptr = & marks;

defining marks will allocate a memory space forthe variable, xptr now has the address of thevariable marks

Assignment

A bit CONFUSING: assignment also uses*, do not confuse it with the pointerdeclaration

int

marks

int

*xptr;

xptr

&marks;

*xptr

marks

will

be

equal

to