C Programming - Input / Output - Discussion
Discussion Forum : Input / Output - General Questions (Q.No. 7)
7.
On executing the below program what will be the contents of 'target.txt' file if the source file contains a line "To err is human"?
#include<stdio.h>
int main()
{
int i, fss;
char ch, source[20] = "source.txt", target[20]="target.txt", t;
FILE *fs, *ft;
fs = fopen(source, "r");
ft = fopen(target, "w");
while(1)
{
ch=getc(fs);
if(ch==EOF)
break;
else
{
fseek(fs, 4L, SEEK_CUR);
fputc(ch, ft);
}
}
return 0;
}
Answer: Option
Explanation:
The file source.txt is opened in read mode and target.txt is opened in write mode. The file source.txt contains "To err is human".
Inside the while loop,
ch=getc(fs); The first character('T') of the source.txt is stored in variable ch and it's checked for EOF.
if(ch==EOF) If EOF(End of file) is true, the loop breaks and program execution stops.
If not EOF encountered, fseek(fs, 4L, SEEK_CUR); the file pointer advances 4 character from the current position. Hence the file pointer is in 5th character of file source.txt.
fputc(ch, ft); It writes the character 'T' stored in variable ch to target.txt.
The while loop runs three times and it write the character 1st and 5th and 11th characters ("Trh") in the target.txt file.
Discussion:
21 comments Page 1 of 3.
Ritesh_IIITA said:
1 decade ago
Index Character
[0] T
[1] o
[2] space
[3] e
[4] r
[5] r
[6] space
[7] i
[8] s
[9] space
[10] h
[11] u
[12] m
[13] a
[14] n
[15] EOF
Now when while loop execution begins:
1st: ch=T and pointer will get incremented to point at index:1 i.e o.
2nd: check if ch==EOF result is no it will go to else block.
3rd: in else block processor will now seek new location by increment of 4 in pointers location so now pointer is pointing at index:5, in the next instruction it will print the value of ch i.e T.
4th: now 2nd iteration of while loop begins an now compiler will store a character from location where pointer is pointing i.e index:5 and store it into ch(==r) and increment the pointer by 1 thereby pointing to index:6.
5th: check if ch==EOF result is no it will go to else block.
6th: in else block processor will now seek new location by increment of 4 in pointers location. current index location is:6, so now pointer is pointing at index:10(6+4), in the next instruction it will print the value of ch i.e r.
7th: now 3rd iteration of while loop begins an now compiler will store a character from location where pointer is pointing i.e index:10 and store it into ch(==h) and increment the pointer by 1 thereby pointing to index:11.
8th: check if ch==EOF result is no it will go to else block.
9th: in else block processor will now seek new location by increment of 4 in pointers location. current index location is:11, so now pointer is pointing at index:15(11+4), in the next instruction it will print the value of ch i.e h.
10th: now 4th iteration of while loop begins an now compiler will store a character from location where pointer is pointing i.e index:15 and store it into ch(==EOF) and increment the pointer by 1 thereby pointing to index:16.
11th: check if ch==EOF result is true it will go inside of if block and their execute break statement and come out of while loop.
This is how step by step this program will get executed.
[0] T
[1] o
[2] space
[3] e
[4] r
[5] r
[6] space
[7] i
[8] s
[9] space
[10] h
[11] u
[12] m
[13] a
[14] n
[15] EOF
Now when while loop execution begins:
1st: ch=T and pointer will get incremented to point at index:1 i.e o.
2nd: check if ch==EOF result is no it will go to else block.
3rd: in else block processor will now seek new location by increment of 4 in pointers location so now pointer is pointing at index:5, in the next instruction it will print the value of ch i.e T.
4th: now 2nd iteration of while loop begins an now compiler will store a character from location where pointer is pointing i.e index:5 and store it into ch(==r) and increment the pointer by 1 thereby pointing to index:6.
5th: check if ch==EOF result is no it will go to else block.
6th: in else block processor will now seek new location by increment of 4 in pointers location. current index location is:6, so now pointer is pointing at index:10(6+4), in the next instruction it will print the value of ch i.e r.
7th: now 3rd iteration of while loop begins an now compiler will store a character from location where pointer is pointing i.e index:10 and store it into ch(==h) and increment the pointer by 1 thereby pointing to index:11.
8th: check if ch==EOF result is no it will go to else block.
9th: in else block processor will now seek new location by increment of 4 in pointers location. current index location is:11, so now pointer is pointing at index:15(11+4), in the next instruction it will print the value of ch i.e h.
10th: now 4th iteration of while loop begins an now compiler will store a character from location where pointer is pointing i.e index:15 and store it into ch(==EOF) and increment the pointer by 1 thereby pointing to index:16.
11th: check if ch==EOF result is true it will go inside of if block and their execute break statement and come out of while loop.
This is how step by step this program will get executed.
Vaibhav said:
1 decade ago
I would say that it is better to think in terms of index rather than the number of the character in file. See it as first when the while loop starts T is stored in ch as it is at 0 index. Then, fgetc increases the pointer by 1 and then fseek increases the pointer by 4 which makes the value at index 5 to be stored in ch and the same happens for the next time when the character at index 10 is stored in ch. Thus, it makes 'Trh'. As simple as that :D.
Monakons said:
8 years ago
sizeof (int) and sizeof (long) are equal most of the times but you will find some platforms where int is 32 bits and long is 64 bits.
fseek waits for long int as the second argument but it's perfectly normal to use 4 (int) rather than 4L (long int), especially if the size is the same.
fseek waits for long int as the second argument but it's perfectly normal to use 4 (int) rather than 4L (long int), especially if the size is the same.
Judaism said:
1 decade ago
It will write 1st and 6th and 11th, not "1st and 5th and 11th".
first loop: getc will store 'T' to ch and advances 1 character, then fseek will advances 4 character. it total 5 advances from 1st. so it will write 6th for the second loop
first loop: getc will store 'T' to ch and advances 1 character, then fseek will advances 4 character. it total 5 advances from 1st. so it will write 6th for the second loop
Shakthi Yokesh said:
1 decade ago
I didn't understand why it writes 1st and 5th and 11th characters("Trh") instead of 1st and 5th and 9th characters("Trs").
Somebody please clarify my doubt....
Somebody please clarify my doubt....
Aditi said:
1 decade ago
Can any one explain me that - Why while loop execute three times. Act I didn't understand the condition i.e. while (1).
Please clarify my doubt. Thanks in advance.
Please clarify my doubt. Thanks in advance.
Hemc said:
1 decade ago
It will be 1st. 6th and 11th.
Because after that it skip 4 char including space and then after are it skip 4 char including two spaces and print h.
Because after that it skip 4 char including space and then after are it skip 4 char including two spaces and print h.
Anupss said:
1 decade ago
Here while loop executes only three times because, if it executes 4th time then EOF in which program execution stops and condition will be false.
Judaism said:
1 decade ago
ch=getc(fs); the file pointer advances 1 character. so it will be 1st. 6th and 11th.
Kanchan Tiwari said:
8 years ago
I am not getting the while condition 1 is used for what. What does while (1) means?
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers