Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reading and writing to files

Status
Not open for further replies.

clsyorkshire

Programmer
Feb 10, 2004
4
GB
Hi, I'm making a program where items of stock can be added to a data file, and then I need another program that will display all the data in the file. I've started making the program to read the data but my problem is it only allows storage for one set of data. Heres what I have so far (below), any help would be appreciated:



Program one;
Uses crt;
TYPE
recs=record
code:string[8]; {number in square brackets = number of characters}
itemname:string[30];
costprice:real;
saleprice:real;
end;
VAR
stockfile:file of recs;
temp:recs;
filename:string[12];
answer:char;
BEGIN
clrscr; {clears the screen}
gotoxy(5,2);
textcolor(white);
writeln('Please enter the name of the file to create : ');
textcolor(yellow);
gotoxy(51,2);
readln(filename);
assign(stockfile,filename);
rewrite(stockfile);
REPEAT
textcolor(white);
gotoxy(22,5);
writeln('Please enter the item code : ');
textcolor(yellow);
gotoxy(51,5);
readln(temp.code);
textcolor(white);
gotoxy(22,8);
writeln('Please enter the item name : ');
textcolor(yellow);
gotoxy(51,8);
readln(temp.itemname);
textcolor(white);
gotoxy(21,11);
writeln('Please enter the cost price : ');
textcolor(yellow);
gotoxy(51,11);
readln(temp.costprice);
textcolor(white);
gotoxy(22,14);
writeln('and finally the sale price : ');
textcolor(yellow);
gotoxy(51,14);
readln(temp.saleprice);
write(stockfile,temp);
textcolor(cyan);
gotoxy(20,20);
writeln('Do you want to enter another?');
gotoxy(51,20);
textcolor(yellow);
readln(answer);
clrscr;
UNTIL upcase(answer)='N';
Close(stockfile);
END.
 
You can't use the [tt]append[/tt] procedure on binary files (they can be opened in read/write mode, so there's no need).

When you open an existing binary file, you should use [tt]reset[/tt] ([tt]rewrite[/tt] clears an existing file and makes a new one). To write to the end of a binary file, use the [tt]seek[/tt] procedure.
Example:
Code:
assign(f,'TEST.TXT'); // link file variable with filename
reset(f); // open file in read/write mode
seek(f,filesize(f)-1); // position file pointer past the last record

When referring to records in a file, know that the first record has index 0, hence the [tt]filesize(f)-1[/tt].

Regards,
Bert Vingerhoets
vingerhoetsbert@hotmail.com
Don't worry what people think about you. They're too busy wondering what you think about them.
 
The problem is with reset, you can only open a file that's already there. Clearly youre new to Pascal, most likely to programming too?

Any way, you dont need two programs, you need three (minimum) procedures. A procedure is a self contained set of instructions that as a whole, performs a task such as creating a file, appending a file or reading a file. Call them "mini programs" if you will. This is modular programming.

So you already have your Create a file procedure, it just needs a slight tweak. You also have your Update a file procedure thanks to BertV100's post (yes it's identical to what you have but with one word difference). And you'll be adding a read a file procedure in class no doubt.

So lets write the procedure for updating a file:

start by declaring the procedure and its variables as you would the program. Then you need to begin reading instructions, implementing the code, then stop the whole procedure.

PROCEDURE UpdateStockFile;
{Adds a record to the end of the stock file}

BEGIN
reset(stockfile);
REPEAT
writeln('Please enter the item code : ');
readln(temp.code);
writeln('Please enter the item name : ');
readln(temp.itemname);
writeln('Please enter the cost price : ');
readln(temp.costprice);
writeln('and finally the sale price : ');
readln(temp.saleprice);
write(stockfile,temp);
writeln('Do you want to enter another?');
readln(answer);
clrscr;
UNTIL upcase(answer)='N';
Close(stockfile);
END;

Make sure you end a procedure with a semi-colon NOT a full stop. I didn't declare any variables as I assume you don't know how to pass by reference yet (yuor teacher can tell you this) so you can work with golbal variables for now.

Seeing as ive given you a procedure, i might as well tell you how to use it in your program:

PROGRAM StockCatalogue;

Uses crt;

TYPE
recs = record
code : string[8];
itemname : string[30];
costprice : real;
saleprice : real;
END;

VAR
stockfile : FILE OF recs;
temp : recs;
filename : string[12];
answer : char;

PROCEDURE readfile;
BEGIN
{add your readfile code here}
END;

PROCEDURE updatefile;
BEGIN
{copy above code here}
END;

PROCEDURE createfile;
BEGIN
{your create a file code here}
END;

{------ MAIN PROGRAM ------}

