C++相当于C fgets

Sap*_*pan 0 c c++

我期待找到C fgets的C++ fstream等效函数.我尝试使用fstream的get函数,但没有得到我想要的.get函数不提取delim字符,而fgets函数用于提取它.所以,我写了一个代码来从我的代码本身插入这个delim字符.但它给出了奇怪的行为.请参阅下面的示例代码;

#include <stdio.h>
#include <fstream>
#include <iostream>

int main(int argc, char **argv)
{
    char str[256];
    int len = 10;
    std::cout << "Using C fgets function" << std::endl;
    FILE * file = fopen("C:\\cpp\\write.txt", "r");
    if(file == NULL){
      std::cout << " Error opening file" << std::endl;
    }
    int count = 0;
    while(!feof(file)){
        char *result = fgets(str, len, file);
        std::cout << result << std::endl ;
        count++;
    }
    std::cout << "\nCount = " << count << std::endl;
    fclose(file);


std::fstream fp("C:\\cpp\\write.txt", std::ios_base::in);
int iter_count = 0; 
 while(!fp.eof() && iter_count < 10){

    fp.get(str, len,'\n');
    int count = fp.gcount();
    std::cout << "\nCurrent Count = " << count << std::endl;
    if(count == 0){
        //only new line character encountered
        //adding newline character
        str[1] = '\0';
        str[0] = '\n';
        fp.ignore(1, '\n');
        //std::cout << fp.get(); //ignore new line character from stream
    }
    else if(count != (len -1) ){

         //adding newline character
        str[count + 1] = '\0';
        str[count ] = '\n';
        //std::cout << fp.get(); //ignore new line character from stream
        fp.ignore(1, '\n');
        //std::cout << "Adding new line \n";
    }       

    std::cout << str  << std::endl;
    std::cout << " Stream State : Good: " << fp.good() << " Fail: " << fp.fail() << std::endl;

    iter_count++;
}
std::cout << "\nCount = " << iter_count <<  std::endl;
fp.close();


return 0;
Run Code Online (Sandbox Code Playgroud)

}

我使用的txt文件是write.txt,内容如下:

This is a new lines.
Now writing second
line
DONE
Run Code Online (Sandbox Code Playgroud)

如果您观察我的程序,我首先使用fgets函数,然后在同一文件上使用get函数.在get函数的情况下,流状态变坏.

谁能指出我这里出了什么问题?

更新:我现在发布一个最简单的代码,在我的最后不起作用.如果我现在不关心delim字符,只需使用getline一次读取整个文件10个字符:

void read_file_getline_no_insert(){
    char str[256];
    int len =10;
    std::cout << "\nREAD_GETLINE_NO_INSERT FUNCITON\n" << std::endl;
    std::fstream fp("C:\\cpp\\write.txt", std::ios_base::in);
    int iter_count = 0; 
     while(!fp.eof() && iter_count < 10){

        fp.getline(str, len,'\n');
        int count = fp.gcount();
        std::cout << "\nCurrent Count = " << count << std::endl;
        std::cout << str  << std::endl;
        std::cout << " Stream State : Good: " << fp.good() << " Fail: " << fp.fail() << std::endl;   
        iter_count++;
    }
    std::cout << "\nCount = " << iter_count <<  std::endl;
    fp.close();

}
int main(int argc, char **argv)
{

    read_file_getline_no_insert();  

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

如果我们看到上面代码的输出:

READ_GETLINE_NO_INSERT FUNCITON

Current Count = 9
This is a
 Stream State : Good: 0 Fail: 1

Current Count = 0

 Stream State : Good: 0 Fail: 1
Run Code Online (Sandbox Code Playgroud)

您会看到流的状态变为Bad并且设置了失败位.我无法理解这种行为.

Rgds Sapan

Rob*_*Rob 6

std::getline() 将从流中读取一个字符串,直到它遇到一个分隔符(默认为换行符).

fgets()与之不同,std :: getline()会丢弃分隔符.但是,同样不同的是fgets(),它将读取整行(可用内存允许),因为它与a std::string而不是a一起工作char *.这使得在实践中使用起来更容易一些.

std::istream(它是所有输入流的基类)派生的所有类型也有一个被调用的成员函数getline(),它更像是fgets()- 接受a char *和缓冲区大小.它仍然丢弃了分隔符.

特定于C++的选项是重载函数(即可以在多个版本中使用),因此您需要阅读文档以确定哪个适合您的需求.