0

我尝试在以下结构(struct_iobuf)中获取 cnt、ptr、flag 和 base 的替代功能

  • cnt = 使用 lseek 获取文件中的字符数
  • flag= 使用了 iostream 标志

对于 ptr 和 base,任何指导都会有所帮助。

 struct _iobuf_VS2012 { // ...\Microsoft Visual Studio 11.0\VC\include\stdio.h #56
        char *_ptr;
        int   _cnt;
        char *_base;
        int   _flag;
        int   _file;
        int   _charbuf;
        int   _bufsiz;
        char *_tmpfname; };

#include <iostream>
#include <fstream>
#include<io.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include<stdio.h>
using namespace std;

int main() 
{
    ofstream myfile;
    myfile.open("example.txt");
    myfile << "Writing this to a file.\n";
    /*cout << "\n eof:"<<myfile.eof();
    cout << "\n fail:"<< myfile.fail();
    cout << "\n rdstate:" << myfile.rdstate();
    cout << "\n bad:" << myfile.bad();
    cout << "\n good:" << myfile.good();
    myfile.clear();*/
    int fh;
    long pos;
    _sopen_s(&fh,"example.txt", _O_RDONLY, _SH_DENYNO, 0);

    pos = _lseek(fh, 0L, SEEK_SET);
    if (pos == -1L)
        perror("_lseek to beginning failed");
    else
        printf("Position for beginning of file seek = %ld\n", pos);

    pos = _lseek(fh, 0L, SEEK_END);
    if (pos == -1L)
        perror("_lseek to end failed");
    else
        printf("Position for end of file seek = %ld\n", pos);
    //FILE* pfile = fopen("example.txt", "r");
    
   //fseek(pfile, 0, SEEK_END);
   //int cnt = ftell(pfile);
   //std::cout << "\n cnt:" << ftell(pfile);
    myfile.close();
    return 0;
}

输出:

Position for beginning of file seek = 0
Position for end of file seek = 0
4

0 回答 0