Solved

Open file as: 10101001101....?

Posted on 2001-07-01
11
237 Views
Last Modified: 2010-04-06
Hi.
How can i open a file as integer:101011010100011110001010111 ???

How can i put the file together again, and save it??

It should be able to be displayed as a string (inttostr).

Thanks.
Dennis
0
Comment
Question by:Dennis9
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 3
  • +1
11 Comments
 
LVL 2

Expert Comment

by:bugroger
ID: 6242716
Hi,

"open a file as integer: 101010101010101010?"
 do you mean as bits?

0
 
LVL 1

Author Comment

by:Dennis9
ID: 6242760
Yes, but can they  be converted to integer or string?

Dennis
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 6242782
just from head, not tested

var
  f : file of integer;
  i : Integer;
begin
  assignfile(f,AFilename);
  rewrite(f);
  for i := 0 to 50 do
    write(f,I);
  closefile(f);
  reset(f);
  seek(f,25); //read the 26th Integervalue
  read(f,i);
  showmessage(IntToStr(i);
  closefile(f);
end;

meikl ;-)
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 2

Expert Comment

by:bugroger
ID: 6242805
Hi,
 here are 3 functions to convert a byte, word and dword
 to a string of BITS and 1 function to convert a string
 of BITS to INTEGER.
 I don't know if it that what you want.

Function ByteToBit(b : byte) : String;
Var
 z : byte;
Begin
 Result := '';
 For z := 1 to 8 do
 Begin
  IF b AND 128 = 128 then Result := Result + '1'
                     else Result := Result + '0';
  b := b SHL 1;
 End;
End;

Function WordToBit(w : word) : string;
Var
 z : byte;
Begin
 Result := '';
 For z := 1 to 16 do
 Begin
  IF w AND 32768 = 32768 then Result := Result + '1'
                         else Result := Result + '0';
  w := w SHL 1;
 End;
End;

Function DWordToBit(d : dword) : string;
Var
 z : byte;
Begin
 Result := '';
 For z := 1 to 32 do
 Begin
  IF d AND $80000000 = $80000000 then Result := Result + '1'
                                 else Result := Result + '0';
  d := d SHL 1;
 End;
End;

Function BitsToInt(s : string) : Integer;
Var
 z : byte;
Begin
 Result := 0;
 For z := 1 to Length(s) do
 Begin
  IF s[z] = '1' then Result := Result +1;
  IF z <> Length(s) then Result  :=  Result  SHL 1;
 End;
End;
0
 
LVL 1

Author Comment

by:Dennis9
ID: 6243139
Hi.
I just need a way to open a file, and have it as an integer or string, and then i should be able to save the string/integer back to file. (copy of it)

Dennis
0
 
LVL 2

Expert Comment

by:bugroger
ID: 6243152
like a hex editor?
0
 
LVL 1

Expert Comment

by:edsteele
ID: 6245123
Why don't you use TFileStream.  You can open the file as a stream, then you can read bytes from it into a buffer and do anything you want with the buffer.

Does this help?
Eric
0
 
LVL 1

Author Comment

by:Dennis9
ID: 6245143
I will like it as text, but if u can tell me more about the buffer stuff it would be great!

Dennis
0
 
LVL 1

Expert Comment

by:edsteele
ID: 6245391
Well, a lot of using a stream is knowing what the data you are reading in will look like.  Do you have a good idea of what you are reading in?

Here are some examples.

Reading an integer:

After defining MyIntBuffer as an Integer, you can execute the following code to read an integer from the stream.

MyFileStream.Read(MyIntBuffer, SizeOf(MyIntBuffer));

Reading a string:

After defining MyCharBuffer as an Array of Char, you can execute the following code to read a string from the stream.  The catch is that you need to know how much to read from the stream.

MyFileStream.Read(MyCharBuffer, NumCharsToRead);

Maybe this will work for you and maybe it's too much work.  You have to decide that.  :)

Eric
0
 
LVL 1

Author Comment

by:Dennis9
ID: 6245627
Hi.
I need to read all from the stream, is there a way to do that?

Dennis
0
 
LVL 1

Accepted Solution

by:
edsteele earned 20 total points
ID: 6245666
Do you know the size of the file you will be loading?  If it's not big, just create an Array of Char of the size you need and read the whole file in at once.  The Read method of the TFileStream class returns the actual number of bytes read, so you can tell how much data was actually loaded.  If the size of the file is unknown or it will be large (you can define "large"), you can process the file a chunk at a time:

var
  MyCharBuffer: Array[0..1023] of Char;

while MyFileStream.Read(MyCharBuffer, 1024) do
begin
  Process(MyCharBuffer);
end;

This piece of code will read in 1KB chunks and pass the buffer to the Process procedure (which you will need to write).  The Process procedure could be as simple as converting the array to a string and storing it.

If I had a better idea of what you are trying to accomplish, my examples could be more directed.

Eric
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Downloading email attachments 2 92
Dynamically Created Query 3 76
Browsing a TTreeView in Delphi 5 43
How to add columns to a RAD Studio FMX grid at design time 1 54
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

740 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question