•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the Pascal and Delphi section within the Software Development category of DaniWeb, a massive community of 456,611 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,430 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Pascal and Delphi advertiser: Programming Forums
Views: 5527 | Replies: 46 | Solved
![]() |
| |
•
•
Join Date: Nov 2007
Posts: 87
Reputation:
Rep Power: 2
Solved Threads: 1
ok so basically I am writing a small program that does a few different things in Delphi 2007. I am very new to coding and so I hope that my terminology will be up to par. If not, please forgive me.
I am loading text files into a ListView... I know arrays can do this much faster, but I am trying to do this in a more visual way. When I load a list of oh, say 15,000 lines... I would like to be able to split the list and then save them into smaller lists. I have an editbox to display how many lines to use for splitting..
For example: I want to split the 15,000 line text file into 250 lines.. or maybe 3,000 lines. I cant for the life of me figure out how to do this.
Once the files are split, then naturally they need to be saved as well.. and that is the other problem.
ANY help on this would be a huge relief!
My code is as follows and is horribly wrong i am WELL aware:
procedure TForm1.Button12Click(Sender: TObject);
var
txt : textfile;
x,y : integer;
begin
savedialog1.Execute();
AssignFile(txt, saveDialog1.FileName + '.txt');
ReWrite(txt);
for x := 0 to ListView1.Items.Count - 1 do
begin
Writeln(txt, ListView1.Items[x].Caption, edit6.text);
end;
end;
As you can see, this is extremely fouled up and I have been trying for hours to figure this out. Thanks for any help in advance.
I am loading text files into a ListView... I know arrays can do this much faster, but I am trying to do this in a more visual way. When I load a list of oh, say 15,000 lines... I would like to be able to split the list and then save them into smaller lists. I have an editbox to display how many lines to use for splitting..
For example: I want to split the 15,000 line text file into 250 lines.. or maybe 3,000 lines. I cant for the life of me figure out how to do this.
Once the files are split, then naturally they need to be saved as well.. and that is the other problem.
ANY help on this would be a huge relief!
My code is as follows and is horribly wrong i am WELL aware:
procedure TForm1.Button12Click(Sender: TObject);
var
txt : textfile;
x,y : integer;
begin
savedialog1.Execute();
AssignFile(txt, saveDialog1.FileName + '.txt');
ReWrite(txt);
for x := 0 to ListView1.Items.Count - 1 do
begin
Writeln(txt, ListView1.Items[x].Caption, edit6.text);
end;
end;
As you can see, this is extremely fouled up and I have been trying for hours to figure this out. Thanks for any help in advance.
•
•
Join Date: Oct 2007
Location: Cherry Hill, NJ
Posts: 1,878
Reputation:
Rep Power: 13
Solved Threads: 193
The choice of TListView is a bit odd to me. You could have just as easily used a TListBox or TRichEdit component, which will give you an items: TStrings or lines: TStrings property, as well as LoadFromFile and SaveToFile methods.
You also need to check to make sure the user did actually choose a filename. The filename returned will always be the full path of the filename --there is no need to add '.txt' to it:
If you only want to save, say, 250 lines at a time, you can do something like:
Did you really want the caption saved at the beginning of each line?
Hope this helps.
You also need to check to make sure the user did actually choose a filename. The filename returned will always be the full path of the filename --there is no need to add '.txt' to it:
Delphi Syntax (Toggle Plain Text)
procedure TForm1.Button12Click(Sender: TObject); begin if savedialog1.Execute() then ListBox.items.saveToFile(saveDialog1.fileName) end;
Delphi Syntax (Toggle Plain Text)
procedure save250( filename: string; var strs: tStrings; startAt: integer ); var sl: tStringList; b, e: integer; begin // Our temporary string list sl := tStringList.create; // indices of lines to copy, inclusive b := startAt; e := startAt + 249; if e >= strs.count then e := strs.count -1; // copy the indexed lines into our temporary string list for b := b to e do sl.append( strs[ b ] ); // save the lines to file sl.saveToFile( filename ); // cleanup sl.free end;
Hope this helps.
•
•
Join Date: Nov 2007
Posts: 87
Reputation:
Rep Power: 2
Solved Threads: 1
thank you very much for your quick reply.... 
I am wanting to be able to say in an editbox the number of lines to save.. not just one number or the other. When I click the button that says to perform the procedure, I want the action to be:
1. count from the beginning of the original large list (aka text file) to whatever number is typed in the editbox.
2. save the files that are split into whatever name is chosen by the user named one after the other incrementally. ex: mytextfile1.txt, mytextfile2.txt, etc... until the entire list is split and saved in the desired directory. (mytextfile would be what the user specified in another editbox to give the file a base name)
so, enter how many lines you want to save to each text file in the edit6.text box. use that number to then save that amount of lines to each text file until the list that WAS one huge list, will now be smaller lists incrementally adding until is is complete.
Again, thank you very much for your help
I hope that I have made this a bit clearer. if not, I apologize.

