Solved

reading in and out a program

Posted on 2000-02-22
11
179 Views
Last Modified: 2010-04-16
Hi,

yes me once again. I'm having trouble having the data input saved on the file and having it output later on.

type MemberPointer = ^MEMBERELEMENT;
         MEMBERELEMENT= record
                            NEXTMEMBER:MEMBERPOINTER;
                            NAME: string;
                            DATEOFBIRTH: string;
                            SEX: char;
                            ADDRESS: string;
                            PHONE: string;    //use a string so that you can manipulate the numbers//
                            CCARD: string;
                        end;


var
ENTRYINMEMBERLIST, CURRENTMEMBER, NEXTMEMBER: MEMBERPOINTER;
MTEXTFILE: text;  //memberfile.txt//
MEMBERRESPONSE, RESPONSE, KEYPRESS,  ANSWER:char;
FOUND:boolean;


procedure Query;      //to know if user wants to add another file used by mem and vid//
begin
        writeln('Any more ?');
        readln(RESPONSE);
end;


procedure GetMemberInput;             //input the members information//
begin
     open(MTEXTFILE, 'memberfile.txt');
     new(ENTRYINMEMBERLIST);
     CURRENTMEMBER := ENTRYINMEMBERLIST;
     //enter a new member//
     writeln('Enter the member''s name');
     readln(CURRENTMEMBER^.NAME);
     writeln(MTEXTFILE,CURRENTMEMBER^.NAME);
     //
     writeln('Enter the member''s date of birth');
     readln(CURRENTMEMBER^.DATEOFBIRTH);
     writeln(MTEXTFILE,CURRENTMEMBER^.DATEOFBIRTH);
     //
     CURRENTMEMBER^.NEXTMEMBER:=nil;
     Query;
     while RESPONSE = 'y' do
          begin
               new(CURRENTMEMBER^.NEXTMEMBER);
               //enter an additional member//
               writeln('Enter the member''s name');
               readln(CURRENTMEMBER^.NEXTMEMBER^.NAME);
               writeln(MTEXTFILE,CURRENTMEMBER^.NEXTMEMBER^.NAME);
               //
               writeln('Enter the member''s date of birth');
               readln(CURRENTMEMBER^.NEXTMEMBER^.DATEOFBIRTH);
               writeln(MTEXTFILE,CURRENTMEMBER^.NEXTMEMBER^.DATEOFBIRTH);
               //
               CURRENTMEMBER^.NEXTMEMBER:=nil;
               writeln('Any more?');
               readln(RESPONSE);
          end;
end;




Procedure OutputEntireMemberList;
begin
     open(MTEXTFILE, 'memberfile.txt');
     writeln('List of all of the members');           //outputs all of the members details on file//
     CURRENTMEMBER:=ENTRYINMEMBERLIST;
     while CURRENTMEMBER<>nil do
          begin
               open(MTEXTFILE, 'memberfile.txt');
               readln(MTEXTFILE,CURRENTMEMBER^.NAME);
               writeln('Name: ',CURRENTMEMBER^.NAME);
               //
               readln(MTEXTFILE,CURRENTMEMBER^.DATEOFBIRTH);
               writeln('DOB: ',CURRENTMEMBER^.DATEOFBIRTH);
               //
               CURRENTMEMBER:=CURRENTMEMBER^.NEXTMEMBER;
            end;
      writeln;
end;


~~
when i get to the output, it doesnt output anything. It gives a whole bunch of spaces. ANy idea how to fix?

Cheers.
0
Comment
Question by:yaelie
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
11 Comments
 
LVL 1

Expert Comment

by:nrico
ID: 2546439
Did you check the linked list actually *contains* the values you want to output? (The problem may lie somewhere else than you think)
0
 
LVL 1

Expert Comment

by:nrico
ID: 2546458
You don't actually _add_ to the linked list. You are missing a CURRENTMEMBER:=CURRENTMEMBER^.NEXTMEMBER; statement at the end of your input loop.
0
 
LVL 1

Expert Comment

by:nrico
ID: 2546464
Actually, I would place the statement just after the New(CURRENTMEMBER^.NEXTMEMBER); so you don't have to type so many letters ;-).
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 48

Expert Comment

by:dbrunton
ID: 2549462
Procedure OutputEntireMemberList;
var
  TheMember : MEMBEREELEMENT     // don't need a pointer
begin
  open(MTEXTFILE, 'memberfile.txt');
  writeln('List of all of the members');           //outputs all of the members details on file//

  while not eof(MTEXTFILE) do
  begin
                      //       open(MTEXTFILE, 'memberfile.txt');   file already open
    read(MTEXTFILE, TheMember);
    writeln('Name: ', TheMember.NAME);  
    writeln('DOB: ', TheMember.DATEOFBIRTH);
  end;
 writeln;
end;


This hasn't been tested yet I am afraid but should work

I have only looked at the output procedure and nothing else so I can't comment on the rest of the code.
0
 
LVL 1

Expert Comment

by:nrico
ID: 2550570
By the way, why are you storing your members in a linked list when you write them directly to disk?
And if you do use them another place in the program, it would make much more sense to make a separate loading/saving routine.

Rico
0
 
LVL 1

Expert Comment

by:nrico
ID: 2550596
Something like this:

Procedure SaveList;
Var
  F:Text;
  P:MEMBERPOINTER;
Begin
  Assign(F,'memberfile.txt');
{ This won't work by the way 'cuz the filename is longer than 8 characters }
  Rewrite(F);
  P:=ENTRYINMEMBERLIST;
  While (P<>Nil) Do
  Begin
    WriteLn(F,P^.NAME);
    WriteLn(F,P^.DATEOFBIRTH);
    P:=P^.Next;
  End;
  Close(F);
