473,692 Members | 2,259 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.r dp";
$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 2463
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",$o ut);
$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",$o ut);
$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\0 o").

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\x00 2". The best way to do this is with
chunk_split(). Example: chunk_split("22 2", 1, "\x00");

Aug 15 '05 #10

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

Similar topics

3
11634
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 = fopen("xyz.txt","r+"); fwrite ($fp,"$newline"); fclose ($fp);
1
6555
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 returns false, however, on the first time the function is run, but returns true the second time it is run. Can anyone suggest why fwrite only works the second time?
4
2616
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 before the fwrite. WHY??? Works: fseek(fp, itemloc, SEEK_SET); tmp = ReadLongBE(fp); if (*itemsize == tmp){ printf("\n%X\n",ftell(fp));
23
17841
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); fprintf(fnew, " %f", array);
3
4524
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
6310
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
3161
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
3770
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 have to store data for each submodel and their respective triangle data, wich are the vertex and neighbor(triangles sharing a common edge) indexes. Now, I'm storing this data in these struct's: struct Face { Face() {} Face(GLushort v0,...
6
2309
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 want the one that is easier on the server. Any advice is greatly appreciated. -- Your friend, Scott
0
8610
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8545
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8968
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8810
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7633
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6462
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4327
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
2242
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
1961
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.