有一些问题与seekg(二进制)和getline(txt / csv) [英] Having some issue with seekg(binary) and getline (txt/csv)

查看:84
本文介绍了有一些问题与seekg(二进制)和getline(txt / csv)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我写了一个C ++程序我有多个CSV文件用作输入,我一次打开一个并在解压缩后将其关闭数据到输出文件是唯一的文件。



我运行getline(inFile,line);

outFile<< line<< endl;



我运行这段代码,只有部分内容转到我得到的输出文件,也有随机间隔到特定文件并且不一致



但是当我减慢代码时,比如循环中的系统(暂停),我可以完美地提取我想要的东西....



我的程序运行速度快,为什么getline会跳过我想要的东西?



我真的不知道在哪里问题来自或从哪里开始



非常感谢!



OP代码:

Hi all,

I have written a C++ program I have multiple of CSV file used as input, which I open one at a time and close it after extracting data to a output file which is the only file.

I run getline(inFile,line);
outFile << line << endl;

I run this code, and only part of it is goes to the output file I got, also have spacing randomly to specific file and inconsistent

But when I slower the code, like system("Pause") in the loop, I can get extract what I want perfectly....

Is my program running to fast, why getline would be skipping part of what things I want?

I really have no idea where the problem is coming from, or where to start

Many Thanks!

OP's code:

if (dataname[i] == dataname)
{
    inFile.seekg(datalength[i], ios::beg);
    for (int j = 0; j < datacount[i]; j++)
    {
        getline(inFile, line);
        outFile << line << endl;
    }

}

推荐答案

seekg()通常用于以二进制模式打开的流。 getline()通常用于以非二进制(aka文本)模式打开的流。



无论哪种方式,一起使用它们可以得到你描述的结果。解决方案:不要在同一个流上使用它们。
seekg() is generally used on streams opened in binary mode. getline() is generally used on streams opened in non-binary (aka text) mode.

Either way, using them together can give results like you describe. The solution: don't use them both on the same stream.


定期刷新outfile。有时数据无法正确写入基础媒体。



每次写操作后调用函数

Flush the outfile periodically. Sometimes data doesn't get written properly to the underlying medium.

Call the function after every write operation
outFile.flush();





祝你好运!



Good Luck!


open the output file
while (there are more input files)
{
    open the next input file
    while (there is more data in the input file)
    {
        read the next line from the input file
        write the line to the output file
    }
    close the input file
}
close the output file



无需寻找或测试姓名等。


No need for seeks or tests for names etc.


这篇关于有一些问题与seekg(二进制)和getline(txt / csv)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