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.
genpodAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

viktornetCommented:
to create a file you do this..

var
  f : file;
begin
  assign(f, 'a:\whatever.txt');
  rewrite(1);
{ now the file is created and you can use write(), writeln(), read(), readln() to read and write stuff to the file }

  close(f);  {close the file}
end.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
BatalfCommented:
Or you can declare the f as a textfile.

var
   f : textfile;

0
vikiingCommented:
>>rewrite(1);

The correct statement would be ReWrite(f);
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

BatalfCommented:
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

0
BatalfCommented:
Viktornets answer is kind of sloppy
0
viktornetCommented:
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=-..
0
BatalfCommented:
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
0
BatalfCommented:
As far as I know you should use
reset(f) instead of rewrite(f)
when you want to read from a file.
0
vikiingCommented:
>>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).
0
BigRatCommented:
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.
0
BatalfCommented:
You're right about textfiles BigRat. Sometimes I have problems to differ between Delphi and Pascal :-)
0
viktornetCommented:
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=-..
0
BatalfCommented:
To viktornet
Why Propose an answer if you don't answer everything that's been asked for????
0
viktornetCommented:
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=-..
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Pascal

From novice to tech pro — start learning today.