Search Results

Search found 222 results on 9 pages for 'fwrite'.

Page 1/9 | 1 2 3 4 5 6 7 8 9  | Next Page >

  • A couple questions using fwrite/fread with data structures

    - by Nazgulled
    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: typedef struct sUserProfile { char name[NAME_SZ]; char address[ADDRESS_SZ]; int socialNumber; char password[PASSWORD_SZ]; HashTable *mailbox; short msgCount; } UserProfile; And this is how I'm currently writing all the profiles to disk: void ioWriteNetworkState(SocialNetwork *social) { Vertex *currPtr = social->usersNetwork->vertices; UserProfile *user; FILE *fp = fopen("save/profiles.dat", "w"); if(!fp) { perror("fopen"); exit(EXIT_FAILURE); } fwrite(&(social->usersCount), sizeof(int), 1, fp); while(currPtr) { user = (UserProfile*)currPtr->value; fwrite(&(user->socialNumber), sizeof(int), 1, fp); fwrite(user->name, sizeof(char)*strlen(user->name), 1, fp); fwrite(user->address, sizeof(char)*strlen(user->address), 1, fp); fwrite(user->password, sizeof(char)*strlen(user->password), 1, fp); fwrite(&(user->msgCount), sizeof(short), 1, fp); break; currPtr = currPtr->next; } fclose(fp); } Notes: The first fwrite() you see will write the total user count in the graph so I know how much data I need to read back. The break is there for testing purposes. There's thousands of users and I'm still experimenting with the code. My questions: After reading this I decided to use fwrite() on each element instead of writing the whole structure. I also avoid writing the pointer to to the mailbox as I don't need to save that pointer. So, is this the way to go? Multiple fwrite()'s instead of a global one for the whole structure? Isn't that slower? How do I read back this content? I know I have to use fread() but I don't know the size of the strings, cause I used strlen() to write them. I could write the output of strlen() before writing the string, but is there any better way without extra writes?

    Read the article

  • fwrite write an integer

    - by user149100
    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 like I used it correctly, is there something I'm missing?

    Read the article

  • NSOperation and fwrite (Iphone)

    - by Sridhar
    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:readblePixels length:nBytes*15]; [_queue addOperation:operation]; // Here it is waiting to complete the fwrite } The WrtiteFilerOperation is an NSoperation class which it has to write the passing buffer to a file. I added this code in WriteFileOperation's "start" method. (void)start { if (![NSThread isMainThread]) { [self performSelectorOnMainThread:@selector(start) withObject:nil waitUntilDone:NO]; return; } [self willChangeValueForKey:@"isExecuting"]; _isExecuting = YES; [self didChangeValueForKey:@"isExecuting"]; NSLog(@"write bytes %d",fwrite(_buffer, 1, _nBytes, _file)); free(_buffer); [self finish]; } The problem here is , my timerFunction blocked by NSOperation until it writes the buffer into file.(I mean blocked till start method finishes its execution) and the performance seems same as directly placing the fwrite in timerFunction. I want to just return to timerFunction with out waiting from the start method execution to be completed. What I am doing wrong here ? Thanks In Advance Raghu

    Read the article

  • fwrite to txt-file

    - by timkl
    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, $somecontent) === FALSE) { echo "Cannot write to file ($filename)"; exit; } echo "Success, wrote ($somecontent) to file ($filename)"; fclose($handle); } else { echo "The file $filename is not writable"; } ?> I get a success-message, but nothing is written to the file. Even if I delete the txt-file I still get the success-message Does anybody know how to troubleshoot a situation like this?

    Read the article

  • c++ function overloading, making fwrite/fread act like PHP versions

    - by Newbie
    I'm used to the PHP fwrite/fread parameter orders, and i want to make them the same in C++ too. I want it to work with char and string types, and also any data type i put in it (only if length is defined). I am total noob on c++, this is what i made so far: size_t fwrite(FILE *fp, const std::string buf, const size_t len = SIZE_MAX){ if(len == SIZE_MAX){ return fwrite(buf.c_str(), 1, buf.length(), fp); }else{ return fwrite(buf.c_str(), 1, len, fp); } } size_t fwrite(FILE *fp, const void *buf, const size_t len = SIZE_MAX){ if(len == SIZE_MAX){ return fwrite((const char *)buf, 1, strlen((const char *)buf), fp); }else{ return fwrite(buf, 1, len, fp); } } Should this work just fine? And how should this be done if i wanted to do it the absolutely best possible way?

    Read the article

  • Question about fwrite API

    - by michael
    Hi, In C++, there is a fwrite() which writes a buffer to a file on disk: http://www.cplusplus.com/reference/clibrary/cstdio/fwrite/ Can you please tell me if there is any buffer inside that fwrite implementation? i.e. if I call fwrite() multiple times (say 10 times), does it actually invoke file i/o 10 different times? Thank you.

    Read the article

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

    - by filip
    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 run as owner?

    Read the article

  • fwrite = unblocking ?

    - by John Michaels
    Since you can't be sure that what you write with fwrite has been written (god thats an awesome sentence) before you call fflush can i consider fwrite a nonblocking write?? And if not, why not and what are my alternatives?

    Read the article

  • Alternatives to fread and fwrite for use with structured data

    - by forest58
    The book Beginning Linux Programming (3rd ed) says "Note that fread and fwrite are not recommended for use with structured data. Part of the problem is that files written with fwrite are potentially nonportable between different machines." What does that mean exactly? What calls should I use if I want to write a portable structured data reader or writer? Direct system calls?

    Read the article

  • fread and fwrite are not recommended for use with structured data

    - by forest58
    A book beginning linux programming 3ed says "Note that fread and fwrite are not recommended for use with structured data.Part of the problem is that files written with fwrite are potentially nonportable between different machines." What does that mean exactly? what calls should I use if I want to write a portable structured data reader or writer? direct system calls?

    Read the article

  • fwrite C programing

    - by Pedro
    HI...i want to write something like this in a file, using fwrite fwrite("name is %s\n",name, 60, fp); but is not working, only write in the file the string. any idea?

    Read the article

  • fopen & fwrite function is not working from browser?

    - by ungalnanban
    fopen function is not working in php. I wrote the following code in my php file. $fh = fopen("/home/sugumar/Public_html/sugumar/public_html/123","a+"); fwrite($fh,"hello"); I ran this code from command line: php file_name.php its working fine. But If I run this code from browser it shows the following error. Warning: fopen(Logs/add_employee.logs) [function.fopen]: failed to open stream: Permission denied in /home/sugumar/Public_html/sugumar/public_html/HRMS/HRMS_add_emp_DB.php on line 111 Warning: fwrite(): supplied argument is not a valid stream resource in /home/sugumar/Public_html/sugumar/public_html/HRMS/HRMS_add_emp_DB.php on line 113 how to solve this problem? Thanks in advance.

    Read the article

  • \n not working in my fwrite()

    - by brett
    Not sure what could be the problem. I'm dumping data from an array $theArray into theFile.txt, each array item on a separate line. $file = fopen("theFile.txt", "w"); foreach ($theArray as $arrayItem){ fwrite($file, $arrayItem . '\n'); } fclose($file); Problem is when I open theFile.txt, I see the \n being outputted literally. Also if I try to programmatically read the file line by line (just in case lines are there), it shows them as 1 line meaning \n are really not having their desired effect.

    Read the article

  • Saving a .xls file with fwrite

    - by kielie
    hi guys, I have to create a script that takes a mySQL table, and exports it into .XSL format, and then saves that file into a specified folder on the web host. I got it working, but now I can't seem to get it to automatically save the file to the location without prompting the user. It needs to run every day at a specified time, so it can save the previous days leads into a .XSL file on the web host. Here is the code: <?php // DB TABLE Exporter // // How to use: // // Place this file in a safe place, edit the info just below here // browse to the file, enjoy! // CHANGE THIS STUFF FOR WHAT YOU NEED TO DO $dbhost = "-"; $dbuser = "-"; $dbpass = "-"; $dbname = "-"; $dbtable = "-"; // END CHANGING STUFF $cdate = date("Y-m-d"); // get current date // first thing that we are going to do is make some functions for writing out // and excel file. These functions do some hex writing and to be honest I got // them from some where else but hey it works so I am not going to question it // just reuse // This one makes the beginning of the xls file function xlsBOF() { echo pack("ssssss", 0x809, 0x8, 0x0, 0x10, 0x0, 0x0); return; } // This one makes the end of the xls file function xlsEOF() { echo pack("ss", 0x0A, 0x00); return; } // this will write text in the cell you specify function xlsWriteLabel($Row, $Col, $Value ) { $L = strlen($Value); echo pack("ssssss", 0x204, 8 + $L, $Row, $Col, 0x0, $L); echo $Value; return; } // make the connection an DB query $dbc = mysql_connect( $dbhost , $dbuser , $dbpass ) or die( mysql_error() ); mysql_select_db( $dbname ); $q = "SELECT * FROM ".$dbtable." WHERE date ='$cdate'"; $qr = mysql_query( $q ) or die( mysql_error() ); // Ok now we are going to send some headers so that this // thing that we are going make comes out of browser // as an xls file. // header("Pragma: public"); header("Expires: 0"); header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); header("Content-Type: application/force-download"); header("Content-Type: application/octet-stream"); header("Content-Type: application/download"); //this line is important its makes the file name header("Content-Disposition: attachment;filename=export_".$dbtable.".xls "); header("Content-Transfer-Encoding: binary "); // start the file xlsBOF(); // these will be used for keeping things in order. $col = 0; $row = 0; // This tells us that we are on the first row $first = true; while( $qrow = mysql_fetch_assoc( $qr ) ) { // Ok we are on the first row // lets make some headers of sorts if( $first ) { foreach( $qrow as $k => $v ) { // take the key and make label // make it uppper case and replace _ with ' ' xlsWriteLabel( $row, $col, strtoupper( ereg_replace( "_" , " " , $k ) ) ); $col++; } // prepare for the first real data row $col = 0; $row++; $first = false; } // go through the data foreach( $qrow as $k => $v ) { // write it out xlsWriteLabel( $row, $col, $v ); $col++; } // reset col and goto next row $col = 0; $row++; } xlsEOF(); exit(); ?> I tried using, fwrite to accomplish this, but it didn't seem to go very well, I removed the header information too, but nothing worked. Here is the original code, as I found it, any help would be greatly appreciated. :-) Thanx in advance. :-)

    Read the article

  • Overwrite Content in PHP fwrite()

    - by Shahmir Javaid
    Is there any way you can overwrite a line in PHP. let me be a little more clearer using examples. My array array{ [DEVICE] => eth0, [IPADDR] => 192.168.0.2, [NETMASK] => 255.255.255.0, [NETWORK] => 192.168.0.0, [BROADCAST] => 255.255.255.255, [GATEWAY] => 192.168.0.1, [ONBOOT] => no } File im overwriting DEVICE=eth0 IPADDR=192.168.200.2 NETMASK=255.255.255.0 NETWORK=192.168.200.0 BROADCAST=255.255.255.255 GATEWAY=192.168.200.1 ONBOOT=no DNS1=195.100.10.1 Result of the File that is rewritten DEVICE=eth0 IPADDR=192.168.0.2 NETMASK=255.255.255.0 NETWORK=192.168.0.0 BROADCAST=255.255.255.255 GATEWAY=192.168.0.1 ONBOOT=no DNS1=195.100.10.1 Note that DNS1=195.100.10.1 Stays in the file becuase it dosent have a key with the value of DNS in our array. Thanks

    Read the article

  • Add bytes to binary file using only PHP?

    - by hurmans
    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 = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'; for($i=0;$i<=$bs;$i++) { $tmp = $tmp . $tmp; } return $tmp; } $fp = fopen('test.exe', 'ab'); fwrite($fp, junk(1)); fclose($fp); This works fine and the resulting exe is functional but if I want to do junk(100) to add more size to the file I get the php error "Fatal error: Allowed memory size..." In which other way could I achieve this without getting an error? Would it be ok to loop the fwrite xxx times?

    Read the article

  • PHP New Line will not work

    - by user364333
    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 = fopen($userfile, 'r') or die("can't open favourites file"); $currentdata = fread($fo, filesize($userfile)); fclose($fo); $fw = fopen($userfile, 'w') or die("can't open favourites file"); $currentprocessed = "$currentdata\n"; fwrite($fw, $currentprocessed); fwrite($fw, $id); fclose($fw); ?> I have tried a whole range of different ideas but nothing has worked, any help would be appreciated.

    Read the article

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

    - by rg89
    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 run, and the resulting XML file is ~16 MB. I am running PHP 5.2.13 via FastCGI on Windows Server Web edition SP1 64 bit. The code pulls inventory from SQL server, runs a loop to build an XML file for a third party. I created a task in task scheduler to run c:\php5\php.exe "D:\inetpub\tools\build.php" The task scheduler shows a time lapse of about a minute, which is how long the script takes to run in a browser. No error returned, but no file created. Each time I make a change to the scheduled task properties, a user password box comes up and I enter the administrator account password. If I run this same path and argument at a command line it does not error and does not create the file. When I right click run command prompt as an administrator, the file is still not created. I get my echo statement "file published" that is after the file creation and no error is returned. I am doing a simple fopen fwrite fclose to save the contents of a php variable to a .xml file, and the file only gets created when the script is run through the browser. Here's what happens after the xml-building loop: $feedContent .= "</feed"; sqlsrv_close( $conn ); echo "<p>feed built</p>"; $feedFile = "feed.xml"; $handler = fopen($feedFile, 'w'); fwrite( $handler, $feedContent ); fclose( $handler ); echo "<p>file published</p>"; Thanks

    Read the article

  • File counter adds 2 instead of 1

    - by Derk
    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 by 2, instead of 1.

    Read the article

  • Writing a new line to file in PHP

    - by James P
    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 From Google'ing it tells me to use \r\n, but \r is a carriage return which doesn't seem to be what I want to do. I just want the file to look like this: 40119 40122 40120 42155 36925 45881 42145 45880 Thanks.

    Read the article

  • http_post() variables to php script from SMS message but only append data to text file if certain variable (telephone number) equals a known

    - by user1592162
    All, I have an SMS gateway application running on my server that passes incoming SMS messages to a php script. That script writes the data to a text file. Certain aspects of the message can be extracted as variables - for example: Message content: $msgdata Sender: $originator Time: $receivedtime etc... Using the script below I can easily write the message to a text file but it will write the data of every SMS that comes in. I only want to write to a text file if the $originator variable is a certain telephone number (e.g 07123456321) $date = date("d.m.Y H:i:s"); $msgdata = $_REQUEST["msgdata"]; $myFile3 = "test.txt"; $fh = fopen($myFile3, 'a') or die("can't open file"); $stringData3 = "$date - $msgdata\n"; fwrite($fh, $stringData3); fclose($fh); Your assistance is bery much appreciated. Many thanks in advance.

    Read the article

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

    - by Elpezmuerto
    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 can I write to the output files effectively? I also must use the cstdlib library due to legacy code.

    Read the article

  • How do I effectively write to 146 output files in C++ using cstdlib library

    - by Elpezmuerto
    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 can I write to the output files effectively? I also must use the cstdlib library due to legacy code.

    Read the article

  • write 2d array to a file in C (Operating system)

    - by Bobj-C
    Hello All, I used to use the code below to Write an 1D array to a File: FILE *fp; float floatValue[5] = { 1.1F, 2.2F, 3.3F, 4.4F, 5.5F }; int i; if((fp=fopen("test", "wb"))==NULL) { printf("Cannot open file.\n"); } if(fwrite(floatValue, sizeof(float), 5, fp) != 5) printf("File read error."); fclose(fp); /* read the values */ if((fp=fopen("test", "rb"))==NULL) { printf("Cannot open file.\n"); } if(fread(floatValue, sizeof(float), 5, fp) != 5) { if(feof(fp)) printf("Premature end of file."); else printf("File read error."); } fclose(fp); for(i=0; i<5; i++) printf("%f ", floatValue[i]); My question is if i want to write and read 2D array ??

    Read the article

1 2 3 4 5 6 7 8 9  | Next Page >