zoukankan      html  css  js  c++  java
  • C语言fgets()函数

    char * fgets ( char * str, int num, FILE * stream );
    Get string from stream 从文件流读取字符串

    Reads characters from stream and stores them as a C string into str until (num-1) characters have been read or either a newline or a the End-of-File is reached, whichever comes first. 存够n-1个字符或遇到换行符或到文件末尾 结束。
    A newline character makes fgets stop reading, but it is considered a valid character and therefore it is included in the string copied to str.
    A null character is automatically appended in str after the characters read to signal the end of the C string.

    Parameters

    str
    Pointer to an array of chars where the string read is stored.
    num
    Maximum number of characters to be read (including the final null-character). Usually, the length of the array passed as str is used.
    stream
    Pointer to a FILE object that identifies the stream where characters are read from.
    To read from the standard input, stdin can be used for this parameter.

    Return Value

    On success, the function returns the same str parameter.
    If the End-of-File is encountered and no characters have been read, the contents of str remain unchanged and a null pointer is returned.
    If an error occurs, a null pointer is returned.
    Use either ferror or feof to check whether an error happened or the End-of-File was reached.

    #include<stdio.h>
    int main()
    {
    FILE *fp=fopen("D:\\我的资料库\\Documents\\readme.txt","r"); //文件路径要用\转义

    char str[256];
    fgets(str,256,fp);
    printf("%s",str);
    }


    读取整个文件:

     
    int main()
    {
    FILE * pFile;
    char buffer [100];

    pFile = fopen ("D:\\我的资料库\\Documents\\readme.txt" , "r");
    if (pFile == NULL) perror ("Error opening file");
    else
    {
    while ( ! feof (pFile) )
    {
    if ( fgets (buffer , 100 , pFile) != NULL )
    fputs (buffer , stdout); //打印到
    }
    fclose (pFile);
    }
    return 0;
    }


    FILE * stdout;
    Standard output stream标准输出流

    The standard output stream is the default destination of regular output for applications. It is usually directed to the output device of the standard console (generally, the screen). 是一般应用程序默认终端,通常定向到标准控制台的输出设备。

  • 相关阅读:
    .Net平台AOP技术概览
    ARP&ICMP
    .NET面向上下文、AOP架构模式(概述)
    Attribute在拦截机制上的应用
    .NET面向上下文、AOP架构模式(实现)
    使用RequireJS优化Web应用前端
    使用asp.net MVC4创建兼容各种设备显示的web应用程序
    entity framework for asp.net mvc
    jquery多功能软键盘插件
    优美登录页面源码(一)
  • 原文地址:https://www.cnblogs.com/youxin/p/2419980.html
Copyright © 2011-2022 走看看