Programming C : stdio.h Example 2

 getchar: read a character from stdin

    

//Header file:     #include <stdio.h>  
//Declaration:     int getchar(void); 
//Return:          returns EOF on error. 


  #include <stdio.h>

  int main(void)
  {
    char s[256], *p;

    p = s;
    printf("input char:");
    while((*p++ = getchar())!= '\n');
    *p = '\0'; /* add null terminator */

    printf(s);

    return 0;
  }

         
/*
input char:123
123
*/

perror: maps error message to the global variable errno and outputs that string to stderr

    

//Header file:     #include <stdio.h>  
//Declaration:     void perror(const char *str); 
  

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

  int main(void)
  {

    perror("File error ");
  }

         
/*
File error : No Error
*/

putc: writes a character to the output stream

    

//Header file:     #include <stdio.h>  
//Declaration:     int putc(int ch, FILE *stream); 
//Return:          returns the character written on success and EOF on error. 


  

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

    if((fp=fopen("test", "w"))==NULL) {
      printf("Cannot open file.\n");
      exit(1);
    } 
 
    char *str = "www.java2s.com";
 
    for(; *str; str++){
        putc(*str, fp);
    }
  }

puts: writes the string to the standard output device

    

//Header file:     #include <stdio.h>  
//Declaration:     int puts(const char *str); 
//Return:          returns a nonnegative value on success or an EOF upon failure. 

  #include <stdio.h>
  #include <string.h>

  int main(void)
  {
    char str[80];

    strcpy(str, "this is an example");

    puts(str);

    return 0;
  }

         
/*
this is an example
*/
   

rename: changes the file name

    

//Header file:     #include <stdio.h>  
//Declaration:     int rename(const char *oldfname, const char *newfname); 
//Return:          returns zero on success or nonzero on error. 

  

  #include <stdio.h>

  int main(int argc, char *argv[])
  {
    if(rename("oldName", "newName") != 0){
       printf("Rename Error");
    }
    return 0;
  }

scanf: skip the integer between the two strings

  
  #include <stdio.h>
  int main(void)
  {
    char str[80], str2[80];
    int i;

    printf("skip the integer between the two strings:");
    scanf("%s%*d%s", str, str2);

    return 0;
  }

scanf: read input

    

//Header file:     #include <stdio.h>  
//Declaration:     int scanf(const char *format, ...); 

// The scanf() Format Specifiers 

    
//Code Meaning
//%a:  Read a floating-point value (C99 only) 
//%A:  Same as %a (C99 only) 
//%c:  Read a single character 
//%d:  Read a decimal integer 
//%i:  Read an integer in either decimal, octal, or hexadecimal format 
//%e:  Read a floating-point number 
//%E:  Same as %e 
//%f:  Read a floating-point number 
//%F:  Same as %f (C99 only) 
//%g:  Read a floating-point number 
//%G:  Same as %g 
//%o:  Read an octal number 
//%s:  Read a string 
//%x:  Read a hexadecimal number 
//%X:  Same as %x 
//%p:  Read a pointer 
//%n:  Receive an integer value equal to the number of characters read so far 
//%u:  Read an unsigned decimal integer 
//%[ ]:  Scan for a set of characters 
//%%:  Read a percent sign 

  


  
  #include <stdio.h>

  int main(void)
  {
    char str[80], str2[80];
    int i;

    scanf("%79s", str); //scanf up to 79 chars into str

    return 0;
  }

setvbuf: sets the buffer for stream to be buffer, with a size of 'size_t size'

    

//Header file:     #include <stdio.h>  
//Declaration:     int setvbuf(FILE *stream, char *buf, int mode, size_t size); 
//Return:          returns zero on success, nonzero on failure. 


// Possible values of mode are _IOFBF, _IONBF, and _IOLBF.
//_IOFBF: full buffering.
//_IOLBF: line buffered.
//_IONBF: no buffering.
  
#include <stdio.h>

int main ()
{
  FILE *fp;

  fp=fopen ("myfile.txt","w");

  setvbuf ( fp , NULL , _IOFBF , 1024 );

  // File operations here

  fclose (fp);

  return 0;
}

sprintf: is identical to printf() except that the output is a char array

    

//Declaration:  int sprintf(char *buf, const char *format, ...); 
//Return:       returns the number of characters actually placed into the array. 


#include <stdio.h>

int main(void){
  char str[80];

  sprintf(str,"%s %d %c", "one", 2, '3');

  printf("%s", str);
}

         
/*
one 2 3*/ 

tmpfile: opens a temporary binary file for read/write operations and returns a pointer to the stream

//Declaration:  FILE *tmpfile(void); 
//Return:       returns a null pointer on failure. 

#include <stdio.h>

int main(void){
  FILE *temp;
  if((temp=tmpfile())==NULL) {
    printf("Cannot open temporary work file.\n");
    exit(1);
  }
}

ungetc: Put a character back to the input stream

 
//Declaration:  int ungetc(int ch, FILE *stream); 
//Return:       returns ch on success or EOF on failure. 

#include <stdio.h>

int main ()
{
  FILE * fp;
  int c;
  char buffer [256];
  fp = fopen ("test.txt","rt");
 
  if (fp==NULL)
    perror ("Error opening file");
  else {
    while (!feof (fp))
    {
      c=getc (fp);
      if (c == '#')
        ungetc ('@',fp);
      else
        ungetc (c,fp);
      fgets (buffer,255,fp);
      fputs (buffer,stdout);
    }
  }

  return 0; 
}