0% found this document useful (0 votes)
168 views22 pages

C Program To Read The File and Display Contents On The Screen

The document contains C code snippets for various file handling programs and a recursive descent parser. The key programs are: 1. Programs to read and display file contents, take filename as argument, and copy contents of one file to another. 2. A recursive descent parser that checks variable declarations in a file, allowing for arrays. 3. A similar parser that checks function prototypes, allowing array parameters.

Uploaded by

anuprk
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
168 views22 pages

C Program To Read The File and Display Contents On The Screen

The document contains C code snippets for various file handling programs and a recursive descent parser. The key programs are: 1. Programs to read and display file contents, take filename as argument, and copy contents of one file to another. 2. A recursive descent parser that checks variable declarations in a file, allowing for arrays. 3. A similar parser that checks function prototypes, allowing array parameters.

Uploaded by

anuprk
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 22

1. C program to read the file and display contents on the screen.

#include<stdio.h>

int main()
{
FILE *fp;
char ch;
fp=fopen("input.txt","r");
if(fp==0)
{
printf("\nfile can not be opened\n");
return 0;
}

while((ch=getc(fp))!=EOF)
{

printf("%c",ch);
}
fclose(fp);
return 0;
}











2. Filename has to be read from the command line argument.

#include<stdio.h>

int main(int argc,char *argv[])
{
FILE *fp;
char ch;
fp=fopen(argv[1],"r");
if(fp==0)
{
printf("\nfile can not be opened\n");
return 0;
}

while((ch=getc(fp))!=EOF)
{

printf("%c",ch);
}
fclose(fp);
return 0;
}










3. Copy the contents of one file to another.

#include<stdio.h>

int main()
{
FILE *fp1,*fp2;
char ch;
fp1=fopen("input.txt","r");
if(fp1==0)
{
printf("\nfile can not be opened\n");
return 0;
}

fp2=fopen("output.txt","w");
if(fp2==0)
{
printf("\nfile can not be opened\n");
return 0;
}
while((ch=getc(fp1))!=EOF)
{

//printf("%c",ch);
putc(ch,fp2);
}

fclose(fp1);
fclose(fp2);
return 0;
}






4. Source and destination filenames are given as command line
arguments.

#include<stdio.h>

int main(int argc,char *argv[])
{
FILE *fp1,*fp2;
char ch;
fp1=fopen(argv[1],"r");
if(fp1==0)
{
printf("\nfile can not be opened\n");
return 0;
}

fp2=fopen(argv[2],"w");
if(fp2==0)
{
printf("\nfile can not be opened\n");
return 0;
}
while((ch=getc(fp1))!=EOF)
{

//printf("%c",ch);
putc(ch,fp2);
}
fclose(fp1);
fclose(fp2);
return 0;
}






5. C program to read string from one file and write the string to another file.
Destination file should contain only one string per line.

#include<stdio.h>

int main(int argc,char *argv[])
{
int flag;
FILE *fp1,*fp2;
char ch;
fp1=fopen(argv[1],"r");
if(fp1==0)
{
printf("\nfile can not be opened\n");
return 0;
}
fp2=fopen(argv[2],"w");
if(fp2==0)
{
printf("\nfile can not be opened\n");
return 0;
}
while((ch=getc(fp1))!=EOF)
{
if(ch=='\n'||ch=='\t'||ch==' ')
{
if(flag==0)
continue;
else
flag=0;
}
else
flag=1;
//printf("%c",ch);
if(flag==0)
putc('\n',fp2);
else
putc(ch,fp2);
}
return 0;
}


6. Modify program 5. If a string is already present in the destination file ,
donot write it again.

#include<stdio.h>
int main(int argc,char *argv[])
{
int flag=1,i=0;
FILE *fp1,*fp2;
char ch,ch2[10],ch3[10];
fp1=fopen(argv[1],"r");
if(fp1==0)
{
printf("\nfile can not be opened\n"); return 0;
}
fp2=fopen(argv[2],"r+");
if(fp2==0)
{
printf("\nfile can not be opened\n"); return 0;
}
while(fscanf(fp1,"%s",ch2)!=EOF)
{
fseek(fp2,0,SEEK_SET);
while(fscanf(fp2,"%s",ch3)!=EOF)
{
if(!strcmp(ch2,ch3))
{
flag=0; break;
}
else
{
flag=1; continue;
}
}
if(flag==1)
{
fprintf(fp2,"%s\n",ch2);
//printf("%s\n",ch2);
}
}
fclose(fp2);
fclose(fp1);
}

7. C program to count the number of characters, words, tabs and new lines in
a file. If the word is already present dont count as a separate word.

#include<stdio.h>

int main(int argc,char *argv[])
{
int tab=0, line=0, cha=0, words=0, flag=1;
FILE *fp, *fp2;
char ch, ch2[10], ch3[10];
fp=fopen(argv[1],"r");
if(fp==0)
{
printf("\nfile can not be opened\n"); return 0;
}
fp2=fopen(argv[2],"r+");
if(fp2==0)
{
printf("\nfile can not be opened\n"); return 0;
}
while((ch=getc(fp))!=EOF)
{
if(ch=='\t')
tab++;
else if(ch=='\n')
line++;
else if(ch>='a'&&ch<='z')
cha++;
}
printf("\ntabs = %d\nlines = %d\ncharacters = %d\n",tab,line,cha);
fseek(fp,0,SEEK_SET);
while(fscanf(fp,"%s",ch2)!=EOF)
{
fseek(fp2,0,SEEK_SET);
while(fscanf(fp2,"%s",ch3)!=EOF)
{
if(!strcmp(ch2,ch3))
{
flag=0; break;
}
else
{
flag=1; continue;
}
}
if(flag==1)
{
words++;
fprintf(fp2,"%s\n",ch2);
}
}
printf("\nwords = %d\n",words);
fclose(fp);
fclose(fp2);
}

















1. Write C program for recursive-descent parser (predictive parser) for syntactic
checking the variable declarations. Declarations may include arrays also.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void Dimensions();
void advance();
void Start();
void Variables();
void Variable();

intis_type(const char* str) {
char* keywords[]= {"int", "char", "float", "double" };

int i = 0;
for (; i < 4; i++ ) {
if ( strcmp(str, keywords[i]) == 0 ) {
return 1;
}
}
return 0;
}
FILE *fp;

enum {
END,
TYPE,
ID,
CONST,
SEMI_COLON,
OPEN_BRACKET,
CLOSE_BRACKET,
COMMA
};

/* LEXICAL ANALYZER */
intcurrent_token;
intline_num = 1;
intnext_token() {
while (1) {
charch = fgetc(fp);
if ( ch == ' ' || ch == '\t' ) {
}
else if ( ch == '\n' ) {
line_num++;
}
else if ( ch == EOF ) {
return END;
}
else if ( ch == ',' ) {
return COMMA;
}
else if ( ch == ';' ) {
return SEMI_COLON;
}
else if ( ch == '[' ) {
return OPEN_BRACKET;
}
else if ( ch == ']' ) {
return CLOSE_BRACKET;
}
else if ( isalpha(ch) ) {
charstr[100];
int i = 0;
do {
str[i++] = ch;
ch = fgetc(fp);
} while ( isalpha(ch) || isdigit(ch) );

str[i] = '\0';
fseek(fp, -1, SEEK_CUR);
if ( is_type(str) )
return TYPE;
else
return ID;
}
else if ( isdigit(ch) ) {
do {
ch = fgetc(fp);
} while ( isdigit(ch) );

fseek(fp, -1, SEEK_CUR);
return CONST;
}
else {
printf("unknown token\n");
return -1;
}
}
return 0;
}

