c語言入門代碼大全「c語言或者符號怎麼打」

C語言 技能提升 系列文章(八)字元輸入/輸出

大家好,今天和大家聊一下字元的輸入/輸出。

// 從stream中讀取字元
int fgetc ( FILE * stream );
int putc ( int character, FILE * stream );
int getc ( FILE * stream );
// 從stream中讀取字元串
char * fgets ( char * str, int num, FILE * stream );
// 向stream中寫入字元
int fputc ( int character, FILE * stream );
//
int putc ( int character, FILE * stream );
// 向stream中寫入字元串
int fputs ( const char * str, FILE * stream );
// 從標準輸入(stdio)讀取一個字元
int getchar ( void );
// 從標準輸入(stdio)讀取一個字元串
char * gets ( char * str );
// 

putchar
Write character to stdout (function )
puts
Write string to stdout (function )
ungetc
Unget character from stream (function )

下面我們來看看實例代碼。

#include <stdio.h>
int main ( )  {
  FILE * pFile;
  int c;
  int n = 0;
  pFile=fopen ("myfile.txt","r");
  if (pFile==NULL)    
       perror ("Error opening file");
  else  {
    do {
      c = fgetc (pFile);
      if (c == '$') n++;
    } while (c != EOF);
    fclose (pFile);
  }
  return 0;
}
#include <stdio.h>
int main()
{
   FILE * pFile;
   char mystring [100];

   pFile = fopen ("myfile.txt" , "r");
   if (pFile == NULL)
     perror ("Error opening file");
   else {
     if ( fgets (mystring , 100 , pFile) != NULL )
       puts (mystring);
     fclose (pFile);
   }
   return 0;
}
#include <stdio.h>
int main ()
{
  FILE * pFile;
  char c;
  pFile = fopen ("alphabet.txt","w");
  if (pFile!=NULL) {
    for (c = 'A' ; c <= 'Z' ; c++)
      fputc ( c , pFile );
    fclose (pFile);
  }
  return 0;
}
#include <stdio.h>
int main ()
{
   FILE * pFile;
   char sentence [256];
   printf ("Enter sentence to append: ");
   fgets (sentence,256,stdin);
   pFile = fopen ("mylog.txt","a");
   fputs (sentence,pFile);
   fclose (pFile);
   return 0;
}
#include <stdio.h>
int main ()
{
  int c;
  puts ("Enter text. Include a dot ('.')"
           " in a sentence to exit:");
  do {
    c=getchar();
    putchar (c);
  } while (c != '.');
  return 0;
}
#include <stdio.h>
int main()
{
  char string [256];
  printf ("Insert your full address: ");
  // warning: unsafe (see fgets instead)
  gets (string); 
  printf ("Your address is: %sn",string);
  return 0;
}
#include <stdio.h>
int main ()  {
  char c;
  for (c = 'A' ; c <= 'Z' ; c++) 
    putchar (c);
  return 0;
}
#include <stdio.h>
int main ()  {
  char string [] = "Hello world!";
  puts (string);
}

今天的內容就這麼多了,以上的內容雖然看起來簡單。

但是,想要運用自如的話,還是需要大家勤加聯繫的。

原創文章,作者:投稿專員,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/251102.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
投稿專員的頭像投稿專員
上一篇 2024-12-13 17:21
下一篇 2024-12-13 17:21

相關推薦

發表回復

登錄後才能評論