Está en la página 1de 3

Ajmal khan

Fa08-BCE-091

Q1: Write a program in C to count the number of lines, words, spaces present in a text file STORY.TXT also program will calculate the size of STORY.TXT file in bytes. Answer: #include<stdio.h> void main() { FILE *in; int c; in=fopen("story.txt","rd"); int ch=0,w=0,l=0,m,size=0; c=getc(in); while(c!=-1)//loop till the end of the file { size++; //updating size ch++; //updating character m=0; //for checking whether new value is update from file or not if(c=='\n') { l++; //updating line m++; //incriminate m value from file is to be update ch--; // last time that was line not a character so decrement it c=getc(in); // getting value from file while(c==' '||c=='\n') // loop for checking all line and total empty spaces { size++; if(c=='\n') l++; c=getc(in); } w++; } if(c==' ') { m++; ch--;

c=getc(in); while(c==' ') { } w++; } if(m==0) c=getc(in); } printf("\ncharacter are %d\nwords are %d\n line are %d\n size is %d bytes\n",ch,w,l,size); } size++; c=getc(in);

Ans 2:
first it will check the descriptor if it is Null then it show the message Error opening test_file and exit from the program if file descriptor is not null then first it will write the message 1111111111 in the file then overwrite it with "2222222222" then it will check the position if it is not zero then it will print the message Error in seek operation: and exit from the program if seek position is zero then it will write the message from the file to a buffer and print the message from the buffer and output will be The record is 2222222222 Q3: Find error in following code #include<stdio.h> nt main ( ) // return type should be int instead of nt { int * fp; // file descriptor data type is FILE not int int, k; // , can not come with int or int, is undefined fp = fopen ("pizza"); //in which mode it will open mode is not given for (k = 0; k < 30; k++) printf ("Jill likes pizza.\n", fp); fclose ("pizza"); //close should have file descriptor not file name return 0; } correct code is written below #include<stdio.h> int main ( ) //make int { FILE *fp; //data type change int k;/ / remove , fp=fopen("pizza.txt","rd"); //give mode for (k = 0; k < 30; k++) printf ("Jill likes pizza.\n", fp); fclose (fp); //give file descriptor return 0;

} Do as Directed: Question#1 Redirect the stdoutput and stderror of the following command into two separate files called stdout.txt and stderror.txt (LS|ls l) Answer ls 2>stderror.txt ls -1 2>>stderror.txt ls 1>stdout.txt ls -1 1>>stdout.txt Question#2 Create an Empty file your-name.txt and give him the following permission User can write in it, Group can read and execute and others can only read it. Answer Fd=open(Ajmal.txt,O_RDWR|O_CREAT,S_IWUSR|S_IRGRP|S_IXGRP|S_IROTH); Question#3 Display the content of Root / Directory and save them in a file Directory2.txt. Answer cd ~ ls>directory.txt Question#4 Print the directory structure of any linux file system into a file Directory.txt then sort this file content and print them on Screen. Answer cd bin/ Ls -1>Directory.txt cat directory.txt I

También podría gustarte