Вы находитесь на странице: 1из 36

fread ()

Read n items of data from specified file


into buffer
int fread(buffer, size, number, File *fp )
Read data is stored in buffer, eg. char text[30]
Size of each data is size bytes, sizeof(char)
File pointer
Can we use fread in the copy program ?
fwrite()
int *fwrite(buffer, size, n, File *fp)
Write n items from buffer into the specified
file.
We will write a program that opens a text
file and capitalizes all letters in it.
File Position Indicator
FILE *fp;
fp = fopen(Test.txt,r+);
Test.txt
File Stream fp Byte Byte .. Byte EOF

File Position Indicator (starts from 0)

File position indicator indicates the next


read/write position represented by the file
stream fp. Incremented by 1 after each byte is
read/written.
Example: To Upper Case

Write a program that opens a text file and


capitalizes all letters in it.
#include<stdio.h>
#include<stdlib.h>
int main(int argc, char *argv[]) {
if (argc != 2)
exit(1);
FILE *fp, *tmp; char ch;
if ((fp = fopen(argv[1],"r+")) != NULL)
if ((tmp = tmpfile()) != NULL) {
while((ch = fgetc(fp)) != EOF)
fputc(toupper(ch), tmp);
rewind(tmp);
rewind(fp);
while ((ch=fgetc(tmp)) != EOF)
fputc(ch,fp);
}
fclose(tmp); fclose(fp);
}
tmpfile()

FILE *tmpfile(void)
Opens a temporary file. Need not specify
a file name.
The temporary file is opened in wb+
mode. If the request cannot be honoured,
NULL is returned.
The system removes the file after it is
closed, or on program exit.
rewind()

void rewind(FILE *fp)


The file position indicator for the stream
pointed to by fp is set to the beginning of the
file.
Clears EOF and error indicators.
Accessing a file randomly (1)
The library functions fseek() and ftell()
are used to access a file randomly.
An expression of the form
ftell(file_ptr)
returns the current value of the file position
indicator pointed to by stream file_ptr.
Accessing a file randomly (2)
The value represents the number of
bytes from the beginning of the file,
counting from zero.
Technically, the file position indicator is
a member of the structure pointed to by
file_ptr.
ftell() - Example
FILE *fp;
fp = fopen(hello.txt,r);
c=fgetc(fp);c=fgetc(fp); ftell(fp);
hello.txt
File Stream fp h e l l o w o

File Position Indicator

ftell(fp) will return 2


fseek()
The function fseek() takes three
arguments: a file pointer, an integer
offset, and an integer that indicates the
place in the file from which the offset
should be computed.
fseek() - contd.
fseek(file_ptr, offset, place);
sets the file position indicator to offset
positions(bytes) starting from place.
The value for place can be 0, 1, or 2,
meaning the beginning, current position,
or end of the file, respectively.
fseek() - Example
FILE *fp;
fp = fopen(hello.txt,r);
fseek(fp, 4, 0); ftell(fp);
hello.txt
File Stream fp h e l l o w o

File Position Indicator

ftell(fp) will return 4.


What will be an equivalent expression for
the fseek expression?

fseek(fp, 0, SEEK_SET) rewind(fp)


Example

Write a C program to print the contents


of a file backwards.
#include<stdio.h>
#include<stdlib.h>
int main(int argc, char *argv[]) {
if (argc != 2 )
exit(1);
FILE *fp;
if ((fp = fopen("hello.txt","r")) != NULL)
{
fseek(fp, 0 ,2);

while ( (ftell(fp)) > 1 ) {


fseek(fp, -2,1);
putchar(fgetc(fp));
}

}
fclose(fp);
}
Discussion Quiz2
1. What does the function CB evaluate, explain
your answer.
int CB(unsigned int x) {
int count=0;
while(x) {
count++;
x = x&(x-1); }
return count; }
Counts the number of bits set in x
Write a program to multiply two integers
a and b using only bit shifts and
addition.
Effect of shift operations
What is the effect of shifting a number
to the left?
Take for example 5
0 0 0 0 0 1 0 1