End;

LoadList is a bit more complicated, but I can make it if you want...
0
 
LVL 1

Author Comment

by:yaelie
ID: 2551714
Hi..

sorry I didnt give enough info :)
First off, I'm working on a mac which would explain me not noticing the file name being longer then 8chars.

also.. this is part of a database for a videoshop.
 I have the member's files, video's files (keep info such as video name, video genre, price etc..) and one rental file which basicly connects the two (takes a specific member and keeps the names of the videos he's rented)... thing is, i cant get the basic input output working.


brunton (so great of you to be helping me once again!!) ill try the code you gave me tomorrow..

nrico- does that procedure go between the input and the output? let me know and ill try that too..
0
 
LVL 1

Expert Comment

by:nrico
ID: 2554173
My function should go (at least the SaveFile one, want me to make a LoadFile too?) when you HAVE the information in your linked list, but you HAVEN'T saved it to disk (Or you want to re-save or whatever).

So you need to remove the WriteLn(File,Data); lines from your original input procedure.

The best way for this is to keep things in memory and on disk seperate.
You have a linked list in your memory that you can manipulate (You already know how to do that, don't you?), and when the program finished -- or the user requests it -- the list can be saved to disk.
When the program opens -- or the user requests it -- you load the file into your memory to be manipulated.

Don't be loading/writing directly to the file (in which case, anyway, you don't need a list at all), because it will take away the flexibility of your system.

About the files: It seems that currently, you use text-based files to store your information. It would be better to use a

Var
  F:File of MEMBERELEMENT;

to read and write entire records at once. I'll post some demo code right below...
0
 
LVL 1

Expert Comment

by:nrico
ID: 2554223
(This code will _ONLY_ read and write (and delete) entire lists at once. You'll have to implement finer record manipulation yourself)


type
  PMember=^TMember;
  TMember=Record
    Next:PMember;
    Name:String;
    DateOfBirth:String;
    Sex:Char;
    Address:String;
    Phone:String;
    CreditCard:String;
  End;

Const
  MemberList:PMember=Nil;

Function QueryMore:Boolean;
Var
  Ch:Char;
Begin
  WriteLn('Enter another member? (Y/N)');
  Repeat
    Ch:=UpCase(ReadKey);
  Until Ch In ['Y','N'];
  QueryMore:=Ch='Y';
End;

{ Can only be done on an empty list... }
Procedure GetMemberInput;
Var
  Last,Current:PMember;
Begin
  Last:=Nil;
  While (QueryMore) Do
  Begin
    New(Current);
    Current^.Next:=Nil;
    WriteLn('Enter the membername');
    ReadLn(Current^.Name);
    WriteLn('Enter the member''s birthdate');
    ReadLn(Current^.DateOfBirth);
{ ... anything more you want goes here ... }
    If Last=Nil Then MemberList:=Current Else
    Last^.Next:=Current;
    Last:=Current;
  End;
End;

{ Can only be done on a filled list }
Procedure SaveList;
Var
  F:File of TMember;
  P:PMember;
Begin
  Assign(F,'memberfile.dat');
  Rewrite(F);
  P:=MemberList;
  While (P<>Nil) Do
  Begin
    Write(F,P^); { Write the entire record at once }
    P:=P^.Next;
  End;
  Close(F);
End;

{ Can only be done on an empty list, returns if fileload was succesful }
Function LoadList:Boolean;
Var
  F:File Of TMember;
  Last,Current:PMember;
Begin
  Assign(F,'memberfile.dat');
  {$I-}
  Reset(F);
  {$I+}
  If IoResult<>0 Then
  Begin { File not found... }
    LoadList:=False;
    Exit;
  End;

  Last:=Nil;
  While Not Eof(F) Do
  Begin
    New(Current);
    Current^.Next:=Nil;
    Read(F,Current^);
    If Last=Nil Then MemberList:=Current Else
    Last^.Next:=Current;
    Last:=Current;
  End;
End;

{ Main program body }
Begin
  If LoadList=False Then
  Begin
    WriteLn('No file found. Please enter member information');
    GetMemberInput;
  End;
  WriteLn('Member information retrieved OK');
{ ...Manipulate member information here... }
  WriteLn('Now saving information...');
  SaveList;
End.

{ Comments from here }
This ought to work (give or take a few spelling errors I might have made, I haven't tested this.) If any errors come up please tell me.
These procedures load and save the entire records at once. You'll have to add more flexibility yourself (so your assignment doesn't go to waste completely ;-), I have only showed you how to load/save the lists.
The program tries to open the file memberdata.dat. If it exists, all member records are read from it into memory; If it doesn't, the user is requested to enter records.
Then you can do whatever you want, and as the program exists everything is written to disk.
0
 
LVL 1

Accepted Solution

by:
ntdragon earned 50 total points
ID: 2588065
if i'm not wrong you don't close the file after you open it for write/read
of that maybe the problem
if not i"ll try to help again
0
 
LVL 1

Expert Comment

by:nrico
ID: 2602760
Whoops, you're right. I forgot to add Close(F);

But that doesn't influence the flow of the program, it just eats up resources... :->
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

This article describes a method of delivering Word templates for use in merging Access data to Word documents, that requires no computer knowledge on the part of the recipient -- the templates are saved in table fields, and are extracted and install…
In order to fulfill our mission of inspiring learning in the technology community, Experts Exchange is launching a Course of the Month program. Premium and Team Account members will have access to one course per month as a part of their membership, …

726 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