• Welcome to Valhalla Legends Archive.
 

Recreating a file

Started by Yegg, November 11, 2005, 06:25 PM

Previous topic - Next topic

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?

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.


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?

Banana fanna fo fanna

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()


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.

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 :)
This'll make an interesting test for broken AV:
QuoteX5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*


Yegg

#3
Quote from: iago on November 11, 2005, 07:31 PM
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 :)
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.

Joe[x86]

#4
Like iago said, system("cp <file1> <file2>");

In Visual Basic, what you're trying to do..
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


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

EDIT -
Here, a nice function! =)
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
Quote from: brew on April 25, 2007, 07:33 PM
that made me feel like a total idiot. this entire thing was useless.

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.

Warrior

Dissing VB and using Pascal! Hah!

Quote from: effect on March 09, 2006, 11:52 PM
Islam is a steaming pile of fucking dog shit. Everything about it is flawed, anybody who believes in it is a terrorist, if you disagree with me, then im sorry your wrong.

Quote from: Rule on May 07, 2006, 01:30 PM
Why don't you stop being American and start acting like a decent human?

Banana fanna fo fanna

Pascal seems to be more thought-out to me.

Yegg


Warrior

It has the oddest syntax I've ever seen. The code is just painful to read sometimes.
Quote from: effect on March 09, 2006, 11:52 PM
Islam is a steaming pile of fucking dog shit. Everything about it is flawed, anybody who believes in it is a terrorist, if you disagree with me, then im sorry your wrong.

Quote from: Rule on May 07, 2006, 01:30 PM
Why don't you stop being American and start acting like a decent human?

Yegg

Quote from: Warrior on November 12, 2005, 08:20 PM
It has the oddest syntax I've ever seen. The code is just painful to read sometimes.
Actually I find the code very easy to read and understand. IMO, the syntax is simpler than many languages.

Banana fanna fo fanna

Quote from: Warrior on November 12, 2005, 08:20 PM
It has the oddest syntax I've ever seen. The code is just painful to read sometimes.

Think of begin and end as C-style braces.

Warrior

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 ;)
Quote from: effect on March 09, 2006, 11:52 PM
Islam is a steaming pile of fucking dog shit. Everything about it is flawed, anybody who believes in it is a terrorist, if you disagree with me, then im sorry your wrong.

Quote from: Rule on May 07, 2006, 01:30 PM
Why don't you stop being American and start acting like a decent human?

Joe[x86]

Quote from: Yegg on November 12, 2005, 06:02 PM
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.

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 from: brew on April 25, 2007, 07:33 PM
that made me feel like a total idiot. this entire thing was useless.

Yegg

Quote from: Joe on November 13, 2005, 12:37 AM
Quote from: Yegg on November 12, 2005, 06:02 PM
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.

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.
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.