Board index » delphi » Pascal Problem

Pascal Problem

Here's my problem:  I have a variable (call it s ) and I write it to a
file (call it f) and then I clear it using  s := '';  
and then I write something else to the variable and this goes to the file.
 Then I show the contents of the file (f) using assign and reset.  But
when I reset the file (f)and have each line read like this:
   ( oneline: string[80] )

    while not EOF (f) do
      begin
       readln (f, oneline);
       writeln (oneline)
      end;

After doing this, there are still two spaces read, but one is blank (from
when I cleared the variable) and the other has the information that I
wrote before.  How do I get rid of the blank line so that only the line
that I wrote something to is shown?  Thanx for your help

  email:                 Patrick.Kenn...@bbs.mhv.net

 

Re:Pascal Problem


Patrick, I see nothing wrong with the way you are reading the lines, so if
you are reading a blank line from the file, it must be because you wrote
one there.

I suggest that you bench test the procedure you used to write to the file.  
If you still can't find the problem after reading and thinking about the
statements you've written, put them in a follow-up message and we'll try to
explain what you are doing wrong.

                      ...red

Knowledge is one of the few things that you
can give away and still keep for yourself.

Quote
In article <4jhpsm$...@newsbf02.news.aol.com> JKenn23333 wrote:

>Here's my problem:  I have a variable (call it s ) and I write it to a
>file (call it f) and then I clear it using  s := '';  
>and then I write something else to the variable and this goes to the file.
> Then I show the contents of the file (f) using assign and reset.  But
>when I reset the file (f)and have each line read like this:
>   ( oneline: string[80] )

>    while not EOF (f) do
>      begin
>       readln (f, oneline);
>       writeln (oneline)
>      end;

>After doing this, there are still two spaces read, but one is blank (from
>when I cleared the variable) and the other has the information that I
>wrote before.  How do I get rid of the blank line so that only the line
>that I wrote something to is shown?  Thanx for your help

>  email:                 Patrick.Kenn...@bbs.mhv.net

Re:Pascal Problem


Quote
In article <4jmpir$...@news-e2b.gnn.com>, R.E.Donais@?.?.com wrote:

>Patrick, I see nothing wrong with the way you are reading the lines, so if
>you are reading a blank line from the file, it must be because you wrote
>one there.

AFAIR, not quite.  Some software interprets CR as "move to Col.1" and LF
as "Move down a row with respect to the text", whereas (AIUI) Readln(S)
treats CR as EOL and ignores a subsequent LF.

Hence if somehow you have got "Text1 CR CR LF Text2", it will often show
as "Text1/Text2" but can look to Readln like "Text1//Text2".
--
John Stockton, Surrey, UK.  Turnpike v1.12.  MIME.

Re:Pascal Problem


In article <4jhpsm$...@newsbf02.news.aol.com>, jkenn23...@aol.com says...
.>
.>Here's my problem:  I have a variable (call it s ) and I write it to a
.>file (call it f) and then I clear it using  s := '';  
.>and then I write something else to the variable and this goes to the file.
.> Then I show the contents of the file (f) using assign and reset.  But
.>when I reset the file (f)and have each line read like this:
.>   ( oneline: string[80] )
.>
.>    while not EOF (f) do
.>      begin
.>       readln (f, oneline);
.>       writeln (oneline)
.>      end;
.>
.>After doing this, there are still two spaces read, but one is blank (from
.>when I cleared the variable) and the other has the information that I
.>wrote before.  How do I get rid of the blank line so that only the line
.>that I wrote something to is shown?  Thanx for your help
.>
.>  email:                 Patrick.Kenn...@bbs.mhv.net
.>

     What are the types of the variables "s" and "f"?  I'm assuming the
following:

  VAR
   s : char;
   f : text;

     Next, how is f being written?  Which represents the correct code?

     s := 'a';                  s := 'a';
     write (f, s);              writeln (f, s);
     s := ' ';                  s := ' ';
     s := 'b';                  s := 'b';
     write (f, b);              writeln (f, b);

[The key question is, are you using "write" or "writeln"?].

     I realized in the code example above that you specified s := '', which
suggests that s might be a Borland type (i.e. non-Standard Pascal) of
"string", which I know little about.  If this is so, then what I'm about to
say may be wrong (since I don't know how Borland handles this non-standard
data structure ...).

     Generally, when using read/readln with file variables of type "text",
there is a marker (implementation-dependent) that defines the end of the
line (eoln).  I believe that, according to the Pascal standard, you can
read up to, and including, this end-of-line character, which reads as a
space.  To test whether the space you read was or was not the EOL character,
you can use the eoln(f) function.

Bob Schor
Pascal Enthusiast

Re:Pascal Problem


Quote
jkenn23...@aol.com (JKenn23333) wrote:
>Here's my problem:  I have a variable (call it s ) and I write it to a
>file (call it f) and then I clear it using  s := '';  
>and then I write something else to the variable and this goes to the file.
> Then I show the contents of the file (f) using assign and reset.  But
>when I reset the file (f)and have each line read like this:

no need for another assign, just reset(f);

Quote
>   ( oneline: string[80] )
>    while not EOF (f) do
>      begin
>       readln (f, oneline);
>       writeln (oneline)
>      end;
>After doing this, there are still two spaces read, but one is blank (from
>when I cleared the variable) and the other has the information that I
>wrote before.  How do I get rid of the blank line so that only the line
>that I wrote something to is shown?  Thanx for your help

The code that does the writing would be useful to see. I assume
something like:
...
var s:string[80];
file    f:text;

assign(f:"data.dat");
rewrite(f);
s:='something1';
writeln(f,s);
s:=' ';
s:='something2';
writeln(f,s);
...

If the second writeln(f,s) occurs before you input new text to 's'
then the blank will be written to the output file.

---------------------------------------
Dean, VE1CBF        VE1...@VE1CBF.ns.ca
wdmil...@atcon.com  FN96VC

- - ...  ... - -

Re:Pascal Problem


On Tue, 02 Apr 1996 06:43:31 GMT, wdmil...@atcon.com (W. D. Milner)
wrote:

Quote
>jkenn23...@aol.com (JKenn23333) wrote:
>> Then I show the contents of the file (f) using assign and reset.  But
>>when I reset the file (f)and have each line read like this:

>no need for another assign, just reset(f);

In fact, calling assign() without a call to close() before will cause
a bug, because the file will never be closed.

Duncan Murdoch

Other Threads