Board index » delphi » ADDING TEXT TO A TEXT FILE

ADDING TEXT TO A TEXT FILE

Look im really sorry about me posting all these annoying question but im not
getting the rite answer ok now ill explain exactly what i want so i can get
my self the rite answer .
ok look i want to make a program which add a string of text in a text file
for example i want my prorgam to keep on running every time windows boots up
so to do that i could do it through the registry but i want to do it through
the win.ini and i want to add this text to it run=c:\hi\hi.exe and i want to
put it in the correct place for example
there is lots of shit here rite
[Desktop]
Wallpaper=C:\WINDOWS\INTERN~1.BMP
TileWallpaper=0
WallpaperStyle=0
Pattern=(None)

[windows]
NullPort=None
MouseTrails=-1
Programs=com exe bat pif scr
device=HP DeskJet 720C v10.3,HPFDJC14,LPT1:
BLA BLA BLA...........
bla ............

how would i want to put the string of text in the correct place under
[windows] not under [desktop]
help me and i dont want to delte the other text i want it to stay the same.

 

Re:ADDING TEXT TO A TEXT FILE


Quote
trip wrote:

> Look im really sorry about me posting all these annoying question but im not
> getting the rite answer ok now ill explain exactly what i want so i can get
> my self the rite answer.

A clear definition of the problem helps.

Quote
> ok look i want to make a program which add a string of text in a text file
> for example i want my prorgam to keep on running every time windows boots up
> so to do that i could do it through the registry but i want to do it through
> the win.ini and i want to add this text to it run=c:\hi\hi.exe and i want to
> put it in the correct place for example
> there is lots of shit here rite
> [Desktop]
> Wallpaper=C:\WINDOWS\INTERN~1.BMP
> TileWallpaper=0
> WallpaperStyle=0
> Pattern=(None)

> [windows]
> NullPort=None
> MouseTrails=-1
> Programs=com exe bat pif scr
> device=HP DeskJet 720C v10.3,HPFDJC14,LPT1:
> BLA BLA BLA...........
> bla ............

> how would i want to put the string of text in the correct place under
> [windows] not under [desktop]
> help me and i dont want to delte the other text i want it to stay the same.

I take it that you want to insert a specified piece of text at a
specified point in a text file.

The basic solution is to read the text file, line by line, writing each
line to a new text file until you come to the point at which you want to
insert your text.  At this point, write your text to the new file.  Then
continue to read the input, writing it to the new file.  When you reach
the end of the input file, close both files, delete the input file, and
rename the new file to the name the input file had.

As to your specific example, however, using a text editor is a better
solution.  Unless you are trying to develop a virus, of course.  In
which case, you needn't bother with this approach.  It's been done
already.

Al Moore

Re:ADDING TEXT TO A TEXT FILE


Look im really sorry about me posting all these annoying question but im not
getting the rite answer ok now ill explain exactly what i want so i can get
my self the rite answer .
ok look i want to make a program which add a string of text in a text file
for example i want my prorgam to keep on running every time windows boots up
so to do that i could do it through the registry but i want to do it through
the win.ini and i want to add this text to it run=c:\hi\hi.exe and i want to
put it in the correct place for example
there is lots of shit here rite
[Desktop]
Wallpaper=C:\WINDOWS\INTERN~1.BMP
TileWallpaper=0
WallpaperStyle=0
Pattern=(None)

[windows]
NullPort=None
MouseTrails=-1
Programs=com exe bat pif scr
device=HP DeskJet 720C v10.3,HPFDJC14,LPT1:
BLA BLA BLA...........
bla ............

how would i want to put the string of text in the correct place under
[windows] not under [desktop]
help me and i dont want to delte the other text i want it to stay the same.

Re:ADDING TEXT TO A TEXT FILE


Hi,

Quote
trip wrote:
> ok look i want to make a program which add a string of text in a text file
> for example i want my prorgam to keep on running every time windows boots up
> so to do that i could do it through the registry but i want to do it through
> the win.ini and i want to add this text to it run=c:\hi\hi.exe and i want to
> put it in the correct place

Open the file win.ini. Then open a temporary file, for example in your
working directory. (Both files should be opened as text files.) Then
you read the win.ini line by line, and write every line into the temp.
file. However, after writing the line '[windows]', you insert your own
line.

For example, let's assume you have to following function:

   function upstr(s : string) : string;
   var i : integer;
   begin
      for i := 1 to length(s) do s[i] := upcase(s[i]);
   end;

...your code might look like the following:

   while not eof(winini_filevar) do
   begin
      readln(winini_filevar, line);
      writeln(temp_filevar, line);
      if upstr(line) = '[WINDOWS]' then
         writeln(temp_filevar, 'run=c:\hi\hi.exe');
   end;

Then you close both files, delete the win.ini and copy the temporary
file to the win.ini, which could be done in the following way:

   swapvectors;
   exec(getenv('COMSPEC'), '/C del c:\windows\win.ini');
   exec(getenv('COMSPEC'), '/C copy temp.txt c:\windows\win.ini');
   swapvectors;

You can also copy files using BlockRead/BlockWrite.

 - Sebastian

Other Threads