Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ fstream get(char*,int)如何操作空行?_C++_Get_Fstream - Fatal编程技术网

C++ fstream get(char*,int)如何操作空行?

C++ fstream get(char*,int)如何操作空行?,c++,get,fstream,C++,Get,Fstream,strfile.cpp中的代码: #include <fstream> #include <iostream> #include <assert.h> #define SZ 100 using namespace std; int main(){ char buf[SZ]; { ifstream in("strfile.cpp"); assert(in); ofstream out("strfile.out"); asse

strfile.cpp中的代码:

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

#define SZ 100

using namespace std;

int main(){
char buf[SZ];
{
    ifstream in("strfile.cpp");
    assert(in);
    ofstream out("strfile.out");
    assert(out);
    int i = 1;

    while(!in.eof()){
        if(in.get(buf, SZ))
            int a = in.get();
        else{
            cout << buf << endl;
            out << i++ << ": " << buf << endl;
            continue;
        }
        cout << buf << endl;
        out << i++ << ": " << buf << endl;
    }
}
return 0;
}
#包括
#包括
#包括
#定义SZ 100
使用名称空间std;
int main(){
char-buf[SZ];
{
ifstream in(“strfile.cpp”);
断言(in);
流出(“strfile.out”);
断言(输出);
int i=1;
而(!in.eof()){
if(in.get(buf,SZ))
int a=in.get();
否则{

cout因为
ifstream::get(char*,streamsize)
将在流上留下分隔符(在本例中为
\n
),所以您的调用永远不会前进,因此您的调用程序似乎在无休止地读取空行

相反,您需要确定新行是否正在流中等待,并使用.get()中的
或.ignore(1)中的
将其移动过去:

ifstream-in(“strfile.cpp”);
流出(“strfile.out”);
int i=1;

“如何操作”是什么意思?你想忽略空行吗?你的代码在空行上失败了吗?对不起,我的英语很差。当有空行时,我会怎么做?用行号输出它。如果你想复制文件并在每行或100个字符后插入行号,以先发生的为准,这对我来说很好。你能更具体地介绍你吗r问题?@lanmezhe:你想只在空行上输出行号吗?@molbdnilo我知道怎么做,谢谢你!谢谢,我知道getline()可以做到。我只想知道怎么做,只使用get()。谢谢,这很有效!我想问题是当get(buf,SZ)时读到空行,程序出错了。是这样吗?发生的是它将
\n
留在流中,重复调用
get(buf,SZ)
在换行时停止,因此您永远不会前进。它实际上只是没有移动到行的末尾。
1: #include <fstream>
2: #include <iostream>
3: #include <assert.h>
4: ...(many empty line)
ifstream in("strfile.cpp");
ofstream out("strfile.out");

int i = 1;
out << i << ": ";

while (in.good()) {
    if (in.peek() == '\n') {
        // in.get(buf, SZ) won't read newlines
        in.get();
        out << endl << i++ << ": ";
    } else {
        in.get(buf, SZ);
        out << buf;      // we only output the buffer contents, no newline
    }
}

// output the hanging \n
out << endl;

in.close();
out.close();