我正在寻找关于你何时写文件的方式seekg()
和seekp()
工作的一些说明.比方说我有一个像这样的文件:
offset 0: 2
offset 4: 4
offset 8: 6
offset 12: 8
offset 16: 10
Run Code Online (Sandbox Code Playgroud)
现在我想打开文件并尝试读取和写入值.
fstream file;
file.open("file.txt", fstream::in |fstream::out | fstream::binary);
file.seekp(0, ios::end) // seek to the end of the file
int eofOffset = file.tellp(); // store the offset of the end-of-file, in this case 20
int key = 0;
file.seekg(12, ios::beg); // set the seek cursor to offset 12 from the beginning of the file
file.read((char *) &key, (int)sizeof(int)); // read in …
Run Code Online (Sandbox Code Playgroud) 我正在尝试寻找并重新读取数据.但代码失败了.
代码是
std::ifstream ifs (filename.c_str(), std::ifstream::in | std::ifstream::binary);
std::streampos pos = ifs.tellg();
std::cout <<" Current pos: " << pos << std::endl;
// read the string
std::string str;
ifs >> str;
std::cout << "str: " << str << std::endl;
std::cout <<" Current pos: " <<ifs.tellg() << std::endl;
// seek to the old position
ifs.seekg(pos);
std::cout <<" Current pos: " <<ifs.tellg() << std::endl;
// re-read the string
std::string str2;
ifs >> str2;
std::cout << "str2: (" << str2.size() << ") " << …
Run Code Online (Sandbox Code Playgroud) 我试图从正在增长的文件(类似于什么tail -F
)中读取,但我的代码必定存在一些问题:
string log, logFile("test.log");
size_t p = 0;
while(true)
{
ifstream ifs(logFile.c_str());
ifs.seekg(p); //*1
while(ifs.eof() == false)
{
getline(ifs, log);
cout << log << endl;
p = ifs.tellg(); //*2
}
nanosleep(&pause, NULL);
}
Run Code Online (Sandbox Code Playgroud)
如果没有//*1和//*2的行,日志文件会被正确读取到最后,但是如果添加新行,则不会发生任何事情.
使用seekg和tellg我试图存储文件的当前结束位置,这样当我重新打开它时,我可以去那里并阅读已添加的内容.
我想知道我的代码有什么问题,如果真的有必要为此目的关闭并重新打开同一个文件.
谢谢.
我发现在VS2010中,当打开正好为4294967295字节的文件时,seekg函数无法正常工作.
我正在使用简单的代码:
#include <iostream>
#include <fstream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
std::ifstream file;
// cmd: fsutil file createnew tmp.txt 4294967295
file.open(L"c:/tmp.txt", ifstream::in | ifstream::binary);
if(!file.is_open())
return -1;
file.seekg(0, std::ios::end);
auto state = file.rdstate();
// this condition shoots only when size of the file is equal to 4294967295
if((state & ifstream::failbit)==ifstream::failbit)
{
std::cout << "seekg failed";
}
// after seekg failed, tellg returns 0
std::streampos endPos = file.tellg();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
与文件4294967294和4294967296相同的代码工作没有任何问题.
有人知道这个问题的解决方案吗?
更新:
看起来这个问题就在这里:
template<class _Statetype> …
Run Code Online (Sandbox Code Playgroud) 在c ++中,使用istream :: seekg操作有多贵?
编辑:我可以通过寻找文件和读取字节来逃脱多少?频率与偏移幅度有何关系?
我有一个大文件(4GB),我正在解析,我想知道是否有必要尝试巩固我的一些seekg调用.我认为文件位置差异的大小起着作用 - 就像你在内存中寻找超过页面一样,它会影响性能 - 但是小搜索并不重要.它是否正确?
十年前,我曾经是一名C++专家,但在过去的十年里,我一直在编写Java.我刚刚开始使用一个小型第三方XML解析器的C++项目.XML解析器接受STL istream.我的XML数据来自Windows COM IStream.我以为我会做正确的事情并创建一个适配器来获取IStream数据并通过istream将它呈现给XML解析器.
我按照http://www.mr-edd.co.uk/blog/beginners_guide_streambuf上的优秀教程创建了一个COMStreambuf,它从底层的COM IStream获取数据,并将其用作自定义COMIstream的缓冲区.一切看起来都不错,但我从解析器中得到一个读错误.
事实证明,解析器通过在istream上使用seekg()来读取整个文件到内存中以查找其大小,然后使用seekg()返回到开头一次读取它.不出所料,前面提到的教程决定"保存[实施寻求的指示]另一篇文章",这显然从未写过.
有人能告诉我我需要做什么来实现我的自定义istream/streambuf的seekg()吗?我会冒险自己做(我的第一个倾向是覆盖istream中的东西),但由于我在STL的深层次和我的Java心态,我担心我会做一些不完整的事情并且有一个脆弱的解决方案.(例如,在没有阅读教程的情况下,我从来没有想过通过编写新的streambuf来制作自定义istream,或者我需要使用默认语言环境调用imbue()等)
谢谢你的帮助.我对这个网站印象非常深刻 - 无论是参与者的知识还是他们友好,诚实的承认谁拥有最佳答案.:)
当我在myFile中达到EOF时,Seekg似乎不起作用.
ifstream myFile("/path/file");
for(int i; i < 10; i++){
myFile.seekg(0);//reset position in myFile
while(getline(myFile, line)){
doSomething
}
}
Run Code Online (Sandbox Code Playgroud)
所以,现在我在每个循环打开输入流:
for(int i; i < 10; i++){
ifstream myFile("/path/file");//reset position in myFile
while(getline(myFile, line)){
doSomething
}
}
Run Code Online (Sandbox Code Playgroud)
但我宁愿寻求位置0.我怎样才能实现这一目标?
我需要按顺序读取一个大文件(大约10GB)的所有块,该文件包含许多带有几个字符串的浮点数,如下所示(每个项目由'\n'分割):
6.292611
-1.078219E-266
-2.305673E+065
sod;eiwo
4.899747e-237
1.673940e+089
-4.515213
我MAX_NUM_PER_FILE
每次都读取项目并处理它们并写入另一个文件,但我不知道什么时候ifstream
结束.这是我的代码:
ifstream file_input(path_input); //my file is a text file, but i tried both text and binary mode, both failed.
if(file_input)
{
file_input.seekg(0,file_input.end);
unsigned long long length = file_input.tellg(); //get file size
file_input.seekg(0,file_input.beg);
char * buffer = new char [MAX_NUM_PER_FILE+MAX_NUM_PER_LINE];
int i=1,j;
char c,tmp[3];
while(file_input.tellg()<length)
{
file_input.read(buffer,MAX_NUM_PER_FILE);
j=MAX_NUM_PER_FILE;
while(file_input.get(c)&&c!='\n')
buffer[j++]=c; //get a complete item
//process with buffer...
itoa(i++,tmp,10); //int2char
string out_name="out"+string(tmp)+".txt";
ofstream file_output(out_name);
file_output.write(buffer,j);
file_output.close();
}
file_input.close();
delete[] buffer; …
Run Code Online (Sandbox Code Playgroud) 在使用seekg&tellg文件的场景中,我想知道幕后发生了什么?
// Open file and get file size
int myFileSize;
std::fstream myFile;
myFile.open(myFileName, std::ios::in|std::ios::binary);
if (myFile.is_open())
{
myFile.seekg(0, std::ios::end);
myFileSize = myFile.tellg();
myFile.seekg(0, std::ios::beg);
myFile.close();
}
Run Code Online (Sandbox Code Playgroud)
Q1: seekg是否真正遍历文件的全部内容,直到找到一些特殊的"EOF字符"?或者它是否使用文件系统提供的一些其他信息来"知道"文件末尾的位置?
Q2: seekg是一个寻求流的操作.这是否意味着文件的整个内容必须通过流?
请原谅我,如果我对这一切的运作方式只有基本的了解.
我正在尝试编写一个带有菜单的程序,该菜单可以通过几种不同的方式从文本文件中读取.我只是在处理菜单选项#2(从文件末尾向后读),但我无法理解我做错了什么.我已经在这几天了,而且找不到任何好的资源来帮助解决这个问题.任何帮助,将不胜感激.
#include <iostream>
#include <string>
#include <iomanip>
#include <istream>
#include <math.h>
#include <fstream>
using namespace std;
const int SIZE = 20;
typedef char String[SIZE];
//prototypes
void Menu(int &);
void ReadFile(ifstream &);
void BackwardFile(ifstream &,long &);
long CountFile(ifstream &,long &);
int main()
{
char filename[]= "grades.txt";
int choice;
long numBytes;
ifstream InList;
InList.open(filename);
if(InList.good())
{
do
{
Menu(choice);
switch(choice)
{
case 1:
ReadFile(InList);
break;
case 2:
CountFile(InList,numBytes);
BackwardFile(InList,numBytes);
break;
case 3:
cout << "Pick a start position between 0 - " …
Run Code Online (Sandbox Code Playgroud)