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);
}

今天的内容就这么多了,以上的内容虽然看起来简单。

但是,想要运用自如的话,还是需要大家勤加联系的。

二维码

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 394062665@qq.com 举报,一经查实,本站将立刻删除。

发表评论

登录后才能评论
客服