Link to home
Start Free TrialLog in
Avatar of genpod
genpod

asked on

Creation of, reading and writing from text files.

I was given a problem in my CIS101 class, i am to create a students database, inputing marks for 40 students in 5 subject areas, and writing that information onto a text file located on the A:/, i cannot seem to remember how to create a text file much less write and read form it.
ASKER CERTIFIED SOLUTION
Avatar of viktornet
viktornet
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Or you can declare the f as a textfile.

var
   f : textfile;

Avatar of vikiing
vikiing

>>rewrite(1);

The correct statement would be ReWrite(f);
Here's an example which could help you:

PROGRAM studentprg;
USES
   crt;
TYPE
  Student3 = RECORD
     name : string[20];
     age : integer;
     mark1 : string[20];
     mark2 : string[20];
     mark3 : string[20];
     mark4 : string[20];
     mark5 : string[20];
  end;
  student2 = array[1..40] of student3;
var
   student : student2;
   f : textfile;
Procedure getfromfile;
begin
    assignfilefile(f, 'A:\filename.txt');
    reset(f);
    read(f, student);
    close(f); {or closefile(f)}
end;
procedure writetofile;
begin
    assignfile(f, 'A:\filename.txt');
    rewrite(f);
    write(f, student);
    close(f); {or closefile(f)}
end;

begin
   {programcode. What you like to do. You can make call to the procedures above with

    writetofile;
    getfromfile;

}
end.


regards
Batalf

Viktornets answer is kind of sloppy
what the hell is wrong with my answer? I dont have time on my hands to give full examples. The only thing I messed up was the parameter of the Reset() function and I thank vikiing for correcting me... In fact, I haven't used Pascal in a long time, so... uhmm.. that's about it actually.. i give no sloppy answers... maybe you're new to the site, and like me you give full examples... i'll see where you'll be in a few months when you're sick and tired of giving code examples...!!

later...

..-=ViKtOr=-..
Sorry if my comment above offended Viktornet :-)

The reason why I put in that comment was that you didn't answer everything
genpod asked for(both reading and writing from file, spesific textfiles)

Batalf
As far as I know you should use
reset(f) instead of rewrite(f)
when you want to read from a file.
>>reset(f) instead of rewrite(f)

You're right; both procedures give different results. Reset() looks for a file, and returns an error if file is not found; whereas Rewrite() creates that file EMPTY (this is, it destroys its contents).
Most of the comments are sloppy.
1) The file type of a text file is simply "Text". (That's at least the way it has been for the last twenty years).
2) The Assign just associates the file name with the text file variable. The Reset or Rewrite (which for text files takes only the file variable as a parameter) will attempt to open the file. Some systms require that you switch off I/O errors on the command, eg: {$I-} Reset(f); {$I+}, some don't; BUT after the reset/rewrite one must at least test IoError to see whether the file was actually opened and give the user an error response.
You're right about textfiles BigRat. Sometimes I have problems to differ between Delphi and Pascal :-)
hey hey, come on now... everyone has help files (manuals) that come with their compiler if they need technical information... i'm not gonna sit down and write a novel about using reset() or rewrite9) or whatever... i just give some helpful information which would get you started. I also won't write the whole program for you (not in this case, but in other cases that are happening around E-E) PERIOD....

..-=ViKtOr=-..
To viktornet
Why Propose an answer if you don't answer everything that's been asked for????
Tha main question is CREATING the file... (the other two sub questions are reading and writing)... well, after I propose my answer, if the individual still needs more help in implementing the code, s/he would let me know that s/he needs more help. Now that genpod is not saying a word we don't know whether or not my answer helped enough or not. If it didn't help I would add more code/comments if needed in order to fullfil what i've missed. Now please stop complaining and start answering the rest of the Pascal questions that are waiting!!

..-=ViKtOr=-..