Board index » delphi » several problems transfering/reading textfiles

several problems transfering/reading textfiles

Hello,
  I am having some difficulty with this section of my program for school

 what I am trying to do is with procedure number_of_lines I have to make
the
 text file that the user enters and copy it to a new file. I have no
idea
 how to do this. My second problem is in function count_characters. I
have
 to count all the characters that are in the textfile. I have tried many

 different things and I can't figure it out. Here is my code...

program external_file_work(input,output);

uses
  crt,strings;
var
  choice:integer;
  exfile:text;
  filename:string;

{**********************************************
 * This procedure provides a menu for the     *
 * user to use. They have seven choices, which*
 * one is to exit.This program will be execut *
 * ed as many times as the user wants.        *
 **********************************************}
procedure menu(var choice1:integer);

begin
  writeln;
  writeln('Menu:  1. Display the contents of the file');
  writeln('2. Number the lines of the file':38);
  writeln('3. Count the characters in the file':41);
  writeln('4. Count the words in the file':37);
  writeln('5. Replace each occurrence of one character with another
character':73);
  writeln('6. Enter a new file name':31);
  writeln('7. Exit the program':26);
  writeln;
  write('Pick what you would like to have done ---> ');
  readln(choice);
  writeln;
end;

{}
procedure display_file(filename1:string);
var
  line:array[0..99]of char;
begin
  assign(exfile,filename1);
  reset(exfile);
  while not eof(exfile) do
    begin
      readln(exfile,line);
      writeln(line);
    end;
end;

{}
procedure number_of_lines(filename2:string);

var
  numberlines:array[0..99]of char;
  count:integer;
  newfile:string;
begin
  assign(exfile,filename2);
  reset(exfile);
  while not eof(exfile) do
    begin
      count:=0;
      writeln;
        while not eoln (exfile) do
          begin
            count:=count+1;
            readln(exfile,numberlines);
            write(count);
            writeln(' ',numberlines);
          end;
    end;
end;

{}
function count_characters(filename3:string):char;
var
  characters,c1:array[0..99]of char;
  length,length1,x,index:integer;
begin
  assign(exfile,filename3);
  reset(exfile);
  while not eof(exfile) do
    begin
        readln(exfile,characters);
        length:=strlen(characters);
        length:=length-1;
        for x:=length-1 downto 0 do
          characters[length]:=characters[x];
    end;
  writeln;
  writeln('There are ',characters[length],' characters in
',filename3,'.');
end;

begin{of main}
  clrscr;
  write('Enter the full file name that you wish to use ---> ');
  readln(filename);
  writeln(filename);
  choice:=0;
  menu(choice);
  while choice<>7 do
    begin
      display_file(filename);
      number_of_lines(filename);
      count_characters(filename);
      menu(choice)
    end;
readln;
end.

  If any one can give me any clues of what to do I would gladly
  appreciate it. One more question I have, how can you read a word
  (one at a time) from a textfile. Thank you very much.

 sincerely,
   M.G.

-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----==  Over 80,000 Newsgroups - 16 Different Servers! =-----

 

Re:several problems transfering/reading textfiles


Hi,

on Sat, 22 Apr 2000 at 02:31:12 o'clock, Renee Grothe wrote:

Quote
>  what I am trying to do is with procedure number_of_lines I have to make
> the
>  text file that the user enters and copy it to a new file.

Why? As far as I can see, the procedure is supposed to print the
number of lines in a file, so why do you think you have to make a
copy? Instead, the algrorithm should look something like this:

   var s : String; count : Integer;
   :
   Reset(extfile);
   while not eof(extfile) do
   begin
      Readln(extfile, s);  { Dont care for the value of s }
      Inc(count);
   end;
   Close(extfile);
   Writeln('No. of lines: ', count);

Quote
>  My second problem is in function count_characters. I
> have
>  to count all the characters that are in the textfile.

Similar. Just count the number of characters in each line - the Length
function (see online help) will be a great aid. However, Note that
with this solution, you will get erroneous results if the file has
lines longer than 255 characters. Use of file of char (and possibly
BlockRead) to overcome this limitation.

 - Sebastian

Other Threads