fread

fwrite with structs containing an array

How do I fwrite a struct containing an array #include <iostream> #include <cstdio> typedef struct { int ref; double* ary; } refAry; void fillit(double *a,int len){ for (int i=0;i<len;i++) a[i]=i; } int main(){ refAry a; a.ref =10; a.ary = new double[10]; fillit(a.ary,10); FILE *of; if(NULL==(of=fopen("test.bin","w"...

How to read a binary file in c? (video, images, or text)

I am trying to copy a file from a specified library to the current directory. I can copy text files perfectly. Any other files become corrupt. The program detects a feof before it should. #include <stdio.h> int BUFFER_SIZE = 1024; FILE *source; FILE *destination; int n; int count = 0; int written = 0; int main() { unsigned char b...

Is it better to use fseek() fread() on individual lines, or fread() the entire file and substr to parse?

To make this more clear, I'm going to put code samples: $file = fopen('filename.ext', 'rb'); // Assume $pos has been declared // method 1 fseek($file, $pos); $parsed = fread($file, 2); // method 2 while (!feof($file)) { $data = fread($file, 1000000); } $data = bin2hex($data); $parsed = substr($data, $pos, 2); $fclose($file); T...

Create files from file names in another file C++

Hi, I am working on sorting several large files in C++. I have a text file containing the names of all the input files, one on each line. I would like to read the file names in one at a time, store them in an array, and then create a file with each of those names. Right now, I am using fopen and fread, which require character arrays (I ...

Read variable-length records from a buffer - weird memory issues

Hi, I'm trying to implement an i/o intensive quicksort (C++ qsort) on a very large dataset. In the interests of speed, I'd like to read in a chunk of data at a time into a buffer and then use qsort to sort it inside the buffer. (I am currently working with text files but would like to move to binary soon.) However, my data is composed o...

Buffered reading from stdin using fread in C

I am trying to efficiently read from the stdin by using setvbuf in `_IOFBF~ mode. I am new to buffering. I am looking for working examples. The input begins with two integers (n,k). The next n lines of input contain 1 integer. The aim is to print how many integers are divisible by k. #define BUFSIZE 32 int main(){ int n, k, tmp, ans=...

Why does this code not output the expected output ?

This can be a good question for finding bugs. No? Okay for beginners at least. #define SIZE 4 int main(void){ int chars_read = 1; char buffer[SIZE + 1] = {0}; setvbuf(stdin, (char *)NULL, _IOFBF, sizeof(buffer)-1); while(chars_read){ chars_read = fread(buffer, sizeof('1'), SIZE, stdin); printf("%d, %s\n", chars_read,...

Reading and Writing Structures [C]

IMPORTANT EDIT: Sorry everyone, i made a big mistake in the structure. char *name; is meant to be outside of the structure, written to the file after the structure. This way, you read the structure, find out the size of the name, then read in the string. Also explains why there is no need for a null terminator. However, i feel somewhe...

fread/fwrite string in C

I have a binary file which contains records. The structure of the file is as such: Structure (see below) Name String Address String The structure in question: typedef struct{ char * name; char * address; short addressLength, nameLength; int phoneNumber; }employeeRecord; employeeRecord record; I get the name as such:...

Fread binary file dynamic size string

I've been working on this assignment, where I need to read in "records" and write them to a file, and then have the ability to read/find them later. On each run of the program, the user can decide to write a new record, or read an old record (either by Name or #) The file is binary, here is its definition: typedef struct{ cha...

pound sign in CSV file are not read during PHP fopen

Hi, I am having a problem, pound signs and single quotes are being read from a file as � My code is below: $fh = fopen($_FILES['importFile']['tmp_name'], "r"); $contents = fread($fh, filesize($_FILES['importFile']['tmp_name'])); var_dump($contents); Does anybody know how to fix this issue. I know its an encoding issue but unsure as...

How NOT to skip BOM info (FF FE) when using fread or ifstream?

I'm trying to use fread/ifstream to read the first 2 bytes of a .csv with BOM info. But following code always skips the first two bytes (which are 'FF FE'): ifstream is; is.open (fn, ios::binary ); char buf[2]; is.read(buf, 2); is.close(); using FILE*/fread does no better. ...

fread() behaves weird

hi, I have a problem in a C program of mine where after I use fread(), the file pointer goes to the end of the file sometimes. I'll try to explain better - the code looks something like: dummy = ftell(fp); fread(&buf, sizeof(unsigned char), 8, fp); dummy = ftell(fp); where fp is a file pointer to an opened file (opened it with "w+", ...

File counter adds 2 instead of 1

I made a simple counter, but it increments by 2 instead of 1. $handle = fopen('progress.txt', 'r'); $pro = fgets($handle); print $pro; // incremented by 2, WTF? fclose($handle); $handle = fopen('progress.txt', 'w'); fwrite($handle, $pro); fclose($handle); Everytime I read the file it has been incremented ...

Any reasons why fread() would be different on a .php file over a .txt file?

Is there any reason why the fread() function wouldn't work (no errors show up) when reading from a .php file over a .txt file? code $file = fopen("db.php","rw"); $data = fgets($file, filesize("db.php")); echo($data); fclose($file); die(); ...

Why am I not getting the expected results with fread() in C?

Here is my code: #include <stdio.h> int main(void) { FILE *fp; unsigned int i; char bytes[512]; fp = fopen("myFile","r"); for(i = 0;i <= 512;i++) { fread(&bytes, sizeof(bytes), 1, fp); printf("bytes[%d]: %x\n", i, bytes[i]); } } Here is the expected outp...

Lots of questions about file I/O (reading/writing message strings)

Hi, For this university project I'm doing (for which I've made a couple of posts in the past), which is some sort of social network, it's required the ability for the users to exchange messages. At first, I designed my data structures to hold ALL messages in a linked list, limiting the message size to 256 chars. However, I think my ins...

Java gzip.read and PHP fread() gives different results

I have line in Java and in while I get number: i = gzipinputstream1.read(abyte0, j, 4096); From while number: 959 1552 1577 1617 1680 when I want use in php: $i = fread($handle, 959): while return: 959, 959, 959, 5 How make that in PHP result will be the same? ...

Are fopen/fread/fgets PID-safe in C ?

Various users are browsing through a website 100% programmed in C (CGI). Each webpage uses fopen/fgets/fread to read common data (like navigation bars) from files. Would each call to fopen/fgets/fread interefere with each other if various people are browsing the same page ? If so, how can this be solved in C ? (This is a Linux server, co...

A couple questions using fwrite/fread with data structures

Hi, I'm using fwrite() and fread() for the first time to write some data structures to disk and I have a couple of questions about best practices and proper ways of doing things. What I'm writing to disk (so I can later read it back) is all user profiles inserted in a Graph structure. Each graph vertex is of the following type: typede...