fwrite

Checking for success of fwrite in C, perror

With fwrite returning the number of successful elements written to the file, by saying: if (!(fwrite(...))) { fprintf(stderr, "Failure"); //perror(???) I sometimes see code that says perror here and I don't know //exactly what this does. } Does this check for successful writing to the file? Are there other things to wor...

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:...

NSOperation and fwrite (Iphone)

Hi, I am having problem with this code.Basically I want to execute the fwrite from a timer function asyncronusly. Here is the code block in my Timer function. (This will call by the timer every 0.2 seconds. -(void)timerFunction { WriteFileOperation * operation = [WriteFileOperation writeFileWithBuffer:pFile buffer:readb...

saving information in file in php

hey guys i want to write a tracking system and now i can save in my Mysql database . but saving information about each ip that visits is a huge work for mysql so i think if i could save the information in a file , then there is no discussion about database and its problems . but to begin this : i realy dont know how to save in a ...

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 ...

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...

How do I overwrite X bytes on offset Y with fwrite()?

Hi, All I can find using fopen() and fwrite() in C is to delete all contents and start writing again or append to the end of the file. What if I need to go to some offset in the file and overwrite a few bytes? Is that possible with some function? ...

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...

fwrite write an integer

I'm trying to write a word to a file using this function: extern void write_int(FILE * out, int num) { fwrite(&num,sizeof(int),1, out); if(ferror(out)){ perror(__func__); exit(EXIT_FAILURE); } } But I get a segmentation fault whenever it tries to run the fwrite. I looked at the man page for fwrite(3) and I feel li...

How to open a file and remove the last line?

I am looking to open up a file, grab the last line in the file where the line = "?>", which is the closing tag for a php document. Than I am wanting to append data into it and add back in the "?>" to the very last line. I've been trying a few approaches, but I'm not having any luck. Here's what I got so far, as I am reading from a zip...

Why can’t PHP script write a file on server 2008 via command line or task scheduler?

I created a question on serverfault.com, and it was recommended that I ask here. http://serverfault.com/questions/140669/why-cant-php-script-write-a-file-on-server-2008-via-command-line-or-task-schedul I have a PHP script. It runs well when I use a browser. It writes an XML file in the same directory. The script takes ~60 seconds to r...

fwrite to txt-file

I'm currently trying to write to a txt-file with PHP, I've found this small script: <?php $filename = 'testFile.txt'; $somecontent = "Add this to the file\n"; if (is_writable($filename)) { if (!$handle = fopen($filename, 'a')) { echo "Cannot open file ($filename)"; exit; } if (fwrite($handle, $someconten...

Can I use php's fwrite with 644 file permissions?

I am trying to set up automated .htaccess updating. This clearly needs to be as secure as possible, however right now the best I can do file permission-wise is 666. What can I do to setup either my server or php code so that my script's fwrite() command will work with 644 or better? For instance is there a way to set my script(s) to ...

PHP New Line will not work

Hi I am trying to create some code that first reads the existing contents of the file in and then adds the new line of code on a new line but the code i am using just adds it on the new text on to the already existing line instead of the new line... Here is the code i am using: <?php $id = $_GET['id']; $userfile = "user1.txt"; $fo = ...

Add bytes to binary file using only PHP?

I am trying to add random bytes to binary (.exe) files to increase it size using php. So far I got this: function junk($bs) { // string length: 256 chars $tmp = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...

What's the best way to write to more files than the kernel allows open at a time?

I have a very large binary file and I need to create separate files based on the id within the input file. There are 146 output files and I am using cstdlib and fopen and fwrite. FOPEN_MAX is 20, so I can't keep all 146 output files open at the same time. I also want to minimize the number of times I open and close an output file. How ...

Writing a new line to file in PHP

My code: $i = 0; $file = fopen('ids.txt', 'w'); foreach ($gemList as $gem) { fwrite($file, $gem->getAttribute('id') . '\n'); $gemIDs[$i] = $gem->getAttribute('id'); $i++; } fclose($file); For some reason, it's writing \n as a string, so the file looks like this: 40119\n40122\n40120\n42155\n36925\n45881\n42145\n45880 Fro...

Writing a structure all at once in a file

I'm doing a school project about maintaining a personal Database. but my tutors didn't explained the practice, they explained the theory(Data structures and such) not how to code in c. We where given a certain amount of tables, indexes and consults they wanted to solved and we are to program the data structures that run behind. I ch...

Creating an installer script

Hi all, I'm trying to create an installer, using a combination of fwrite and forms. Here's my code: <?php $myFile = "db_config.php"; $fh = fopen($myFile, 'w') or die("can't open file"); fwrite($fh, "$host = " . $_POST['host'] . ";\n $username = " . $_POST['username'] . ";\n $password = " . $_POST['password'] . ";\n $name = " . $_POS...

An easier way to fwrite?

Hi all, fwrite($fh, <?php $fields = array("url", "shoutcast_url", "site_name", "site_subtitle", "email_suffix", "twitter_username", "skype_name", "phone_number"); foreach ($fields as $field) $$field = mysql_real_escape_string($_POST[$field]); mysql_query("UPDATE config SET url='{$url...