473,386 Members | 1,674 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

fwrite() not working when appending RDP file...

Here's the problem, when I try to append an RDP file (which is written
in hex but readable in notepad) it adds funny little squares to the
file. I have tried opening the file in every mode but nothing helps.
Can anyone steer me in the right direction?

Here's my current code:

$file = "try_this.rdp";
$handle = fopen("$file","a+b");
fwrite($handle,"Hello");
fclose($handle);

I have tried opening it in binary mode (as seen above) and regular
mode. I'm not really sure when you should open something in binary
mode, I just tried it because I couldn't write to this file.

Thanks!

Aug 12 '05 #1
10 2438
The problem's with the text being written to the file. If you use a
windows server, new lines will be \r\n. If you use unix, new lines
will be \n.

I think that that's your problem.

Just do this:
$out = "I do not \n even know \n what a
RDP file is!";
$out = str_replace("\n","\r\n",$out);
$file = "test.rdp";
$handle = fopen("$file","a+b");
fwrite($handle,$out);
fclose($handle);

Aug 13 '05 #2
The problem's with the text being written to the file. If you use a
windows server, new lines will be \r\n. If you use unix, new lines
will be \n.

I think that that's your problem.

Just do this:
$out = "I do not \n even know \n what a
RDP file is!";
$out = str_replace("\n","\r\n",$out);
$file = "test.rdp";
$handle = fopen("$file","a+b");
fwrite($handle,$out);
fclose($handle);

Aug 13 '05 #3
What is a rdp file? Is it encoded in 16-bit Unicode? How many squares
were added?

Aug 13 '05 #4
I am not knowledgeable about file types (ASCII, Unicode, etc.). When I
try to append text to the file, two squares are added at the end of the
file (when viewing in a text editor).

I can read the file with a text editor, but if I copy the content to a
text file and name it the same (test.rdp) it is only 1KB (opposed to
the original file being 2KB) so I know there is some type of encoding
that I can't see with a text editor. I can open the file with a hex
editor and read it - does that mean it is a "hex" file? I am afraid i'm
ignorant in the matter.

At any rate, I can manually edit the file and change the ip address in
notepad and the file works fine. If i try to change it using fwrite, it
appends "squares". I'm at a loss.

Any help is GREATLY appreciated :)

Thanks!

Bobby T

Aug 15 '05 #5
It appears the file is saved as 16-bit Unicode, as the file is twice as
large as the ASCII version. The reason why you get two at the end when
you fwrite "hello" to the file is that "he" and "ll" are interpreted by
the computer as two Unicode characters. Their value fall within the
Chinese range, which your computer probably isn't set up to display.

PHP strings are 8-bit. You need to manually pad each byte with a \0 to
expand a string to 16-bit. Try either
fwrite($handle,"H\0e\0l\0l\0o\0") or fwrite($handle,"\0H\0e\0l\0l\0o").

Aug 15 '05 #6
Hey Teapot, I tried that but still no luck. It's just adding "squares"
to the file - at least when i view from a text editor.

Aug 15 '05 #7
Chung,

It does not like the "\"s in the code. I get an unexpected T_LNUMBER
error on the page with that. I tried both of them.

Thanks,

Bobby

Aug 15 '05 #8
OK... i've learned that whatever type of file this is, if I fwrite the
following:

fwrite($handle,"f\0u\0l\0l\0 \0a\0d\0d\0r\0e\0s\0s");

it will insert "full address:"

but if i try to insert numbers it doesn't work, they show up in the
file (looking with a text editor) as squares.

IE, if i try the following...

fwrite($handle,"2\02\02");

it shows up as squares in the file when viewing with a text editor. I
need 222 to be written to the file.

Aug 15 '05 #9
Sorry. It's safer to use "\x00" to represent 0 in a string. So 222
would be "\x002\x002\x002". The best way to do this is with
chunk_split(). Example: chunk_split("222", 1, "\x00");

Aug 15 '05 #10
Thank for all your help Chung. It looks like the problem is because of
some kind of hidden encrytpion on the file. I hate M$ more every day.

Aug 15 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Tuuska | last post by:
Hello! I have this problem, when fwrite() writes to the beginning of xyz.txt file, it overwrites the first line. Any ideas how to prevent this? I'm running PHP 4.3.3RC3 on Linux. <?php $fp =...
1
by: Martin Lucas-Smith | last post by:
I wrote the function below as part of a larger class. The fopen stage works, and, as according to the documentation at www.php.net/fopen that succesfully creates a new file. The fwrite stage...
4
by: Andrew Kibler | last post by:
Two sections of code, in the first one fwrite works, in the second one it doesn't (ms VC++) both are identical except in the working one fseek is used twice to set the file pointer, once just...
23
by: FrancisC | last post by:
how to use fwrite( ) instead of fprintf( ) in this case? I want to generate binary file. FILE *fnew; int i, intName; double array; fprintf(fnew, "%d\n", intName);...
3
by: sumit1680 | last post by:
Hi everyone, I am using the below listed code The code is #include<stdio.h> #include<stdlib.h> #include<string.h>
2
by: Richard Hsu | last post by:
// code #include "stdio.h" int status(FILE * f) { printf("ftell:%d, feof:%s\n", ftell(f), feof(f) != 0 ? "true" : "false"); } int case1() { FILE * f = fopen("c:\\blah", "wb+"); int i = 5;
12
by: Medvedev | last post by:
FILE *a,*b; int z; char str1; char str2; a= fopen(argv,"rb"); //Both files are EXE's b= fopen(argv,"rb+"); z= fread(str1,1,1,a); if (z !=1)
4
by: Highlander2nd | last post by:
Hello there. I'm Andrew Lucas, I'm a programmer for Half-Life. I've been working on stencil shadows lately, and I've been having problems saving mesh data for my models. When I store mesh data, I...
6
by: Scott | last post by:
Hi, Which is faster writing to a file using fwrite or is using a database to store data? I want to create a log file and it does not matter if it is in a text file or on a database. I just...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.