'In the below sample code, i expect to get the number of characters in a file using _lseek(fh, 0L, SEEK_END). But it returns zero
i try to get the alternative functions of cnt, ptr , flag and base in the below structure (struct_iobuf)
- cnt = used lseek to get the number of characters inside the file
- flag= used the iostream flags
for ptr and base, any guidance will be helpful.
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;
}
Output:
Position for beginning of file seek = 0
Position for end of file seek = 0
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
