1
votes

In my code I have a file named "f1.txt" and the text inside this file is a string "robin singh hero"

I have created a string [b]str[/b] of size [10] and a FILE type pointer variable *fp then assigned this pointer the address of the my file "f1.txt" from the fopen function.

Then I used fgets function which takes these three parameters (string,size,file pointer)

#include <stdio.h>

void main()
{
  char str[10];
  FILE *fp;

  fp = fopen("f1.txt","r");

  if(fp == NULL)
  {
  printf("File does not exists");
  exit(1);
  }

  while (fgets(str,8,fp)!=NULL){
    printf("%s",str);
  }
  printf("\n");
  fclose(fp);
  puts(str);
  puts(str);
}

I mentioned size 8 so it means fgets will read first eight characters (robin sin) from my file and store it to my string which is of size 10 and still 2 bytes of space in left in my string. then until my file reached a null it copies the next 8 characters (gh hero).

So I want to know this: 1. there are now 6 characters left, But the actual result is (robin singh hero)

  1. What is happening here in the second step when there are only 6 characters left.

  2. I wanted to know what's happened to my string str after the end of this while loop. so I did puts(str) and I am getting this value (ro) in the terminal.

Below is the actual result of this code: robin singh hero

ro

ro

1
regarding: printf("File does not exists"); This is not (necessarily) true. Suggest: perror( "fopen failed" ) as this will output both your error message and the text reason the system thinks the error occurred to stderruser3629249

1 Answers

3
votes

As you stated in the question that the file f1.txt contains this

robin singh hero

Now when below code gets executes

while (fgets(str,8,fp)!=NULL){
    printf("%s",str);
}

fgets() reads size minus one character i.e 7 character from file pointed by fp and store in str. So in first iteration printf() statement prints

robin s

then in second iteration it again tries to read 7 character i.e printf() statement prints

ingh he

and then again it tries to read 7 characters but there are only 3 char(including \n, if it is) so printf() statement prints

ro

Now the below code segment

printf("\n"); /* prints \n character on console */
fclose(fp); /* closes the file */
puts(str); /* when above while loop terminates str has ro in it, hence it prints ro on console */
puts(str); /* prints ro on console */

since you have called puts() twice, it prints ro twice.

From the manual page of fgets()

fgets() reads in at most one less than size characters from stream and stores them into the buffer pointed to by s. Reading stops after an EOF or a newline. If a newline is read, it is stored into the buffer. A terminating null byte (aq\0aq) is stored after the last character in the buffer.

Also note that void main() {} is not the standard main() prototype. You should be using

int main(void) {

   return 0;
}

From the C standard 5.1.2.2.1 Program startup

1 The function called at program startup is named main. The implementation declares no prototype for this function. It shall be defined with a return type of int and with no parameters:

int main(void) { /*...*/ }

or

with two parameters (referred to here as argc and argv, though any names may be used, as they are local to the function in which they are declared):

int main(int argc, char *argv[]) { /*...*/ }