void advance() {
current_token = next_token();
switch ( current_token ) {
case TYPE:
printf("current_token type\n");
break;
case ID:
printf("current_token identifier\n");
break;
case CONST:
printf("current_tokeninterger constant\n");
break;
case SEMI_COLON:
printf("current_token semicolon\n");
break;
case OPEN_BRACKET:
printf("current_token [\n");
break;
case COMMA:
printf("current_token ,\n");
break;
case CLOSE_BRACKET:
printf("current_token ]\n");
break;
default:
printf("unexpected %d\n", current_token);
}
}

int success = 1;
intmatch_token(int token) {
if ( current_token == token ) {
return 1;
}
else {
success = 0;
printf ("*** Error: Line Number %d: ", line_num);
switch ( token ) {
case TYPE:
printf("expecting a type\n");
break;
case ID:
printf("expecting an identifier\n");
break;
case CONST:
printf("expecting an interger constant\n");
break;
case SEMI_COLON:
printf("expecting a semicolon\n");
break;
case CLOSE_BRACKET:
printf("expecting a ]\n");
break;
default:;
}
}

return success;
};

void Variables() {
Variable();
if ( current_token == COMMA ) {
advance();
Variables();
}
}

void Variable() {
if ( match_token(ID) ) {
advance();
Dimensions();
}
}

void Dimensions() {
if ( current_token == OPEN_BRACKET ) {
advance();
if ( match_token(CONST) ) {
advance();
if ( match_token(CLOSE_BRACKET) ) {
advance();
Dimensions();
}
}
}
}

void Statement() {
advance();
if ( current_token != END &&match_token(TYPE) ) {
advance();
Variables();
if ( match_token(SEMI_COLON) ) {
}
}
}

void Statements() {
Statement();
if ( current_token != END ) {
Statements();
}
}

void Start() {
Statements();
if ( success ) {
printf("Success!\n");
}
}

int main(void)
{
fp=fopen("file7.1.txt", "r");
if(fp==NULL)
{
printf("Error\n");
}
else
{
Start();
}
}
























2. Write C program for recursive-descent parser (predictive parser)for syntactic checking
the function prototype. Arrays may be passed as parameters.
#include<stdio.h>
#include<stdlib.h>

FILE *fp;
charch;

intcheckid()
{
ch=fgetc(fp);
if((ch>= 'a' &&ch<= 'z') || (ch>= 'A' &&ch<= 'Z'||))
{
while(1)
{

if((ch>= 'a' &&ch<= 'z') || (ch>= 'A' &&ch<= 'Z'));
else if(ch>= '0' &&ch<= '9');
else
{
return 0;
}
ch=fgetc(fp);
if(ch=='('||ch==')'||ch==',')
break;
}
return 1;

}
else
return 0;
}
int type()
{
charch;
ch=fgetc(fp);
if(ch=='i')
{
if((ch=fgetc(fp))!='n')
return 0;
if((ch=fgetc(fp))!='t')
return 0;
if((ch=fgetc(fp))!=' ')
return 0;
return 1;
}
else if(ch=='c')
{
if((ch=fgetc(fp))!='h')
return 0;
if((ch=fgetc(fp))!='a')
return 0;
if((ch=fgetc(fp))!='r')
return 0;
if((ch=fgetc(fp))!=' ')
return 0;
return 1;
}
else if(ch=='v')
{
if((ch=fgetc(fp))!='o')
return 0;
if((ch=fgetc(fp))!='i')
return 0;
if((ch=fgetc(fp))!='d')
return 0;
if((ch=fgetc(fp))!=' ')
return 0;
return 1;
}
else
return 0;
}
int main()
{
char c,c1;
fp=fopen("File.c","r");
int flag=0;
//printf("..........................................\n");
flag=type();
if(flag==1)
{
flag=checkid();
if(flag==1)
{
while(1)
{
flag=type();
if(flag==1)
flag=checkid();
if(flag==1 &&ch==',')
continue;
else
break;
}

c=fgetc(fp);
if(flag==1 && c==';')
printf("Function Prototype valid\n");
else
flag=0;

}
}
if(flag==0)
printf("Function Prototype invalid\n");
}












































3. Write C program for recursive-descent parser (predictive parser) for syntactic
checking the structure declarations. Declarations may include another structure as its
member.

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

