zoukankan      html  css  js  c++  java
  • 使用ifstream时碰到的一个小问题

     下面这段代码:

     #include <iostream>

    #include <fstream>
    using namespace std;

    main()
    {
        
    char * filename = (char *)malloc(40);
        strcpy(filename,
    "/home/admin/local/apache/logs/access_log");
        ifstream ifs(filename);
        
        
    if(ifs.good()){
            cout
    <<"open success"<<endl;
            
    string temp;
            getline(ifs,temp);
            cout
    <<temp<<endl;
        }   
    }

    注意标红的40,这个正是下面

    /home/admin/local/apache/logs/access_log的长度,但是ifstream打开失败

    把这个40改成41,问题解决!从这一点可以间断判断:

    ifstream打开文件的时候,要求c-style的string后面一定要加上\0 


  • 相关阅读:
    HDU 1198
    HDU 1863
    HDU 1879
    HDU 1233
    HDU 1232
    HDU 1829
    HDU 2473
    hdu 1829 A Bug's Life
    hdu 3038 How Many Answers Are Wrong
    hdu 1198 Farm Irrigation
  • 原文地址:https://www.cnblogs.com/welkinwalker/p/2104050.html
Copyright © 2011-2022 走看看