fread

fread example from C++ Reference

I often use the website www.cplusplus.com as a reference when writing C code. I was reading the example cited on the page for fread and had a question. As an example they post: /* fread example: read a complete file */ #include <stdio.h> #include <stdlib.h> int main () { FILE * pFile; long lSize; char * buffer; size_t result;...

What is a good way to recover from a fread() failure?

If a call to fread() returns 0 and ferror() indicates an error (vs. EOF), is it OK to retry the read or is it better to close and reopen the file? I can't start over entirely -- the input file has been partially processed in a way that can't be undone (say I'm writing out a chunk at a time to a socket and, due to existing protocol, have...

PHP - Returning the last line in a file?

I'm guessing it's fgets, but I can't find the specific syntax. I'm trying to read out (in a string I'm thinking is easier) the last line added to a log file. ...

Problem in fread and an EoF mark

For some reason, fread stoppes reading at a place in the file that contains 1A1A (raw bytes, ofcourse, not text). The file is open in "rb" mode, and it does skip 1A's, but not the double 1A. Any solution? ...

Reading part of a file in C using fread() and fseek()

I'm trying to read a file into a buffer in blocks of size BLOCK_SIZE (currently equal to 1000 unsigned chars). My code first finds the number of blocks it will have to read in order to read the entire file (usually 2-4), then iterates through a for loop reading the file (ignore the "+17+filenamesize" stuff, that is all needed for later i...

PHP fgets() (or fread()) does not block.

Hello everybody, I'm trying to connect to a beanstalkd server through a PHP script and to reserve jobs from an existing queue. I'm using the fgets() function to get responses from the deamon, expecting the script to hang unless a job is available, here's a sample code: set_time_limit(0); $connection = fsockopen('localhost', 11300); fwr...

PHP technique to query the APNs Feedback Server

Hi all, Can someone clarify what the APNs (Apple Push Notification) wants as far as how you query it? The docs say it starts sending as soon as the connection is made. Does this mean that I don't do an fread() on it? Here's my current code to try and read it. I did NOT put the fread() in a loop as I do not know what response indicates...

c++ fread changing fgetpos strangely

If I run: FILE* pFile = fopen("c:\\08.bin", "r"); fpos_t pos; char buf[5000]; int ret = fread(&buf, 1, 9, pFile); fgetpos(pFile, &pos); I get ret = 9 and pos = 9. However if I run FILE* pFile = fopen("c:\\08.bin", "r"); fpos_t pos; char buf[5000]; int ret = fread(&buf, 1, 10, pFile); fgetpos(pFile, &pos); ret = 10 as expected, ...

PHP fsockopen() / fread() returns messed up data

I read some URL with fsockopen() and fread(), and i get this kind of data: <li 10 ></li> <li 9f >asd</li> d <li 92 Which is totally messed up O_O -- While using file _ get _ contents() function i get this kind of data: <li></li> <li>asd</li> Which is correct! So, what the HELL is wrong? i tried on...

PHP SOAP fread() dynamic POST size

Looking to read the file size of the SOAP POST, any best practices? $data = fopen('php://input','rb'); $content = fread($data,5000); $dom = new DOMDocument(); $dom->loadXML($content); Would like the 5000 to be dynamic as each SOAP POST size will be different or does this matter? Using fread() would be great ...

PHP: Emailing HTML Body of PHP Template file

I'm currently using PHP's mail() function to email the HTML Body of another PHP file using concatenation. This is currently working fine, however, I would prefer the script to simply get the parsed HTML Body rather than using the concatenation method. I've read a few incomplete forum posts regarding using fopen() and fread() but had no...

fread terminating mid-read at null values. Also reading in garbage past expected data.

I am reading in pieces of a binary file using a FILE object in C++. Here is the fseek and corresponding fread call: fseek(fp, startLocation, SEEK_SET); fread(data, m_sizeOfData, 1, fp); m_sizeOfData ends up being an integer greater than 400 thousand. This appears that it should read all 400 thousand+ bytes from the binary file into ...

does fread takes processing power / works with DMA ?

does fread takes processing power / works with DMA ? Assume that device on which file is residing has DMA support ? ...

C-programming ftell fseek fread, read size of a file

I have a file. I read the size of file. Then I loop reading two bytes at a time until I get to the end of the file. After every read operation I increment the current position by 2, however the position does not get incremented after I get to half the size of the file, the fread operation will read 0 bytes. the program reads the file si...

Read data into a float array in C

i am making a program where i need to load some floating point data of the format from a file 103.45 123.45 456.67 ...... i was wondering how to store these data directly into a array of floating point numbers using fread(). i guess it is not that hard using pointers but i am not so good with them. can anyone tell me how ...

C++: copying bmp using loop of fread and fwrite, casues output bmp is filled with color of the fisrt one in input bmp

I dunno why, but first pixel (left bottom) is loaded correctly, but the others won't load and the first color is used for whole picture... I have this in cycle fread(&pix,sizeof(pix),1,pictureIn); fwrite(&pix,sizeof(pix),1,pictureOut); edit: pix is struct of three unsigned chars (rgb), loading file and info header seems to be ok ...

What's the intended use of _fread_nolock, _fseek_nolock?

Hi there, we have a C++ class which basically reads and writes vectors from a binary file. An exemplary read function that loads a single vector into memory looks like this: int load (const __int64 index, T* values) const { int re = _fseeki64(_file, index * _vectorSize + _offsetData, SEEK_SET); assert(re == 0); size_t read = frea...

A C# equivalent of C's fread file i/o

Can anyone tell me how to get a array of bytes into a structure in a direct fashion in C# .NET version 2? Like the familiar fread as found in C, so far I have not had much success in reading a stream of bytes and automatically filling a structure. I have seen some implementations where there is pointer hocus-pocus in the managed code by ...

What is the equivalent of 'fread' from Matlab in Python?

I have practically no knowledge of Matlab, and need to translate some parsing routines into Python. They are for large files, that are themselves divided into 'blocks', and I'm having difficulty right from the off with the checksum at the top of the file. What exactly is going on here in Matlab? status = fseek(fid, 0, 'cof'); fposition...

Are files dumped with fwrite portable across different systems?

Can I assume a file generated with fwrite and read using fread is portable across different systems. 32bit/64bit windows,osx,linux. //dumping FILE *of =fopen("dumped.bin","w"); double *var=new double[10]; fwrite(var, sizeof(double), 10,FILE); //reading file *fo=fopen() double *var=new double[10]; fread(var,sizeof(double),10,of); And w...