Question

Part D. On executing the below program, what will be the output of the following program...

Part D. On executing the below program, what will be the output of the following program if the source file contains a line "This is a sample."? [5 marks]

#include <stdio.h>

int main()

{ char ch;

int k, n=0;

FILE * fs = fopen("source.txt", "r");

while(1) {

ch = fgetc(fs);

if(ch == EOF) break;

else { for(k=0;k<4;k++)

fgetc(fs);

printf("%c", ch);

n += k; }

}

printf("%d\n", n);

fclose(fs);

return 0;

}

----------------------------------

Any one can explain the question to me including the output(not only the output)? many thanks

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <stdio.h>

int main()
{
char ch; // character variable ch for holding character value
int k, n=0; // integer type variable declaration with n initialized with 0
FILE * fs = fopen("source.txt", "r"); //file type pointer fs which holds file status in fs pointer variable , file name source.txt in read mode
while(1) //while there is a content in the file keep reading
{
ch = fgetc(fs); //get first character from file pointer fs and copy in ch
if(ch == EOF) break; // if ch is end of file character then terminate program
else
{
for(k=0;k<4;k++) // else print 4 character from file
fgetc(fs); // read from fs variable
printf("%c",fs);
printf("%c", ch); // print character variable content on screen
n += k; // increment counter of n which is initialized 0 above
}
}
printf(" %d \n ", n); // print value of n
fclose(fs);
return 0;
}

WHAT EVER THE CHARACTER CONTAINS IN THE FILE +1 WILL BE THE VALUE OF VARIABLE N AT THE LAST AND CH VARIABLE FETCH FIRST CHARACTER OF THE NEW WORD AS I HAVE CAPITALIZE THAT EACH CHARACTER WHICH IT READS.

if you have any doubt then please ask me without any hesitation in the comment section below , if you like my answer then please thumbs up for the answer , before giving thumbs down please discuss the question it may possible that we may understand the question different way and we can edit and change the answers if you argue, thanks :)

Add a comment
Know the answer?
Add Answer to:
Part D. On executing the below program, what will be the output of the following program...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • My question is listed the below please any help this assignment ; There is a skeleton...

    My question is listed the below please any help this assignment ; There is a skeleton code:  copy_file_01.c #include <stdio.h> #include <stdlib.h> int main(int argc, char* argv[]) { char ch ; FILE *source , *target;    if(argc != 3){ printf ("Usage: copy file1 file2"); exit(EXIT_FAILURE); } source = fopen(argv[1], "r"); if (source == NULL) { printf("Press any key to exit...\n"); exit(EXIT_FAILURE); } target = fopen(argv[2], "w"); if (target == NULL) { fclose(source); printf("Press any key to exit...\n"); exit(EXIT_FAILURE); } while ((ch...

  • Write a C Program that uses file handling operations of C language to copy the contents...

    Write a C Program that uses file handling operations of C language to copy the contents of a file called “original.dat” to another file called “copy2.dat”. Here, you will read and write from/to the files line by line with the help of fgets() and fputs() functions. Sample file “original.dat” has been provided. Please note that the new file “copy2.dat” should have exactly same contents as in “original.dat”. #include <stdio.h> int main() { FILE *fptr1, *fptr2; if((fptr1=fopen("original.dat","r"))==NULL) printf("\n Error opening File");...

  • C Programming I was given this code to display the following as the output but it...

    C Programming I was given this code to display the following as the output but it does not seem to work. what is wrong? or can someone guide me through the steps? thanks! I have created the txt file named myfile.txt but the program will not compile. please help. I am forced to use Microsoft visual studio. This program makes a duplicate copy of a file (reads input from a file and write output to another file) 1 #include <stdio.h>...

  • Convert C to C++ I need these 4 C file code convert to C++. Please Convert...

    Convert C to C++ I need these 4 C file code convert to C++. Please Convert it to C++ //////first C file: Wunzip.c #include int main(int argc, char* argv[]) { if(argc ==1){ printf("wunzip: file1 [file2 ...]\n"); return 1; } else{ for(int i =1; i< argc;i++){ int num=-1; int numout=-1; int c; int c1;    FILE* file = fopen(argv[i],"rb"); if(file == NULL){ printf("Cannot Open File\n"); return 1; } else{ while(numout != 0){    numout = fread(&num, sizeof(int), 1, file);    c...

  • In which line of the following code the first error is appeared (if any)? #include 2...

    In which line of the following code the first error is appeared (if any)? #include 2 int main() <stdio.h> 4 file p: 6 fopen("newname . txt", "rb"); *p if = (P NULL) 8 perror( "Error opening file") 9 else f while (fgetc(p)- eof) 12 13 if (feof(p)) 14 15 else 16 17 Fclose(p) 18 19 20 return e; 21 printf("%d\n", n); puts("End-of-File was not reached.) 23 O 10 0 17

  • Writing a program in C please help!! My file worked fine before but when I put...

    Writing a program in C please help!! My file worked fine before but when I put the functions in their own files and made a header file the diplayadj() funtion no longer works properly. It will only print the vertices instead of both the vertices and the adjacent like below. example input form commnd line file: A B B C E X C D A C The directed edges in this example are: A can go to both B and...

  • C Language Programming. Using the program below - When executing on the command line only this...

    C Language Programming. Using the program below - When executing on the command line only this program name, the program will accept keyboard input and display such until the user does control+break to exit the program. The new code should within only this case situation “if (argc == 1){ /* no args; copy standard input */” Replace line #20 “filecopy(stdin, stdout);” with new code. Open read a text file “7NoInputFileResponse.txt” that contains a message “There were no arguments on the...

  • Create another program that will prompt a user for input file. The user will choose from...

    Create another program that will prompt a user for input file. The user will choose from 4 choices: 1. Display all positive balance accounts. 2. Display all negative balance accounts. 3. Display all zero balance accounts. 4. End program. C PROGRAMMING my code so far #include<stdlib.h> #include<stdio.h> int main(void) { int request; int account; FILE *rptr; char name[20]; double balance; FILE *wptr;    int accountNumber;    if((wptr = fopen("clients.dat","w")) == NULL) { puts("File could not be opened"); } else {...

  • Write a complete C program that inputs a paragraph of text and prints out each unique...

    Write a complete C program that inputs a paragraph of text and prints out each unique letter found in the text along with the number of times it occurred. A sample run of the program is given below. You should input your text from a data file specified on the command line. Your output should be formatted and presented exactly like the sample run (i.e. alphabetized with the exact spacings and output labels). The name of your data file along...

  • The program is written in c. How to implement the following code without using printf basically...

    The program is written in c. How to implement the following code without using printf basically without stdio library? You are NOT allowed to use any functions available in <stdio.h> . This means you cannot use printf() to produce output. (For example: to print output in the terminal, you will need to write to standard output directly, using appropriate file system calls.) 1. Opens a file named logfle.txt in the current working directory. 2. Outputs (to standard output usually the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT