Fgetc reads character with value = -1

Asked
Active3 hr before
Viewed126 times

5 Answers

readscharacter
90%

Reads a character from a file,The fgetc() function reads the character at the current file position in the specified file, and increments the file position.,The return value of fgetc() has the type int. If the file position is at the end of the file, or if the end-of-file flag was already set, fgetc() returns EOF and sets the end-of-file flag. If you convert the function’s return value to char, you might no longer be able to distinguish a value of EOF from a valid character such as '\xFF'.,getc(), getchar(), putc(), fputc(), fgets(), fgetwc(), getwc()

Reads a character from a file

#include <stdio.h>
   intfgetc( FILE *fp );
load more v
88%

Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.,fgetwc is the wide-character version of fgetc; it reads c as a multibyte character or a wide character according to whether stream is opened in text mode or binary mode.,fgetc is equivalent to getc, but is implemented only as a function, rather than as a function and a macro.,Each of these functions reads a single character from the current position of the file associated with stream. The function then increments the associated file pointer (if defined) to point to the next character. If the stream is at end of file, the end-of-file indicator for the stream is set.

Syntax

int fgetc(
   FILE * stream
);
wint_t fgetwc(
   FILE * stream
);
load more v
72%

when calling the function: fopen(), always check (!=NULL) the returned value to assure the operation was successful. – user3629249 Nov 5 '16 at 6:19 , Check whether a lowercase alphabet-only string has any character repetition ,Always do error checking for all standard functions. What if fopen() fails? ,Making statements based on opinion; back them up with references or personal experience.

You have another problem. You don't store the char read from the file at all. Do something like:

   int in ;

   while (( in = fgetc(fp)) != EOF) {
      in = abc[i];
      printf("%c", abc[i]);
      i++;
   }
load more v
65%

fgetc()                  Read a Character from a Stream
 
 #include   <stdio.h>
 
 int        fgetc(stream);
 FILE       *stream;                     Pointer to file structure
 
 
    fgetc() reads a character from 'stream' at the current position. The
    file pointer is incremented to point to the next character.
 
    Returns:    The character read.  EOF is returned on error or
                end-of-file. However, because EOF is a legitimate integer
                value that can be read by this function, feof() or
                ferror() should be used to verify an end-of-file or error
                condition.
 
      Notes:    The fgetchar() function is equivalent to fgetc(stdin).
 
                fgetc() is similar to getc(), but getc() is a macro,
                while fgetc() is a function.
 
  -------------------------------- Example ---------------------------------
 
    The following statements open an existing file and print out its
    contents.
 
         #include <stdio.h>
 
         FILE *in;
         int next_ch;
 
         main()
         {
              if ((in = fopen("alpha.bet","r+"))!= NULL) {
                  while(!feof(in)) {
                      next_ch = fgetc(in);
                      printf("%c",next_ch);
                  }
                  fclose(in);
               }
          }

Seealso:


 fgetchar() 
 getc() 
 getchar() 
 fputc() 

75%

fgetc reads a single character from the stream associated with the FILE object addressed by f and returns the character. , fgetc returns the next input character, or EOF if no character can be read. , fgetc is implemented by an actual function call, not a built-in function, so it is slower than getc . (However, less code is generated.) , getc , getchar , ungetc

#include <stdio.h>

   int fgetc(FILE *f);
load more v