从文件中获取数据时如何放置换行符?

时间:2019-04-24 14:56:15

标签: c++

我需要从文件中获取数据,然后想将其保存为字符串,将其保存为字符串后,我想将数据放入从中获取数据的同一文件中。问题是在获取数据时,它是没有发现我的新线。 让我们看一下示例。

我有一个文件“ check.txt”,其中的数据如下所示: ABC 换行 DEF 换行 XYZ 换行

现在,我想再次在文件中发送相同的数据,但不是发送相同的数据,而是像这样发送: ABC DEF XYZ。

如何重新添加数字1?

这是我尝试的代码:

HttpServletRequest request = null;
if (getMessageContext() != null) {
    request = (HttpServletRequest) getMessageContext()
            .getHttpServletRequest();
} else {
    if (PhaseInterceptorChain.getCurrentMessage() != null) {
        request = (HttpServletRequest) PhaseInterceptorChain
                .getCurrentMessage().get("HTTP.REQUEST");
    }
}

2 个答案:

答案 0 :(得分:2)

当getline读取一行时,它将丢弃换行符。将line附加到line1时,需要重新插入换行符。

答案 1 :(得分:1)

Bilai。

尝试一下:

在程序获取每行的同时,使用相同的循环将其写入字符串流:

--w--wx--T 1 s8771-26 s8771-26    21 Apr 24 22:09 test1
--w--wx--T 1 s8771-26 s8771-26    21 Apr 24 22:09 test2
--w--wx--T 1 s8771-26 s8771-26    21 Apr 24 22:09 test3
--w--wx--T 1 s8771-26 s8771-26    21 Apr 24 22:09 test4


#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

int main(int argc, char ** argv)
{
    int fd = open(argv[1],O_WRONLY | O_TRUNC | O_CREAT, 0666);
    dup2(fd,1);
    printf("TEST");
    return 0;
}