Category Archives: File Program ( C Linux )

Reading and writing :

Input from keyboard and output to the console :-
First open  Linux Terminal > (write) gedit filename.c > (press enter)

int main()
{
char buf[50];
int n = read(0,buf,50);
write(1,buf,50/n);
}

Save the file .
How to compile and run :-
Open  Linux Terminal > (write ) gcc filename.c > (press enter)
> (write) ./a.out > (press enter)

Syntax explanation :-

read( int fd , void *buf ,int  size)
file descriptors :
0 – Input from the keyboard.
1 – Output to the console. 

Copy one file content into another file : Linux

First create a file named as file1 and write something into it.

#include<stdio.h>
#include<unistd.h>
#include<fcntl.h>
int main()
{
int n,m;
char buffer[50];
n = open ("file1", O_RDONLY);  // read only mode
m = open("file2" , O_CREAT | O_WRONLY,0777) ;  // write only mode
printf("Both files are open");
read(n,buffer,20);  // reading from file1 (n file descriptor )
write(m,buffer,20); // writing into file2
printf("Task completed ");
return 0;
}