zoukankan      html  css  js  c++  java
  • printf、sprintf与fprintf 的用法区分

    1: fprintf()
    #include <stdio.h> 
    fprintf是用于文件操作的,原型是int fprintf( FILE *stream, const char *format [, argument ]...);

    fprintf()函数根据指定的format(格式)发送信息(参数)到由stream(流)指定的文件.因此fprintf()可以使得信息输出到指定的文件.比如
    char name[20] = "Mary";
    FILE *out;
    out = fopen( "output.txt", "w" );
    if( out != NULL )
    fprintf( out, "Hello %s ", name );
    对于其输出格式参数,和printf()一样.
    fprintf()和printf()一样工作. fprintf()的返回值是输出的字符数,发生错误时返回一个负值.
    在有些地方,有这样的定义:printf(...)=fprintf(stdout,...).

    fprintf函数的用法

    用法举例:

    #include <stdio.h>
    #include <process.h>
     
    FILE *stream;
     
    void main( void )
    {
    int i = 10;
    double fp = 1.5;
    char s[] = "this is a string";
    char c = '
    ';
     
    stream = fopen( "fprintf.out", "w" );
    fprintf( stream, "%s%c", s, c );
     
    fprintf( stream, "%d
    ", i );
    fprintf( stream, "%f
    ", fp );
    fclose( stream );
    system( "type fprintf.out" );
    } 
     
    屏幕输出:

    this is a string
    10
    1.500000

    printf就是在屏幕打印出一段字符串来啊
    原型是int printf( const char *format [, argument]... );
    是标准输出。

    3:printf、sprintf与fprintf 的用法区分

    1.printf 是和标准输出文件(stdout)关联的,fprintf 则没有这个限制.
    2.fprintf是用于文件操作的,原型是int fprintf( FILE *stream, const char *format [, argument ]...);

    3.sprintf是格式化输出到一个字符串,fprintf是格式化输出到一个stream,通常是到文件。
    int  fprintf(  FILE  *stream,  const  char  *format  [,  argument  ]...); 
    int  sprintf(  char  *buffer,  const  char  *format  [,  argument]  ...  );

    文章转自:http://www.cnblogs.com/stli/admin/EditArticles.aspx

  • 相关阅读:
    SP笔记:交叉实现七行并成一行
    HTML tag 学习
    操作哈希表
    Efficient bipedal robots based on passivedynamic walkers
    Pushing People Around
    ZEROMOMENT PONTTHIRTY FIVE YEARS OF ITS LIFE

    Active Learning for RealTime Motion Controllers
    Accelerometerbased User Interfaces for the Control of a Physically Simulated Character
    Dynamic Response for Motion Capture Animation
  • 原文地址:https://www.cnblogs.com/AI-Algorithms/p/3391277.html
Copyright © 2011-2022 走看看