Tag Archive for write

Read and Write User Preferences

//
//  SetPrefsAppDelegate.h
//
#import <UIKit/UIKit.h>
 
@interface SetPrefsAppDelegate : NSObject <UIApplicationDelegate> 
{
  BOOL        saveUsername;
  NSInteger   preferredIndexInTabbar;
  UIWindow    *window;
}
 
@property (nonatomic, retain) IBOutlet UIWindow *window;
 
@end


//  SetPrefsAppDelegate.m
//
#import "SetPrefsAppDelegate.h"
#import "Preferences.h"
 
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// App Delegate implementation
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@implementation SetPrefsAppDelegate
 
@synthesize window;
 
/*------------------------------------------------------
* Read preferences from system
*-----------------------------------------------------*/
-(void) loadPreferences
{ 
  saveUsername = [Preferences shouldSaveUsername];
  preferredIndexInTabbar = [Preferences startupTab]; 
}
 
/*------------------------------------------------------
* Application startup code
*-----------------------------------------------------*/
- (void)applicationDidFinishLaunching:(UIApplication *)application 
{   
  // Load user preferences (notice the default values the first time through)
  [self loadPreferences];
 
  // Show the current values of the preferences
  NSLog(@"Save user preferences: %s", saveUsername == YES ? "Yes" : "No");
  NSLog(@"Preferred startup tab: %d", preferredIndexInTabbar);
 
  // This is a little contrived, but you get the point...
  BOOL saveUname = YES;
  NSInteger index = 3;
 
  // Write new values to the sytem
  [Preferences setPreferences:saveUname startupTab:index];
 
//  NSLog(@"Home: %s", NSHomeDirectory());
//  NSLog(@"Home: %@", NSHomeDirectory());
 
  // Override point for customization after application launch
  [window makeKeyAndVisible];
}
 
- (void)dealloc 
{
  [window release];
  [super dealloc];
}
 
@end

source

VB.NET Log File

Sub Log(ByVal txt As String)
        'Open a file for writing
        Dim FILENAME As String = Server.MapPath("Log.txt")

        'Get a StreamReader class that can be used to read the file
        Dim objStreamWriter As StreamWriter
        
        objStreamWriter = File.AppendText(FILENAME)

        objStreamWriter.WriteLine(DateTime.Now.ToString() + " >> " + txt)
    
        'Close the stream
        objStreamWriter.Close()
    End Sub

source

PHP Write to file

function write($path, $content, $mode="w+"){ if (file_exists($path) && !is_writeable($path)){ return false; } if ($fp = fopen($path, $mode)){ fwrite($fp, $content); fclose($fp); } else { return false; } return true; }

source

Write a file in Ruby

=begin
r - Open a file for reading. The file must exist.
w - Create an empty file for writing. If a file with the same name already exists its content is erased and the file is treated as a new empty file.
a - Append to a file. Writing operations append data at the end of the file. The file is created if it does not exist.
r+ - Open a file for update both reading and writing. The file must exist.
w+ - Create an empty file for both reading and writing. If a file with the same name already exists its content is erased and the file is treated as a new empty file.
a+ - Open a file for reading and appending. All writing operations are performed at the end of the file, protecting the previous content to be overwritten. You can reposition (fseek, rewind) the internal pointer to anywhere in the file for reading, but writing operations will move it back to the end of file. The file is created if it does not exist.
=end

File.open(local_filename, 'w') {|f| f.write(doc) }

source

write and read XML

<?php
session_start();

$_SESSION['arrConfig'];


$docConfigXML = new DOMDocument;
$docConfigXML->preserveWhiteSpace = FALSE;
$docConfigXML->load( 'resources/xml/config.xml' );

$_SESSION['arrConfig']['fax'] = trim($docConfigXML->getElementsByTagName("fax")->item(0)->getAttribute('value'));
$_SESSION['arrConfig']['phone'] = trim($docConfigXML->getElementsByTagName("phone")->item(0)->nodeValue);

echo $_SESSION['arrConfig']['fax'];
echo "<br />";
echo $_SESSION['arrConfig']['phone'];

$strConfigXML = "";


$fax 	= "702-555-1faxxx";
$phone 	= '702-555-phone';

$strConfigXML =
<<<END
<?xml version="1.0" encoding="utf-8"?>
<config>
<fax value='$fax' />
<phone>
$phone
</phone>
</config>
END;

$file = "resources/xml/config.xml";

writeFile($file,$strConfigXML);

function writeFile($file,$strFileContent)
{
$fh = fopen($file, 'w' ) or die ("can't create file");
fwrite($fh, $strFileContent);
fclose($fh);
}
?>

source

Alias a command and save it in .profile

echo "alias name='command string'" >> ~/.profile

source

Append Line to a File

echo "<string to append>" >> <file>

#Example:

echo "My string" >> /home/user/myfile.txt

source

Remove duplicate lines from a text file with Perl

#!/usr/bin/perl -w
use strict;
my $origfile = shift;
my $outfile  = "no_dupes_" . $origfile;
my %hTmp;

open (IN, "<$origfile")  or die "Couldn't open input file: $!";
open (OUT, ">$outfile") or die "Couldn't open output file: $!";

while (my $sLine = <IN>) {
next if $sLine =~ m/^s*$/;  #remove empty lines. Without this, still destroys empty lines except for the first one.
$sLine=~s/^s+//;            #strip leading/trailing whitespace
$sLine=~s/s+$//;
print OUT qq{$sLine
} unless ($hTmp{$sLine}++);
}
close OUT;
close IN;

source

Make a list of JPEGs into a slide show

#! /usr/bin/perl -w
use strict;

############################################################
#                                                          #
#                                                          #
#                                                          #
#                  NOAH SUSSMAN                            #
#                                                          #
#                  showserial.pl                           #
#                                                          #
#                Created 10/8/3 at 1.41am                  #
#                                                          #
#    Show jpegs one-per-page on a series of hyperlinked    #
#    web pages.  Purpose is to provide a good _linear_     #
#    viewing mechanism for jpegs on the palm.              #
#    Adapted from showsm.pl                                #
#                                                          #
#                                                          #
############################################################


my @images;

while (<*.jp*>) {
push @images, $_;  #read image filenames into a list
}

shift @images;  #first image is never the subject of  a "next" hyperlink

while (<*.jp*>) {  #regex captures any filename with extension beginning "JP"

my $next_file;
$next_file = $images[0]; #read the next filename into $next_file
shift @images; #manually discard the value we just read into $next_file

my $out="$_";
$out =~ s/jpeg/html/gio;
open OUT, ">$out" or die "Cannot open $out for write :$!";


if ($next_file){
#print $next_file;
$next_file =~ s/jpeg/html/gio;
print OUT "<a href="$next_file">";
}

print OUT "<html><head><title>$_</title></head><body>
";
print OUT "<img src="$_" >
";  #an image tag points to each file name
print OUT "</body></html>";


if ($next_file){
print OUT "</a>";
}
}

source

php easy file write

$myFile = "/tmp/slike.txt";
$fh = fopen($myFile, 'a') or die("can't open file");
fwrite($fh, print_r($povratak_fje));
fclose($fh);

source