Shift it by left, it becomes 10


0 0 0 0 1 0 1 0

Shift it by left again, it becomes 20


0 0 0 1 0 1 0 0
Multiplying using shift and add
operations
Take an example, multiplication of a = 6
and b = 5.
6 x 5 can be written as
6 x (1 + 4) that is
6 x 20 + 6 x 2 2
Which is equivalent to
a << 0 + a << 2
How the algorithm for shift and
add works
Initialize c (where we will store the result
to 0).
int c = 0;
a 0 0 0 0 0 1 1 0

b 0 0 0 0 0 1 0 1
How the algorithm for shift and
add works (2)
Check whether the left most bit of b is
set to 1.
If yes, add a (=6) to c, i.e. c = c + a;
a 0 0 0 0 0 1 1 0

b 0 0 0 0 0 1 0 1
How the algorithm for shift and
add works (3)
Shift a to left by 1 and b to right by 1.
Check whether the left most bit of b is
set to 1.
a 0 0 0 0 1 1 0 0

b 0 0 0 0 0 0 1 0
How the algorithm for shift and
add works (4)
Shift a to left by 1 and b to right by 1.
Check whether the left most bit of b is
set to 1. If yes, c = c (6) + a (24)
Continue the loop until b becomes 0.

a 0 0 0 1 1 0 0 0

b 0 0 0 0 0 0 0 1
int main() {
int a = 6, b = 5;
int c = 0;
while (b != 0) {
if ((b & 1) != 0)
c = c + a;
a = a << 1;
b = b >> 1; }
printf("%d", c);
}
char *p[2][3] = { abc, defg, hi,
jklmno, pqrstuvw, xyz };
Expression Equivalent Value
Expression
**p[1]
**(p[1] + 2)
***p
(*(*(p+1) +1))[7]
*(p[1][2] + 2)
defg hi
abc
0 1 2
**p[1] = j
p[0]
char *p[2][3] =
p[1]
**(p[1] + 2) = x
xyz
***p = a jklmno
pqrstuvw

*(*(p+ 1) + 1)[7] = w
*(p[1][2] + 2) = z
Can two unions be compared using the
logical equality operator (==). Explain.
The compiler cannot tell what the union
currently contains and hence cannot
compare them.
Can we use the keyword typedef to
define new data types.
No. The keyword typedef is used to
define new names (aliases) for
previously defined data types.
Can we pass a structure to a function
using call-by-reference.
Structures are always passed to
functions using call-by-value.
Executing commands from
within a C program
The library function system() is used to
access operating system commands.
system(date);
The string passed to system() is treated
as a operating system command.
int main()
{ system(date); }
Using pipes from C program
The function
FILE *popen(command,r/w)
int pclose(File *fp)
is used to communicate with the
operating system.
Creates a pipe between the command
executed and the calling environment.
popen() and pclose(): Example
#include <ctype.h>
#include <stdio.h>
main()
{
int c;
FILE *ifp;
ifp = popen("ls","r");
while ((c=getc(ifp)) != EOF)
putchar (toupper(c));
pclose(ifp);
}
Environment Variables
A set of dynamic variables that can
affect the way a process behaves in a
computer. Example PATH, HOME
The value of an environment variable
can be printed using the $ symbol
before the variable, echo $PATH
Accessing environment
variables
An environment variable can be
accessed from within a C program by
using the standard function (included in
stdlib.h) getenv
char *getenv(const char *name)
If name is an environment variable the
address of the string is returned else NULL
printf(%s,getenv(PATH));
Another example
#include<stdio.h>
#include<stdlib.h>

int main(){
printf("%s%s \n %s%s \n %s%s \n",
"User:", getenv("USER"),
"Shell: ", getenv("SHELL"),
"Home Directory", getenv("HOME"));
}

Вам также может понравиться