the title it's self explanatory
i am using freepascal with the Lazarus IDE and this is quite a challenge for a noob like me
so please the code with some explanation

this is what i worked for me:

var f,g:text;
x:string;


begin
assign(f, 'd:\pascal\dosare\in.txt');
assign(g,'d:\pascal\dosare\out.txt');
x:='d:\pascal\dosare\in.txt';
reset(f);
rewrite(g);
while not eof(f) do
begin
read(f,x);


write(g,x);
end;
close(f);
close(g);

end.

let's see... :D

(*your source code interpretered...by me*)
program text_files;

var f,(*in.txt*)
    g:text;(*out.txt*)
    x:string;  (*one string...*)

begin

     (*in.txt's path assigned to f var*)
     assign(f, 'd:\pascal\dosare\in.txt');
     (*out.txt's path assigned to g var*)
     assign(g,'d:\pascal\dosare\out.txt');
     (*x's value is be that string..?,why?,if you don't use it?*)
     x:='d:\pascal\dosare\in.txt';
     reset(f);   (*open for reading f*)
     rewrite(g); (*create g or clear its contents*)
     (*while we do not reach the end of file f do*)
     while not eof(f) do  begin
           (*read one string (max 256 char) from f (in.txt) to the puffer*)
           read(f,x);
           (*and write the readed value of x to g(out.txt)*)
           write(g,x);
     end;
     (*last close all opened file*)
     close(f);
     close(g);
     (*wait to press ENTER to quit...*)
     readln;
end.         (*end of main program...*)

now look at that,compare with yours!

program text_files_fixed_version;

var f,(*in.txt*)
    g:text;(*out.txt*)
    x:string;  (*one string...*)

begin

     (*in.txt's path assigned to f var*)
     assign(f, 'in.txt'); (*this is in the bin dir where the pascal installed*)

     (*out.txt's path assigned to g var*)
     assign(g,'out.txt');

     (*x's value is a path,I think...*)
     x:='d:\pascal\dosare\in.txt';

     (*so let's write it to f*)
     rewrite(f);
     write(f,x); (*to write x's contents to f (in.txt)*)
     x:='';

     reset(f);   (*open for reading f*)
     rewrite(g); (*create g or clear its contents*)

     (*while we do not reach the end of file f do*)
     while not eof(f) do  begin
           (*read one line from f (in.txt) to the puffer*)
           readln(f,x);
           (*and write the readed value of x to g(out.txt)*)
           writeln(g,x);
     end;
     (*last close all opened file*)
     close(f);
     close(g);
     (*wait to press ENTER to quit...*)
     readln;
end.         (*end of main program...*)

(*created (fixed) by FlamingClaw 2010.03.30.Hungary*)

Edited 6 Years Ago by FlamingClaw: n/a

i don't know what u r trying to show me.....if this work's for me, i want to copy the entire content of the file not just one line, and i must move my folders where i want

i don't know what u r trying to show me.....if this work's for me, i want to copy the entire content of the file not just one line, and i must move my folders where i want

You can store your file where you want to.
If you want to copy one file's contents to another then,you have to
read the file line by line,and write it to another line by line.
Got it?
First I was trying to create the in.txt and fill some text to copy something ,got it?
If the in.txt is empty then what do you want to copy? :'(

[#
program text_files_fixed_version;
#

#
var f,(*in.txt*)
#
g:text;(*out.txt*)
#
x:string; (*one string...*)
#

#
begin
#

#(*in.txt's path assigned to f var*)
#assign(f, 'in.txt'); (*this is in the bin dir where the pascal installed*)
#

#(*out.txt's path assigned to g var*)
#assign(g,'out.txt');
#

#
(*x's value is a path,I think...*)
#
x:='d:\pascal\dosare\in.txt';
#

#
(*so let's write it to f*)
#
rewrite(f);
#
write(f,x); (*to write x's contents to f (in.txt)*)
#
x:='';
#

#
reset(f); (*open for reading f*)
#
rewrite(g); (*create g or clear its contents*)
#

#
(*while we do not reach the end of file f do*)
#
while not eof(f) do begin
#
(*read one line from f (in.txt) to the puffer*)
#
readln(f,x);
#
(*and write the readed value of x to g(out.txt)*)
#
writeln(g,x);
#
end;
#
(*last close all opened file*)
#
close(f);
#
close(g);
#
(*wait to press ENTER to quit...*)
#
readln;
#
end. (*end of main program...*)


ok i understand, but in fact f is d:\pascal\dosare\in.txt, so why write x to f if f is x?

sorry if i seem dumb but i am new to programming and i think that only by asking can i learn more

Edited 6 Years Ago by thusarix: n/a

{look at that...we're conditioning that in.txt isn't empty...}
program text_files_version0002;

var f,(*in.txt*)
    g:text;(*out.txt*)
    x,y:string;  (*two string to store the paths of the two files.*)
    temp:string;(*to read/write the lines of the specified file(s)*)
begin
     (*x will store the in.txt's path.*)
     x:='d:\pascal\dosare\in.txt';

     (*and y'll store the out.txt's path.*)
     y:='d:\pascal\dosare\out.txt';

     (*in.txt's path assigned to f var*)
     assign(f,x);

     (*out.txt's path assigned to g var*)
     assign(g,y);


     reset(f);   (*open for reading f*)
     rewrite(g); (*create g or clear its contents*)

     (*while we do not reach the end of file f do*)
     while not eof(f) do  begin
           (*read one line from f (in.txt) to the puffer*)
           readln(f,temp);
           (*and write the readed value of temp to g(out.txt)*)
           writeln(g,temp);
     end;
     (*last close all opened file*)
     close(f);
     close(g);
     (*wait to press ENTER to quit...*)
     readln;
end.         (*end of main program...*)

(*created (fixed) by FlamingClaw 2010.04.2.Hungary*)
var
	input,output : text ; loc1,loc2,x : string ;
begin
	writeln('enter location and file name of input file : ');readln(loc1);
	assign(input,loc1);	
	reset(input);
	writeln('enter location and file name for output file :  ');
	readln(loc2);
	assign(output,loc2);	
	rewrite(output);
	while not eof(input) do
		begin
					readln(input,x);
					writeln(output,x);
		end;
	close(input);
	close(output);
	readln 
end.

I have several output files (*.out) contain a lot of information. I would like to have a program can just sort specific data from each output file and save as the same filename with txt extension.

While reading each line (from *.out) if you are on a line that says "state summary:" then save next 10 lines (into *.txt). Please advise. Thanks!

with TStringList.Create do
  try
    LoadFromFile('a.txt');
    SaveToFile('b.txt');
  finally
    Free;
  end;

or WinAPI
BOOL CopyFile(
LPCTSTR lpExistingFileName, // pointer to name of an existing file
LPCTSTR lpNewFileName, // pointer to filename to copy to
BOOL bFailIfExists // flag for operation if file exists );

well ..
if the "state summary" located at the first of the line then you can take the first 13 character of every line and check wither this string equals the "state summary " phrase , if it does then you copy the whole line to a new .txt file , this may not be the best idea but good luck :)

This article has been dead for over six months. Start a new discussion instead.