In: Computer Science
printf("You have entered %d arguments.\n", argc);
for (int i = 0; i < argc; ++i)
printf( "argument %d: %s\n",i+1,argv[i]);
please help me, this is c programming. thank you in advance.
(a).Program
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
void simpleFileWrite();
void simpleFileRead();
int countCharcter();
int main()
{
int count;
simpleFileWrite();
simpleFileRead();
count=countCharacter();
// printf("Number ofcharacter in file is
%d\n",count);
return 0;
}
void simpleFileWrite()
{
FILE *fp;
char ch;
//Create the file for write operation
fp=fopen("myfile.txt","w");
if(fp==NULL)
{
printf("Error on writing\n");
exit(0);
}
printf("Enter line of text until Ctrl+z\n");
while((ch=getchar())!=EOF)
{
putc(ch,fp);
}
fclose(fp);
printf("File created..\n");
}
void simpleFileRead()
{
FILE *fp;
char ch;
fp=fopen("myfile.txt","r");
if(fp==NULL)
{
printf("Error on writing\n");
exit(0);
}
printf("\n");
while(!feof(fp))
{
//takes the characters in the
character array
ch=getc(fp);
//and print the characters
printf("%c",ch);
}
printf("\n");
fclose(fp);
}
int countCharacter()
{
FILE *fp;
int count=0;
fp=fopen("myfile.txt","r");
if(fp==NULL)
{
printf("Error on writing\n");
exit(0);
}
while(!feof(fp))
{
getc(fp);
count++;
}
printf("\n");
fclose(fp);
return (count-1) ;
}
(b) Program
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
void stringHandle();
void mixedDataHandler();
int main()
{
stringHandle();
mixedDataHandler();
return 0;
}
void stringHandle()
{
FILE *fp;
char str[40];
int n,i;
fp=fopen("string.txt","w");
if(fp==NULL)
{
printf("Error on writing\n");
exit(0);
}
printf("Enter number of strings towrite\n");
scanf("%d",&n);
getchar();
printf("Enter %d strings\n",n);
for(i=0;i<n;i++)
{
gets(str);
fputs(str,fp);
fputs("\n",fp);
}
fclose(fp);
printf("File created..\n");
fp=fopen("string.txt","r");
if(fp==NULL)
{
printf("Error on writing\n");
exit(0);
}
printf("String in the files are....\n");
while(!feof(fp))
{
fgets(str,40,fp);
puts(str);
}
fclose(fp);
}
void mixedDataHandler()
{
FILE *fp;
char name[40];
int n,i,age;
float height;
fp=fopen("person.txt","w");
if(fp==NULL)
{
printf("Error on writing\n");
exit(0);
}
printf("Enter number of records\n");
scanf("%d",&n);
getchar();
printf("Enter %d records\n",n);
for(i=0;i<n;i++)
{
printf("Name Age Height?\n");
scanf("%s%d%f",name,&age,&height);
fprintf(fp,"%s %d %f",name,age,height);
}
fclose(fp);
printf("File created..\n");
fp=fopen("person.txt","r");
if(fp==NULL)
{
printf("Error on writing\n");
exit(0);
}
printf("String in the files are....\n");
while(!feof(fp))
{
fscanf(fp,"%s%d%f",name,&age,&height);
printf("%s %d %f\n" ,name,age,height);
}
fclose(fp);
}