[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

Creation of, reading and writing from text files.

Posted on 1999-12-06
14
Medium Priority
?
193 Views
Last Modified: 2010-04-16
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.
0
Comment
Question by:genpod
  • 7
  • 4
  • 2
  • +1
14 Comments
 
LVL 10

Accepted Solution

by:
viktornet earned 200 total points
ID: 2260504
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
 
LVL 32

Expert Comment

by:Batalf
ID: 2261049
Or you can declare the f as a textfile.

var
   f : textfile;

0
 
LVL 3

Expert Comment

by:vikiing
ID: 2261461
>>rewrite(1);

The correct statement would be ReWrite(f);
0
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

 
LVL 32

Expert Comment

by:Batalf
ID: 2261529
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
 
LVL 32

Expert Comment

by:Batalf
ID: 2267728
Viktornets answer is kind of sloppy
0
 
LVL 10

Expert Comment

by:viktornet
ID: 2270180
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
 
LVL 32

Expert Comment

by:Batalf
ID: 2271002
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
 
LVL 32

Expert Comment

by:Batalf
ID: 2271012
As far as I know you should use
reset(f) instead of rewrite(f)
when you want to read from a file.
0
 
LVL 3

Expert Comment

by:vikiing
ID: 2271239
>>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
 
LVL 27

Expert Comment

by:BigRat
ID: 2274007
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
 
LVL 32

Expert Comment

by:Batalf
ID: 2274086
You're right about textfiles BigRat. Sometimes I have problems to differ between Delphi and Pascal :-)
0
 
LVL 10

Expert Comment

by:viktornet
ID: 2278738
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
 
LVL 32

Expert Comment

by:Batalf
ID: 2279140
To viktornet
Why Propose an answer if you don't answer everything that's been asked for????
0
 
LVL 10

Expert Comment

by:viktornet
ID: 2279157
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

Featured Post

[Webinar] Kill tickets & tabs using PowerShell

Are you tired of cycling through the same browser tabs everyday to close the same repetitive tickets? In this webinar JumpCloud will show how you can leverage RESTful APIs to build your own PowerShell modules to kill tickets & tabs using the PowerShell command Invoke-RestMethod.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Virtualization software lets you run different versions of Windows, Ubuntu Linux and other versions of Linux all at the same time, rather than running each one directly from your computer's hard drive.
A question that many companies need to answer until May 25th of 2018... Is your company ready for GDPR?
SQL Database Recovery Software repairs the MDF & NDF Files, corrupted due to hardware related issues or software related errors. Provides preview of recovered database objects and allows saving in either MSSQL, CSV, HTML or XLS format. Ensures recov…
Stellar Phoenix SQL Database Repair software easily fixes the suspect mode issue of SQL Server database. It is a simple process to bring the database from suspect mode to normal mode. Check out the video and fix the SQL database suspect mode problem.

607 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question