Board index » delphi » heres a {*word*218}

heres a {*word*218}

I need to reset a text file but whenever I put
reset(f)
then try to access it
readln(a, firstline)
I get a "File not open for input"
append(f) puts the pointer at the end
Can i move the pointer to the start some other way?

Thanks
Matt Hoyle

 

Re:heres a {*word*218}


Quote
Matt Hoyle wrote in message <37049E79.8BE30...@webone.com.au>...
>I need to reset a text file but whenever I put
>reset(f)
>then try to access it
>readln(a, firstline)
>I get a "File not open for input"
>append(f) puts the pointer at the end
>Can i move the pointer to the start some other way?

>Thanks
>Matt Hoyle

>readln(a, firstline)

Well, it looks like you reset file f, and are trying to read from file a.
Make sure you are accessing the correct file.

readln(f, firstline)

Hope this helps.
--

Jeff Freeman
cakew...@voicenet.com
http://www.voicenet.com/~cakewalk/

Re:heres a {*word*218}


Quote
Matt Hoyle <m...@webone.com.au> wrote:
>I need to reset a text file but whenever I put
>reset(f)
>then try to access it
>readln(a, firstline)
>I get a "File not open for input"
>append(f) puts the pointer at the end
>Can i move the pointer to the start some other way?

I noticed that you had the variable 'a' in your readln() statement.
If you have two variables, a and f defined as type text, this might
be your problem. Also, if you closed the file somewhere before
the readln() you could get the error as well.  Reset(f) would be
the idea way that I know of to put the file pointer back to the
beginning of the file.

Hope this helps,
Mike McWhinney
Chief Engineer
elja, Inc.

Re:heres a {*word*218}


Quote
In article <37049E79.8BE30...@webone.com.au> m...@webone.com.au wrote...
> I need to reset a text file but whenever I put
> reset(f)
> then try to access it
> readln(a, firstline)
> I get a "File not open for input"

Not surprising - you've reset 'f' but are reading from 'a'.

Mike{*word*106}son, Black Cat Software Factory, Edinburgh, Scotland
fax 0131-271-1551 - Columnated Ruins Domino - Mellotron M400 #996

Re:heres a {*word*218}


Sorry, That was just a typo.

I have the variable assigned as
Var a:text;

begin
 Assign(a, Path);  {Path is from another procedure which is correct}
 reset(a);
 writeln(a, 'Firstline');
 close(a);
end.
This alway give's me a "file not open for output".

begin
 Assign(a, Path);  {Path is from another procedure which is correct}
 append(a);
 reset(a);
 writeln(a, 'Firstline');
 close(a);
end.
This just writes it to the end of the file.

