0

I want to read file with C program here is the code:

#include <stdio.h>
#include <stdlib.h>

int main()
{
    FILE * fptr;

    fptr = fopen("text.txt","r");

    char arr[150];
    char c;
    int i;

    while(!feof(fptr) && i<5)
    {
        printf("%d\n",i++);
        fgets(arr,150,fptr);
        puts(arr);
    }

    fclose(fptr);
    return 0;
}

When executed the program wont stop and the characters printed are weird, i dont know what is going wrong ?

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • 6
    [Why is “while ( !feof (file) )” always wrong?](https://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong). And consider what the value of `i` is at the start of the `while` loop. It's not initialised. – kaylum Sep 28 '20 at 22:41
  • 3
    It would also be a good idea to check the return value of `fopen()` – r3mainer Sep 28 '20 at 22:43
  • 1
    You need to initialize `i`. As it is now, its value is undefined. – Tom Karzes Sep 28 '20 at 22:52
  • I tried before adding it, i just added it to stop the program but the printed char remains weird –  Sep 29 '20 at 00:22

2 Answers2

1

The part causing error in your program is :

while(!feof(fptr))

Better read : What is wrong with "while(!feof(fptr))" and Why it's bad to use feof() to control a loop

A simple program to read is below which checks if file is opened or not. It's a good practice to check if file you are to perform operations on is opened or not.

#include <stdio.h>
#include <stdlib.h>
int main()
{
   char ch;
   FILE *fp;

   fp = fopen("text.txt", "r"); // read mode

   if (fp == NULL) //Checking if file is open
   {
      perror("Error while opening the file.\n");
      exit(EXIT_FAILURE);
   }

   printf("The contents of %s file are:\n", file_name);

   while((ch = fgetc(fp)) != EOF){
      printf("%c", ch);   //Avoided creating a buffer 
   }

   fclose(fp);
   return 0;
}
Mohit Sharma
  • 338
  • 2
  • 13
0

#Though not so much realevant!!

I think the easiest way to read/write from/to file is using freopen() function. You can use scanf() & printf() in case of C and cin & cout in case C++ to read or write from file with this function.

Read from file: freopen("input.txt","r",stdin); where input.txt is filename.

Write to file: freopen("output.txt","w",stdout); no need to create output.txt your own. The file is automatically created when the program is executed.

na_yan
  • 56
  • 6
  • Isn't is ok now? – na_yan Sep 29 '20 at 02:45
  • By the way, I believe what you wrote would have been appropriate as a comment, not as an answer, because what you write does not really answer the question. I am aware that you cannot write comments yet on other people's questions (you need 50 reputation points for that). However, I'm afraid that using the answer feature for comments will get your answers downvoted. – Andreas Wenzel Sep 29 '20 at 02:52
  • Thank you for correcting me, specially for you suggestion. – na_yan Sep 29 '20 at 02:55
  • Personally, I don't see any advantage in using `freopen` in this case. You can always use [`fscanf`](https://en.cppreference.com/w/c/io/fscanf) and [`fprintf`](https://en.cppreference.com/w/c/io/fprintf) instead of `scanf` and `printf`, so there is little point in using `freopen`, in my opinion. – Andreas Wenzel Sep 29 '20 at 03:13
  • In my opinion, it helps because when I comment out those two lines it works normal without file input or output. On the other hand, putting differen input every time needs much work in the file but easy in console. But when you use fscanf() or fprint() you cannot do that immediately. – na_yan Sep 29 '20 at 04:01
  • 1
    Reading and writing to files normally does not mean that you want to lose access to `stdin` and `stdout`. How to ask for next menu option after you redirected `stdin` to some file? Or how to print status updates to the terminal during writing to the file? If that was a legal option, the user could redirect I/O during caling the program. – Gerhardh Sep 29 '20 at 09:37