BEGIN
createfile; {jumps to the create file procedure}
END.


Okay. I might as well go the whole 9 yards with this one. All key words (the ones in white if you use TP7) should be written in captials. The only one i never capitalise is "string." Also get your program to work before playing with color and positioning. gotoxy may look fancy but its just ass ugly for other ppl to look at especially when youre asking for help.

I'd be asking "how do i know wether to create or update a file?" I guess you really dont need to know that now or you'd show some signs of knowing in your code. just play around, i might suggest menus for your skill perhaps.

I know this helps, hope it doesnt confuse

~*Gwar3k1*~
"To the pressure, everything's just like: an illusion. I'll be losing you before long..."
 
Some remarks:
- This is not modular programming. It's procedural or structured programming. You get modular programming when you use mudules (duh). A module is a black box with an interface. The user only needs to know the interface, the programmer of the module only needs to know what it should do to provide an implementation. An important keyword in modular programming is "data-hiding". An example of a module in TP would be a unit.
- I strongly advise against using capitals in your code. Since TP is not case sensitive, it doesn't matter anyhow, so use small letters. Capitalized letters are difficult to read. I suggest capitalizing only keywords like [tt]PROCEDURE, FUNCTION,[/tt] main [tt]BEGIN[/tt] and [tt]END, PROGRAM, USES, VAR, CONST, TYPE, UNIT, INTERFACE[/tt] and [tt]IMPLEMENTATION.[/tt]

Regards,
Bert Vingerhoets
vingerhoetsbert@hotmail.com
Don't worry what people think about you. They're too busy wondering what you think about them.
 
Sorry, yeah makes more sence procedural and I agree with the keywords capitalisation (see above post) =) I thought maybe you might have told me off for giving so much code *phew*

~*Gwar3k1*~
"To the pressure, everything's just like: an illusion. I'll be losing you before long..."
 
It is quite a lot, but since it's there ...
It's also clear you were giving a practical solution. If you would have responded like that to a core aspect of the problem, I would have red flagged it. Questions that are too general or vague deserve an equally general and vague answer. If, however, someone has a specific difficulty, the first answer he deserves is one that points him in the general direction, if relevant accompanied with a small code example that is not necessarily directly related to the goal he wants to achieve. If the problem remains, you can release more and more information piecewise (as long as the asking guy keeps responding).
When it is clear from the context of the given explanations and/or code that the asker has some experience and is indeed actively working on a solution to his problem, I have no trouble giving away the precise solution he wants; since his problem clearly results from some language feature.
I've had, for example, a question about someone asking me about a way of getting a result displayed in an evaluation loop. I didn't annoy him with giving vague directions but gave a full solution with relevant code example immediately. Why? Because if you can write (or even understand) a meta-circular evaluator, displaying something on screen clearly is trivial, and not knowing how to do it is merely irritating and has nothing to do with the goal to achieve (this was in Scheme BTW, meta circular evaluators in Pascal are not so straightforward).

Pascal typically is a language in which people learn programming. Learning to program is not, unlike many people might think, learing aspects of a programming language and how to put statements together. It is about learning to solve problems by dividing a large problem into smaller problems. A better name for first courses of "computer science", "programming", "informatics" or whatever they're called would better be named "computer aided problem solving". This because a problem is solved on paper, by thinking about it, the actual programming is trivial. I'm not saying the programming part is not important, but when you can solve problems, you only need to know a bit of syntax to reach your goal. Once you can program in one language, you can work with any language located within the same paradigm, wether it's structured, object oriented or something else.

Like Harold Abelson (or was it Gerald Sussman?) says in his first lecture: "Computer science is not much of a science, and it's not really about computers either" (
Regards,
Bert Vingerhoets
vingerhoetsbert@hotmail.com
Don't worry what people think about you. They're too busy wondering what you think about them.
 
BertV
Its my opinion that maybe your last post should be in some form of Code of conduct faq for the Pascal in the workplace board. =)

~*Gwar3k1*~
"To the pressure, everything's just like: an illusion. I'll be losing you before long..."
 
Thanks for the help, I'm still stuck but I appreciate the help I've got here.
 
I appreciate you telling us that, but which problems do you still have?

Regards,
Bert Vingerhoets
vingerhoetsbert@hotmail.com
Don't worry what people think about you. They're too busy wondering what you think about them.
 
the last part of the program should be a way of inputting a stock code and the details for that item of stock need to appear
 
In that case you should add a stock_code entry in the record and expand your user interaction with a writeln and a readln, analogous to what you have now.

Regards,
Bert Vingerhoets
vingerhoetsbert@hotmail.com
Don't worry what people think about you. They're too busy wondering what you think about them.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top