I am wanting to be able to say in an editbox the number of lines to save.. not just one number or the other. When I click the button that says to perform the procedure, I want the action to be:
1. count from the beginning of the original large list (aka text file) to whatever number is typed in the editbox.
2. save the files that are split into whatever name is chosen by the user named one after the other incrementally. ex: mytextfile1.txt, mytextfile2.txt, etc... until the entire list is split and saved in the desired directory. (mytextfile would be what the user specified in another editbox to give the file a base name)
so, enter how many lines you want to save to each text file in the edit6.text box. use that number to then save that amount of lines to each text file until the list that WAS one huge list, will now be smaller lists incrementally adding until is is complete.
Again, thank you very much for your help
I hope that I have made this a bit clearer. if not, I apologize. •
•
Join Date: Oct 2007
Location: Cherry Hill, NJ
Posts: 1,878
Reputation:
Rep Power: 13
Solved Threads: 193
Ah, that helps. You need to be more careful about reading the documentation when using various procedures (line writeln).
For what you want to do, the save250 procedure can almost do it. Just add another parameter for the number of lines to save and adjust line 10 to use the correct value.
Then you can save each n lines in a loop.
I used the name save250 just because that is what I gave you before, but I imagine you will want to rename it. Also, you can make it a private member of the TForm if you like...
Hope this helps.
For what you want to do, the save250 procedure can almost do it. Just add another parameter for the number of lines to save and adjust line 10 to use the correct value.
Then you can save each n lines in a loop.
Delphi Syntax (Toggle Plain Text)
procedure TForm1.Button12Click( Sender: TObject ); var lines_per_block: integer; line_index: integer; file_number: integer; file_extension: string; base_filename: string; begin lines_per_block := strToInt( edit6.text ); // number of lines to save per file if saveDialog1.execute then begin // get the file extension and everything except the extension in // separate strings so we can inject the 1, 2, 3, ... file number. file_extension := extractFileExt( saveDialog1.filename ); base_filename := copy( saveDialog1.filename, 1, length( saveDialog1.filename ) -length( file_extension ) ); // save each block of lines line_index := 0; file_number := 1; while line_index < ListBox.items.count do begin save250( base_filename + intToStr( file_number ) + file_extension, ListBox.items, line_index, lines_per_block ); inc( file_number ); inc( line_index, lines_per_block ) end end end;
Hope this helps.
•
•
Join Date: Nov 2007
Posts: 87
Reputation:
Rep Power: 2
Solved Threads: 1
I added the code exactly as you put it in there...
I made some changes from listbox to listview, and defined the procedure up top.
procedure save250(sender: TObject);
procedure TForm1.Button12Click( Sender: TObject );
var
lines_per_block: integer;
line_index: integer;
file_number: integer;
file_extension: string;
base_filename: string;
begin
lines_per_block := strToInt( edit6.text ); // number of lines to save per file
if saveDialog1.execute then
begin
// get the file extension and everything except the extension in
// separate strings so we can inject the 1, 2, 3, ... file number.
file_extension := extractFileExt( saveDialog1.filename );
base_filename := copy( saveDialog1.filename,1,length( saveDialog1.filename ) -length( file_extension ));
// save each block of lines
line_index := 0;
file_number := 1;
while line_index < ListView1.items.count do
begin
save250(base_filename + intToStr( file_number ) + file_extension,
ListView1.items, line_index, lines_per_block);
inc( file_number );
inc( line_index, lines_per_block );
end;
end;
end;
/And here are the errors I am receiving when I try to compile.
[DCC Error] : E2065 Unsatisfied forward or external declaration: 'TForm1.save250'
[DCC Error] : E2034 Too many actual parameters
[DCC Error] : E2010 Incompatible types: 'TObject' and 'string'
thanks again
I made some changes from listbox to listview, and defined the procedure up top.
procedure save250(sender: TObject);
procedure TForm1.Button12Click( Sender: TObject );
var
lines_per_block: integer;
line_index: integer;
file_number: integer;
file_extension: string;
base_filename: string;
begin
lines_per_block := strToInt( edit6.text ); // number of lines to save per file
if saveDialog1.execute then
begin
// get the file extension and everything except the extension in
// separate strings so we can inject the 1, 2, 3, ... file number.
file_extension := extractFileExt( saveDialog1.filename );
base_filename := copy( saveDialog1.filename,1,length( saveDialog1.filename ) -length( file_extension ));
// save each block of lines
line_index := 0;
file_number := 1;
while line_index < ListView1.items.count do
begin
save250(base_filename + intToStr( file_number ) + file_extension,
ListView1.items, line_index, lines_per_block);
inc( file_number );
inc( line_index, lines_per_block );
end;
end;
end;
/And here are the errors I am receiving when I try to compile.
[DCC Error] : E2065 Unsatisfied forward or external declaration: 'TForm1.save250'
[DCC Error] : E2034 Too many actual parameters
[DCC Error] : E2010 Incompatible types: 'TObject' and 'string'
thanks again
Last edited by squidd : Nov 7th, 2007 at 4:27 pm.
•
•
Join Date: Oct 2007
Location: Cherry Hill, NJ
Posts: 1,878
Reputation:
Rep Power: 13
Solved Threads: 193
You didn't define save250 the way I gave it to you. The one you defined takes a single argument: a TObject. The one I gave you takes entirely different arguments.
The procedure header should look like this:
Please notice how I renamed the procedure from save250 to saveNLines.
Hope this helps.
The procedure header should look like this:
procedure SaveNLines( filename: string; var strs: tStrings; startAt, numLines: integer );Please notice how I renamed the procedure from save250 to saveNLines.
Hope this helps.
Last edited by Duoas : Nov 7th, 2007 at 4:38 pm.
•
•
Join Date: Nov 2007
Posts: 87
Reputation:
Rep Power: 2
Solved Threads: 1
Here is what I have now....
Procedure Header:
procedure SaveNLines( filename: string; var strs: tStrings; startAt, numLines: integer );
Code For Button12:
procedure TForm1.Button12Click(Sender: TObject);
var
lines_per_block: integer;
line_index: integer;
file_number: integer;
file_extension: string;
base_filename: string;
begin
lines_per_block := StrToInt( edit6.text ); // number of lines to save per file
if saveDialog1.execute then
begin
// get the file extension and everything except the extension in
// separate strings so we can inject the 1, 2, 3, ... file number.
file_extension := extractFileExt( saveDialog1.filename );
base_filename := copy( saveDialog1.filename,1,length( saveDialog1.filename ) -length( file_extension ));
// save each block of lines
line_index := 0;
file_number := 1;
while line_index < ListView1.items.count do
begin
SaveNLines(base_filename + intToStr( file_number ) + file_extension,
ListView1.items, line_index, lines_per_block);
inc( file_number );
inc( line_index, lines_per_block );
end;
end;
end;
And the errors that inevitably ensue when I attempt to compile:
[DCC Error] : E2065 Unsatisfied forward or external declaration: 'TForm1.SaveNLines'
[DCC Error] : E2033 Types of actual and formal var parameters must be identical
I simply have no idea why this is happening... I really appreciate your patience and help on this matter regardless of whether or not I can get this work for me.
Procedure Header:
procedure SaveNLines( filename: string; var strs: tStrings; startAt, numLines: integer );
Code For Button12:
procedure TForm1.Button12Click(Sender: TObject);
var
lines_per_block: integer;
line_index: integer;
file_number: integer;
file_extension: string;
base_filename: string;
begin
lines_per_block := StrToInt( edit6.text ); // number of lines to save per file
if saveDialog1.execute then
begin
// get the file extension and everything except the extension in
// separate strings so we can inject the 1, 2, 3, ... file number.
file_extension := extractFileExt( saveDialog1.filename );
base_filename := copy( saveDialog1.filename,1,length( saveDialog1.filename ) -length( file_extension ));
// save each block of lines
line_index := 0;
file_number := 1;
while line_index < ListView1.items.count do
begin
SaveNLines(base_filename + intToStr( file_number ) + file_extension,
ListView1.items, line_index, lines_per_block);
inc( file_number );
inc( line_index, lines_per_block );
end;
end;
end;
And the errors that inevitably ensue when I attempt to compile:
[DCC Error] : E2065 Unsatisfied forward or external declaration: 'TForm1.SaveNLines'
[DCC Error] : E2033 Types of actual and formal var parameters must be identical
I simply have no idea why this is happening... I really appreciate your patience and help on this matter regardless of whether or not I can get this work for me.
![]() |
•
•
•
•
•
•
•
•
DaniWeb Pascal and Delphi Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Similar Threads
- printing of sort list (C++)
- Recrusive Split on a linked list (C)
- Something is very very strange about my IE. Perhaps it's a spyware...Please help me!! (Viruses, Spyware and other Nasties)
- Taskmgn.exe adware? (Viruses, Spyware and other Nasties)
- PC crashes (Viruses, Spyware and other Nasties)
- Spyware is Redirecting Homepage (Viruses, Spyware and other Nasties)
- IE6 has been constantly hijacked by .... (Viruses, Spyware and other Nasties)
- Microsoft IE Offline Pop-ups (Web Browsers)
Other Threads in the Pascal and Delphi Forum
- Previous Thread: Disassembly pane, CPU Window
- Next Thread: Package GXOUTLOOK



Hybrid Mode