intis_type(char *str);
intmatch_token(int token);
intnext_token();
void start();
void variables();
void variable();
void dimensions();
void advance();

FILE *fp;
int success=1;
intcurrent_token;
intlineno=1;

intis_type(char *str)
{
int i=0;
char *keyword[] ={ "struct","int","char","float","void"};
if(strcmp(keyword[0],str)==0) {
return 3;
}
for(i=0;i<4;i++)
{
if(strcmp(keyword[i],str)==0)
{
return 1;
}
}
return 0;
}

enum
{
END,
TYPE,
CONST,
START,
O_FLOWER,
C_FLOWER,
COMMA,
SEMICOLON,
O_BRACKET,
C_BRACKET,
ID
};

int main()
{
fp=fopen("new.txt","r");
if(fp==NULL)
{
printf("error\n");
}
else
{
start();
if(success) {
printf("Successful completion\n"); }
else {
printf("Error in grammar\n"); }
}
}

void start()
{
advance();
if((current_token!=END) &&match_token(START) )
{
advance();
match_token(ID);
advance();
if(current_token==O_FLOWER)
{
advance();
variables();
if(match_token(C_FLOWER)) {
advance();
if(match_token(SEMICOLON) )
;
}
}
}
}

void variables()
{
variable();
advance();
if(current_token==COMMA) {
advance();
variables();
}
}

void variable()
{
if(match_token(TYPE)) {
advance();
if(match_token(ID)) {
advance();
dimensions();
}
}
}

void dimensions()
{
if(current_token==O_BRACKET) {
advance();
if(match_token(CONST)) {
advance();
if(current_token==C_BRACKET) {
advance();
dimensions();
}
}
}
}


void advance()
{ current_token=next_token();
switch(current_token) {
case ID :
printf("Current _token is Identifier\n");
break;

case CONST :
printf("Current _token is a numerical constant\n");
break;

case SEMICOLON :
printf("Current _token is ; \n");
break;

case COMMA :
printf("Current _token is , \n");
break;

case O_FLOWER :
printf("Current _token is { \n");
break;

case C_FLOWER :
printf("Current _token is } \n");
break;

case END :
printf("End of file \n");
break;

case O_BRACKET :
printf("Current_tokenos [ \n");
break;

case C_BRACKET :
printf("Current_token is ] \n");
break;

case START :
printf("Current_token is struct \n");
break;
default : ;
}
}

intnext_token()
{
charch;
int j;
while(1) {
ch=fgetc(fp);
if(ch==EOF)
return END;
else if(ch=='\n')
lineno++;
else if(ch==';')
return SEMICOLON;
else if(ch==',')
return COMMA;
else if(ch=='{')
return O_FLOWER;
else if(ch=='}')
return C_FLOWER;
else if(ch=='[')
return O_BRACKET;
else if(ch==']')
return C_BRACKET;
else if(isalpha(ch)) {
charstr[100];
int i=0;
do {
str[i++]=ch;
ch=fgetc(fp) ; }
while(isalpha(ch) | isdigit(ch));
fseek(fp,-1,SEEK_CUR);
j=is_type(str);
if(j==1)
return TYPE;
else if(j==3)
return START;
else
return ID;
}

else if(isdigit(ch)) {
do {
ch=fgetc(fp);}
while(isdigit(ch));

fseek(fp,-1,SEEK_CUR);
return CONST;
}
}

return 0;
}


intmatch_token(int token)
{
if(current_token==token)
return 1;
else
{
success=0;
printf("Error in line %d \n",lineno);

switch(token) {
case START :
printf("Expecting keyword struct\n");
break;

case TYPE :
printf("Expecting keyword \n");
break;

case ID :
printf("Expecting identifier \n");
break;

case CONST :
printf("Expecting a numeric constant \n");
break;


case SEMICOLON :
printf("Expecting a ;\n");
break;


case C_FLOWER :
printf("Expecting a } \n");
break;

default :printf("Error\n");
}

exit(0);

}

return success;

};

You might also like