I cant use seek or anything and this seems not to be working :(
Is there a problem with my coding, or is there another way i can do it?

Thanks
Matt Hoyle

Quote
Mike McWhinney wrote:

> Matt Hoyle <m...@webone.com.au> wrote:

> >I need to reset a text file but whenever I put
> >reset(f)
> >then try to access it
> >readln(a, firstline)
> >I get a "File not open for input"
> >append(f) puts the pointer at the end
> >Can i move the pointer to the start some other way?

> I noticed that you had the variable 'a' in your readln() statement.
> If you have two variables, a and f defined as type text, this might
> be your problem. Also, if you closed the file somewhere before
> the readln() you could get the error as well.  Reset(f) would be
> the idea way that I know of to put the file pointer back to the
> beginning of the file.

> Hope this helps,
> Mike McWhinney
> Chief Engineer
> elja, Inc.

Re:heres a {*word*218}


Quote
Matt Hoyle wrote:

> I have the variable assigned as
> Var a:text;

> begin
>  Assign(a, Path);  {Path is from another procedure which is correct}
>  reset(a);
>  writeln(a, 'Firstline');
>  close(a);
> end.
> This alway give's me a "file not open for output".

Of course. Textfile - reset is made for "READ" only.

Quote
> begin
>  Assign(a, Path);  {Path is from another procedure which is correct}
>  append(a);
>  reset(a);
>  writeln(a, 'Firstline');
>  close(a);
> end.
> This just writes it to the end of the file.

Append and Rewrite are made for "WRITE" only.
Quote

> I cant use seek or anything and this seems not to be working :(
> Is there a problem with my coding, or is there another way i can do it?

The problem is with your misunderstanding of Text - files.

A text file is made as a "serial" file. There is no means to
seek or read/write randomly. You can understand that very similarly
to a serial port (the words are the same, but they have different
meaning of course: with the port the bits are written serially).

For random files use a file of char or an untyped file with
blockread and blockwrite, but here you have to handle all r/w
"manually". The benefit is: they are randomly accessible.
--
Franz Glaser, Glasau 3, A-4191 Vorderweissenbach Austria ++43-7219-7035
Muehlviertler Elektronik Glaser. Industrial control and instrumentation
-----------------------------------------------------------------------
http://members.eunet.at/meg-glaser           mailto:meg-gla...@eunet.at
http://www.geocities.com/~franzglaser  http://members.xoom.com/f_glaser

Re:heres a {*word*218}


Quote
Matt Hoyle wrote:
> So does this mean there no way easy way to write a pascal search and
> replace program then?
> Do i have to read the entire file into variables then do the
> search/replace then rewrite the file and write all the variables back in
> again.

No.  Do something like this:

var
  infile,outfile: text;
begin
  assign(infile,infilename);
  assign(outfile,'temp.tmp');
  reset(infile);
  rewrite(outfile);
  while not eof(infile) do begin
    readln(infile,line);
    modify line as you wish
    writeln(outfile,line);
  end;
  close(infile);
  close(outfile);
  erase(infile);
  rename(outfile,infilename);
end.

Re:heres a {*word*218}


It was a typo:(
Quote
Mike{*word*106}son wrote:

> In article <37049E79.8BE30...@webone.com.au> m...@webone.com.au wrote...

> > I need to reset a text file but whenever I put
> > reset(f)
> > then try to access it
> > readln(a, firstline)
> > I get a "File not open for input"

> Not surprising - you've reset 'f' but are reading from 'a'.

> Mike{*word*106}son, Black Cat Software Factory, Edinburgh, Scotland
> fax 0131-271-1551 - Columnated Ruins Domino - Mellotron M400 #996

Re:heres a {*word*218}


Thanks for the info, I have been using types files recently so just
assumed it would work the same in text files.
So does this mean there no way easy way to write a pascal search and
replace program then?
Do i have to read the entire file into variables then do the
search/replace then rewrite the file and write all the variables back in
again.

Matt Hoyle

Quote
"Ing. Franz Glaser" wrote:

> Matt Hoyle wrote:

> > I have the variable assigned as
> > Var a:text;

> > begin
> >  Assign(a, Path);  {Path is from another procedure which is correct}
> >  reset(a);
> >  writeln(a, 'Firstline');
> >  close(a);
> > end.
> > This alway give's me a "file not open for output".
> Of course. Textfile - reset is made for "READ" only.

> > begin
> >  Assign(a, Path);  {Path is from another procedure which is correct}
> >  append(a);
> >  reset(a);
> >  writeln(a, 'Firstline');
> >  close(a);
> > end.
> > This just writes it to the end of the file.
> Append and Rewrite are made for "WRITE" only.

> > I cant use seek or anything and this seems not to be working :(
> > Is there a problem with my coding, or is there another way i can do it?
> The problem is with your misunderstanding of Text - files.

> A text file is made as a "serial" file. There is no means to
> seek or read/write randomly. You can understand that very similarly
> to a serial port (the words are the same, but they have different
> meaning of course: with the port the bits are written serially).

> For random files use a file of char or an untyped file with
> blockread and blockwrite, but here you have to handle all r/w
> "manually". The benefit is: they are randomly accessible.
> --
> Franz Glaser, Glasau 3, A-4191 Vorderweissenbach Austria ++43-7219-7035
> Muehlviertler Elektronik Glaser. Industrial control and instrumentation
> -----------------------------------------------------------------------
> http://members.eunet.at/meg-glaser           mailto:meg-gla...@eunet.at
> http://www.geocities.com/~franzglaser  http://members.xoom.com/f_glaser

Re:heres a {*word*218}


Quote
Matt Hoyle wrote:

> Thanks for the info, I have been using types files recently so just
> assumed it would work the same in text files.
> So does this mean there no way easy way to write a pascal search and
> replace program then?
> Do i have to read the entire file into variables then do the
> search/replace then rewrite the file and write all the variables back in
> again.

Once again: A textfile is a serial stream of characters. The lines
can be different in length, the text may contain control characters
and Esc - codes, tabs and delimiters.

Where would you like to write the replaced string if it was
lengthened? Or opposite, how would you like to fill the gaps if
the string was shortened?

If you want to write a search/replace routine you must first be
aware of the kind of data. There are sophisticated texteditors on
the market which work with data in files - but surely they do not
use primitive text files. You can treat the file as a file of char,
or even better as an untyped file - using the block R/W procedures
as described in the previous mail. But all handling is YOUR task.

Whenever there is a chance that the text modified the size, you
have to buffer it in RAM memory, what else?

YOU simply use the WRONG filetype, there is no reason
to doubt about Pascal's (whichever compiler) functionality.
--
Franz Glaser, Glasau 3, A-4191 Vorderweissenbach Austria ++43-7219-7035
Muehlviertler Elektronik Glaser. Industrial control and instrumentation
-----------------------------------------------------------------------
http://members.eunet.at/meg-glaser           mailto:meg-gla...@eunet.at
http://www.geocities.com/~franzglaser  http://members.xoom.com/f_glaser

Re:heres a {*word*218}


Matt Hoyle [mailto:m...@webone.com.au] decided to regale us with

Quote
>Thanks for the info, I have been using types files recently so just
>assumed it would work the same in text files.
>So does this mean there no way easy way to write a pascal search and
>replace program then?
>Do i have to read the entire file into variables then do the
>search/replace then rewrite the file and write all the variables back in
>again.

Two options present themselves to you:

1. Use Text Files and read each line individually from one file, check
for search and replace string if needed. Output all lines into new file.
At end of file close them both, erase input file and rename the
temporary output file - see SetTextBuf as well.

2. Use the type "file" and use blockread into a buffer. Search the block
copying or replacing as necessary. Output each buffer into a new file.
At end of file close them both, erase input file and rename the
temporary output file.

Whichever way you do it, you do need to think about the logistics of
*how* you do it, remembering that search and replace may lengthen,
decrease, or keep the same byte size.

--
Pedt
sig away for Easter

Re:heres a {*word*218}


Gee!  Someone's taking that discussion a little personal!

Quote
-- nz Glaser wrote in message <3706A246.32907...@eunet.at>...
>Matt Hoyle wrote:

>> Thanks for the info, I have been using types files recently so just
>> assumed it would work the same in text files.
>> So does this mean there no way easy way to write a pascal search
and
>> replace program then?
>> Do i have to read the entire file into variables then do the
>> search/replace then rewrite the file and write all the variables
back in
>> again.

>Once again: A textfile is a serial stream of characters. The lines
>can be different in length, the text may contain control characters
>and Esc - codes, tabs and delimiters.

>Where would you like to write the replaced string if it was
>lengthened? Or opposite, how would you like to fill the gaps if
>the string was shortened?

>If you want to write a search/replace routine you must first be
>aware of the kind of data. There are sophisticated texteditors on
>the market which work with data in files - but surely they do not
>use primitive text files. You can treat the file as a file of char,
>or even better as an untyped file - using the block R/W procedures
>as described in the previous mail. But all handling is YOUR task.

>Whenever there is a chance that the text modified the size, you
>have to buffer it in RAM memory, what else?

>YOU simply use the WRONG filetype, there is no reason
>to doubt about Pascal's (whichever compiler) functionality.
>--
>Franz Glaser, Glasau 3, A-4191 Vorderweissenbach Austria
++43-7219-7035
>Muehlviertler Elektronik Glaser. Industrial control and
instrumentation
>---------------------------------------------------------------------
--
>http://members.eunet.at/meg-glaser

mailto:meg-gla...@eunet.at
Quote
>http://www.geocities.com/~franzglaser

http://members.xoom.com/f_glaser

Re:heres a {*word*218}


Quote
In article <3705744E.89CD7...@webone.com.au> m...@webone.com.au wrote...
> begin
>  Assign(a, Path);  {Path is from another procedure which is correct}
>  reset(a);
>  writeln(a, 'Firstline');
>  close(a);
> end.
> This alway give's me a "file not open for output".

Assuming there are no other typos (!), the problem here is that you are
using reset() on a text file, which gives read-only access. You can't
write to a text file opened with reset(). You either have to use
rewrite() or append().

Mike{*word*106}son, Black Cat Software Factory, Edinburgh, Scotland
fax 0131-271-1551 - Columnated Ruins Domino - Mellotron M400 #996

Re:heres a {*word*218}


hehe,
I worked it out the first time, just didn't specify that i knew now that
i could work in the middle of text files.
Quote
Bobby B wrote:

> Gee!  Someone's taking that discussion a little personal!

> -- nz Glaser wrote in message <3706A246.32907...@eunet.at>...
> >Matt Hoyle wrote:

> >> Thanks for the info, I have been using types files recently so just
> >> assumed it would work the same in text files.
> >> So does this mean there no way easy way to write a pascal search
> and
> >> replace program then?
> >> Do i have to read the entire file into variables then do the
> >> search/replace then rewrite the file and write all the variables
> back in
> >> again.

> >Once again: A textfile is a serial stream of characters. The lines
> >can be different in length, the text may contain control characters
> >and Esc - codes, tabs and delimiters.

> >Where would you like to write the replaced string if it was
> >lengthened? Or opposite, how would you like to fill the gaps if
> >the string was shortened?

> >If you want to write a search/replace routine you must first be
> >aware of the kind of data. There are sophisticated texteditors on
> >the market which work with data in files - but surely they do not
> >use primitive text files. You can treat the file as a file of char,
> >or even better as an untyped file - using the block R/W procedures
> >as described in the previous mail. But all handling is YOUR task.

> >Whenever there is a chance that the text modified the size, you
> >have to buffer it in RAM memory, what else?

> >YOU simply use the WRONG filetype, there is no reason
> >to doubt about Pascal's (whichever compiler) functionality.
> >--
> >Franz Glaser, Glasau 3, A-4191 Vorderweissenbach Austria
> ++43-7219-7035
> >Muehlviertler Elektronik Glaser. Industrial control and
> instrumentation
> >---------------------------------------------------------------------
> --
> >http://members.eunet.at/meg-glaser
> mailto:meg-gla...@eunet.at
> >http://www.geocities.com/~franzglaser
> http://members.xoom.com/f_glaser

Other Threads