Ce diaporama a bien été signalé.
Le téléchargement de votre SlideShare est en cours. ×

Write a few statements that open a file called --words-- and writes th.docx

Publicité
Publicité
Publicité
Publicité
Publicité
Publicité
Publicité
Publicité
Publicité
Publicité
Publicité
Publicité

Consultez-les par la suite

1 sur 3 Publicité

Write a few statements that open a file called --words-- and writes th.docx

Télécharger pour lire hors ligne

Write a few statements that open a file called \"words\" and writes the last word in the file to a file called \"lastword\".
help please
1 char ch file name [25 2 FILE *fp, *fn; 3 char name [20] t lastword\" 4 int i; 5 fp fopen words .txt\". \"a\" 7 fprintf (fp \"%s An\", name fclose (fp) 9 fopen words txt r\" 10 printf The contents of words .txt file are: \ \") 12 whiel (ch getc (fp) EOF) 13 14 printf \"%c\", ch 15 16 17 fclose (fp) 18 fp fope (\"f://words .txt \"r\"); n 19 fn fope (\"f://lastword .txt\", \"w\"); n 20 while fgets (fi name, 25 fp NULL) le 21 print %s\ fi le name 22 if(strcmp name, file name) 30) 23 24 fprintf fn Ann file name 25 26 27 f close (fp) 28 fclose (fn)
Solution
//Use the following code which has no compilation errors
#include <stdio.h>
int main(void)
{
char ch,file_name[25];
FILE *fp,*fn;
char name[20]={\"lastword\"};
int i;
fp=fopen(\"words.txt\",\"a\");
fprintf(fp,\"%s \ \",name);
fclose(fp);
fopen(\"words.txt\",\"r\");
printf(\"the contents of words.txt file are: \ \");
while((ch=getc(fp)!=EOF))
{
fprintf(\"%c\",ch);
}
fclose(fp);
fp=fopen(\"f://words.txt\",\"r\");
fn=fopen(\"f://lastword.txt\",\"w\");
while((fgets(file_name,25,fp))!=NULL)
{
printf(\"%s \ \",file_name);
if(strcmp(name,file_name)==0)
{
fprintf(fn,\"%s/n\",file_name);
}
}
fclose(fp);
fclose(fn);
}
.

Write a few statements that open a file called \"words\" and writes the last word in the file to a file called \"lastword\".
help please
1 char ch file name [25 2 FILE *fp, *fn; 3 char name [20] t lastword\" 4 int i; 5 fp fopen words .txt\". \"a\" 7 fprintf (fp \"%s An\", name fclose (fp) 9 fopen words txt r\" 10 printf The contents of words .txt file are: \ \") 12 whiel (ch getc (fp) EOF) 13 14 printf \"%c\", ch 15 16 17 fclose (fp) 18 fp fope (\"f://words .txt \"r\"); n 19 fn fope (\"f://lastword .txt\", \"w\"); n 20 while fgets (fi name, 25 fp NULL) le 21 print %s\ fi le name 22 if(strcmp name, file name) 30) 23 24 fprintf fn Ann file name 25 26 27 f close (fp) 28 fclose (fn)
Solution
//Use the following code which has no compilation errors
#include <stdio.h>
int main(void)
{
char ch,file_name[25];
FILE *fp,*fn;
char name[20]={\"lastword\"};
int i;
fp=fopen(\"words.txt\",\"a\");
fprintf(fp,\"%s \ \",name);
fclose(fp);
fopen(\"words.txt\",\"r\");
printf(\"the contents of words.txt file are: \ \");
while((ch=getc(fp)!=EOF))
{
fprintf(\"%c\",ch);
}
fclose(fp);
fp=fopen(\"f://words.txt\",\"r\");
fn=fopen(\"f://lastword.txt\",\"w\");
while((fgets(file_name,25,fp))!=NULL)
{
printf(\"%s \ \",file_name);
if(strcmp(name,file_name)==0)
{
fprintf(fn,\"%s/n\",file_name);
}
}
fclose(fp);
fclose(fn);
}
.

Publicité
Publicité

Plus De Contenu Connexe

Similaire à Write a few statements that open a file called --words-- and writes th.docx (20)

Plus par lez31palka (20)

Publicité

Write a few statements that open a file called --words-- and writes th.docx

  1. 1. Write a few statements that open a file called "words" and writes the last word in the file to a file called "lastword". help please 1 char ch file name [25 2 FILE *fp, *fn; 3 char name [20] t lastword" 4 int i; 5 fp fopen words .txt". "a" 7 fprintf (fp "%s An", name fclose (fp) 9 fopen words txt r" 10 printf The contents of words .txt file are: ") 12 whiel (ch getc (fp) EOF) 13 14 printf "%c", ch 15 16 17 fclose (fp) 18 fp fope ("f://words .txt "r"); n 19 fn fope ("f://lastword .txt", "w"); n 20 while fgets (fi name, 25 fp NULL) le 21 print %s fi le name 22 if(strcmp name, file name) 30) 23 24 fprintf fn Ann file name 25 26 27 f close (fp) 28 fclose (fn) Solution //Use the following code which has no compilation errors #include <stdio.h> int main(void) { char ch,file_name[25]; FILE *fp,*fn; char name[20]={"lastword"}; int i; fp=fopen("words.txt","a"); fprintf(fp,"%s ",name); fclose(fp);
  2. 2. fopen("words.txt","r"); printf("the contents of words.txt file are: "); while((ch=getc(fp)!=EOF)) { fprintf("%c",ch); } fclose(fp); fp=fopen("f://words.txt","r"); fn=fopen("f://lastword.txt","w"); while((fgets(file_name,25,fp))!=NULL) { printf("%s ",file_name); if(strcmp(name,file_name)==0) { fprintf(fn,"%s/n",file_name); } } fclose(fp); fclose(fn); }

×