Valhalla Legends Forums Archive | General Programming | Recreating a file

AuthorMessageTime
Yegg
The thread title isn't informative enough. So here's my problem. I am trying to read all data from a file, the file can be of *any* type. Whether it be a text document, executable file, or an image file. I want to be able to read the data from the file, and write it into another file. My goal in doing so is to create that file again, however with a different name. I am not sure why, but whenever I try this it only writes a very small amount of text to the new file. I tried this in Python and Pascal so far. Both had the same exact failing effects.

Here is the Pascal version, maybe you see something wrong?

[code]program test;

var f  :text;
var f2 :text;
var data :string;

begin
  assign(f, 'HighResUptime.exe');
  reset(f);
  assign(f2, 'HighResUptime2.exe');
  rewrite(f2);
  while not eof(f) do begin
      readln(f, data);
      writeln(f2, data);
  end;
  writeln('Done!!!');
  close(f);
  close(f2);
  readln;
end.[/code]

Incase you can't read Pascal code, not that this code is complex at all. All that this code does is reads data from HighResUptime.exe, and writes data into HighResUptime2.exe. This fails. Any suggestions?
November 12, 2005, 12:25 AM
St0rm.iD
You want to copy a file. There will probably be a stdlib call for that (in Python, it's shutil.copyfile, iirc).

However, I'll give you the Python for doing it manually. Enjoy.

CHUNK_SIZE = 1024
f = open("HighResUptime.exe", "rb")
f2 = open("HighResUptime2.exe", "wb")

while True:
    try:
        chunk = f.read(CHUNK_SIZE)
        f2.write(chunk)
    except EOFError:
        break

f.close()
f2.close()
[code]

You can also directly iterate over a file object, but I did it explicitly here with chunks to make it easier to port to other languages.[/code]
November 12, 2005, 1:30 AM
iago
Although I can't help you with your problem, I'm curious: why?  What's the problem with using the cp or copy command? 

Or, some languages have the functionality built in.  Java has File.copy(). 

Now, my little attempt to help: in C, when you read in a file, it only reads a line at a time.  Is it possible that that's your problem, you're only reading in the first line? If that's the case, you might need to loop until the whole file is read.  I don't know Pascal, so I have no clue if that's your problem, though :)

It looks like you shouldn't have that problem, but I do notice other potential problems:
- How does readln/writeln handle newlines?  In some languages, newlines might be read as \r\n or \n, and written as another.  You have to be careful with line termination when you're doing a binary file.
- How does the program handle NULL ('\0') characters?  Are they read into the string, then not written?  Or are they stripped out?  Or does the input end when one is reached?  iirc, Pascal doesn't use NULL-terminators, so that might not be a problem, but that's something else to think about. 

Hopefully something there will help you.

Unless, of course, somebody else solves it while I'm typing.  Bastard :)
November 12, 2005, 1:31 AM
Yegg
[quote author=iago link=topic=13212.msg133691#msg133691 date=1131759064]
Although I can't help you with your problem, I'm curious: why?  What's the problem with using the cp or copy command? 

Or, some languages have the functionality built in.  Java has File.copy(). 

Now, my little attempt to help: in C, when you read in a file, it only reads a line at a time.  Is it possible that that's your problem, you're only reading in the first line? If that's the case, you might need to loop until the whole file is read.  I don't know Pascal, so I have no clue if that's your problem, though :)

It looks like you shouldn't have that problem, but I do notice other potential problems:
- How does readln/writeln handle newlines?  In some languages, newlines might be read as \r\n or \n, and written as another.  You have to be careful with line termination when you're doing a binary file.
- How does the program handle NULL ('\0') characters?  Are they read into the string, then not written?  Or are they stripped out?  Or does the input end when one is reached?  iirc, Pascal doesn't use NULL-terminators, so that might not be a problem, but that's something else to think about. 

Hopefully something there will help you.

Unless, of course, somebody else solves it while I'm typing.  Bastard :)
[/quote]
With plain text documents, Pascal's readln and writeln functions seem to work just fine controlling strings that contain newlines and return carriages in them. I'm not sure how it will handle '\0'. My guess is that it will read it into the string just like any other character, but I havn't tried that yet. Thank you for the code banana fanna fo fanna. I'll try it out.

As of now I'm just going to write this application in Python. If I did know how to do it in Pascal, then I can't remember for the time being.

Update: I know how to accomplish this task in Pascal now. When I ge the time to write it and test it, I'll post it up here.
November 12, 2005, 1:37 AM
JoeTheOdd
Like iago said, [tt]system("cp <file1> <file2>");[/tt]

In Visual Basic, what you're trying to do..
[tt]Dim sTmp As String
Open File1 For Input as #1
Open File2 For Output As #2
Do While Not EOF(#1)
  Input #1, sTmp
  Print #2, sTmp
Loop
Close #2
Close #1[/tt]

I don't know any Pascal, so I can't help you there, but eh?

EDIT -
Here, a nice function! =)
[code]Public Function CopyFile(File1 As String, File2 As String) As Boolean
    Dim Ret As Boolean, Tmp As String
    On Error Goto CopyFile_Error

    Open File1 For Input As #1
        Open File2 For Output As #2
            Do While Not EOF(#1)
                Input #1, Tmp
                Print #2, Tmp
            Loop
        Close #1
    Close #1

    CopyFile = True: Exit Function
CopyFile_Error:
    Call MsgBox("Error copying file." + vbCrLf + "Error number: " + Err.Num + vbCrLf + "Description: " + Err.Description)
    CopyFile = False
End Function

'// Usage:
If CopyFile("C:\Windows\cmd.exe", "D:\Windows\cmd.exe") = True Then
    Call MsgBox("File copied successfully.")
Else
    Call MsgBox("Error copying file.")
End If[/code]
November 12, 2005, 9:20 PM
Yegg
Eww. Visual Basic. Anyways, In my case I cannot use any system commands. This is for a simple program used on my network, where a client program sends data to a server program located on another computer respectively. It's for sending files across the network. So the server receives pieces of data of a file being sent from the client. So I would have no use for such a command. Thanks anyways.
November 13, 2005, 12:02 AM
Quarantine
Dissing VB and using Pascal! Hah!

November 13, 2005, 12:16 AM
St0rm.iD
Pascal seems to be more thought-out to me.
November 13, 2005, 1:37 AM
Yegg
[quote author=Banana fanna fo fanna link=topic=13212.msg133792#msg133792 date=1131845879]
Pascal seems to be more thought-out to me.
[/quote]
*hugs *fanna*
November 13, 2005, 2:04 AM
Quarantine
It has the oddest syntax I've ever seen. The code is just painful to read sometimes.
November 13, 2005, 2:20 AM
Yegg
[quote author=Warrior link=topic=13212.msg133803#msg133803 date=1131848440]
It has the oddest syntax I've ever seen. The code is just painful to read sometimes.
[/quote]
Actually I find the code very easy to read and understand. IMO, the syntax is simpler than many languages.
November 13, 2005, 2:28 AM
St0rm.iD
[quote author=Warrior link=topic=13212.msg133803#msg133803 date=1131848440]
It has the oddest syntax I've ever seen. The code is just painful to read sometimes.
[/quote]

Think of begin and end as C-style braces.
November 13, 2005, 3:45 AM
Quarantine
I guess it depends on how fluent you are in the language, pascal and VB are about the same thing in simplicity of coding (It follows a basic-ish type syntax for the most part) but it isn't wrapped around MS which is always a good thing ;)
November 13, 2005, 3:52 AM
JoeTheOdd
[quote author=Yegg link=topic=13212.msg133779#msg133779 date=1131840150]
Eww. Visual Basic. Anyways, In my case I cannot use any system commands. This is for a simple program used on my network, where a client program sends data to a server program located on another computer respectively. It's for sending files across the network. So the server receives pieces of data of a file being sent from the client. So I would have no use for such a command. Thanks anyways.
[/quote]

You're using Windows, right? Mount the network drive as Z: or something then use a system command. You're making this too hard on yourself son.
November 13, 2005, 6:37 AM
Yegg
[quote author=Joe link=topic=13212.msg133828#msg133828 date=1131863835]
[quote author=Yegg link=topic=13212.msg133779#msg133779 date=1131840150]
Eww. Visual Basic. Anyways, In my case I cannot use any system commands. This is for a simple program used on my network, where a client program sends data to a server program located on another computer respectively. It's for sending files across the network. So the server receives pieces of data of a file being sent from the client. So I would have no use for such a command. Thanks anyways.
[/quote]

You're using Windows, right? Mount the network drive as Z: or something then use a system command. You're making this too hard on yourself son.
[/quote]
No. I'm not making it to hard on myself. And you're wasting your time for saying so. I am creating this program for the experience, despite the fact that it really isn't too hard in the first place. Don't you think I know about the network drive??
What about my computer that has Linux on it, what if I want to send a file from that computer to one of my computers with Windows on it? I'm not going to have much luck am I. This is one of the main reasons I am even creating this program.
November 13, 2005, 2:49 PM
Kp
[quote author=Yegg link=topic=13212.msg133852#msg133852 date=1131893387]What about my computer that has Linux on it, what if I want to send a file from that computer to one of my computers with Windows on it? I'm not going to have much luck am I. This is one of the main reasons I am even creating this program.[/quote]

Mount the drive as Z: using Samba on the Linux system to export it, then use a copy command? :P  Or do a CIFS mount of the Windows drive on your Linux system and copy to it that way.  Not sure if CIFS is included in default kernels though.  The advantage of the Samba approach is you don't need any kernel modules on the Linux side.

Of course, neither of these will work that well when the systems are so far apart that you can't use NetBIOS.  At that point, you should turn to WebDAV.  The Linux system can mount a WebDAV share via DAVfs.  Windows has native support for WebDAV as of IE5.  However, beware that Windows XP took a major step backward with regard to WebDAV.  Its easily accessible implemention of WebDAV sucks.  Microsoft hid the less sucky one.

To export the filesystem over WebDAV, you can use Apache's httpd with mod_dav, which is in the mainline for Apache2 and available as a third-party module for Apache1.3.  IIS supposedly supports exporting filesystems via WebDAV, but I've seen several reports that suggest it doesn't work quite right.  You're better off using Apache IMO.

Of course, if you'd just abandon Windows, you could use NFS and everything would work smoothly. ;)
November 13, 2005, 4:59 PM
Yegg
Thanks for the tip Kp. However I'll enjoy myself making a small Python script. :)
November 13, 2005, 7:35 PM
dRAgoN
[quote author=Joe link=topic=13212.msg133768#msg133768 date=1131830447]
Like iago said, [tt]system("cp <file1> <file2>");[/tt]

In Visual Basic, what you're trying to do..
[tt]Dim sTmp As String
Open File1 For Input as #1
Open File2 For Output As #2
Do While Not EOF(#1)
  Input #1, sTmp
  Print #2, sTmp
Loop
Close #2
Close #1[/tt]

I don't know any Pascal, so I can't help you there, but eh?

EDIT -
Here, a nice function! =)
[code]Public Function CopyFile(File1 As String, File2 As String) As Boolean
    Dim Ret As Boolean, Tmp As String
    On Error Goto CopyFile_Error

    Open File1 For Input As #1
        Open File2 For Output As #2
            Do While Not EOF(#1)
                Input #1, Tmp
                Print #2, Tmp
            Loop
        Close #1
    Close #1

    CopyFile = True: Exit Function
CopyFile_Error:
    Call MsgBox("Error copying file." + vbCrLf + "Error number: " + Err.Num + vbCrLf + "Description: " + Err.Description)
    CopyFile = False
End Function

'// Usage:
If CopyFile("C:\Windows\cmd.exe", "D:\Windows\cmd.exe") = True Then
    Call MsgBox("File copied successfully.")
Else
    Call MsgBox("Error copying file.")
End If[/code]
[/quote]
.....

Sub FileCopy(Source As String, Destination As String)
    Member of VBA.FileSystem
    Copies a file
November 14, 2005, 3:50 AM
EpicOfTimeWasted
[quote author=Kp link=topic=13212.msg133864#msg133864 date=1131901143]
Of course, if you'd just abandon Windows, you could use NFS and everything would work smoothly. ;)
[/quote]

I actually use NFS to access my fileserver when I'm in Windows.  Win Services for Unix isn't overly terrible.  The only thing that bugs me about it is having the mount reconnect at startup.  It seems like Windows is trying to mount it before SFU is prepared to handle the mount requests, and then bitches about how it can't connect whenever I log in.  It still connects, just makes me hit enter to get rid of the stupid error message.
November 14, 2005, 8:19 PM
Skywing
[quote author=EpicOfTimeWasted link=topic=13212.msg133947#msg133947 date=1131999587]
[quote author=Kp link=topic=13212.msg133864#msg133864 date=1131901143]
Of course, if you'd just abandon Windows, you could use NFS and everything would work smoothly. ;)
[/quote]

I actually use NFS to access my fileserver when I'm in Windows.  Win Services for Unix isn't overly terrible.  The only thing that bugs me about it is having the mount reconnect at startup.  It seems like Windows is trying to mount it before SFU is prepared to handle the mount requests, and then bitches about how it can't connect whenever I log in.  It still connects, just makes me hit enter to get rid of the stupid error message.
[/quote]
Slightly offtopic, but beware of the NFS server in SFU 3.5.  There's a nasty bug in it that will cause it to hang your system occasionally that I finally tracked down some weird hangs on one of my computers to.

Some more information -

http://support.microsoft.com/default.aspx?scid=kb;en-us;895398
http://support.microsoft.com/kb/835152/

It looks like you still have to call in to get the hotfix though; annoying.
November 14, 2005, 9:47 PM
St0rm.iD
Yegg, I'd suggest just using HTTP. urllib, urllib2, and SimpleHTTPServer are your friends (in Python).
November 15, 2005, 3:00 AM
Yegg
[quote author=Banana fanna fo fanna link=topic=13212.msg133996#msg133996 date=1132023614]
Yegg, I'd suggest just using HTTP. urllib, urllib2, and SimpleHTTPServer are your friends (in Python).
[/quote]
Yes, I know. But I wanted to create my own script that didn't require such modules. But I might just go ahead and take your advice, it will probably work out better that way.
November 15, 2005, 11:59 AM

Search