By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,541 Members | 1,427 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,541 IT Pros & Developers. It's quick & easy.

reading/writing records from/to a file

P: 2
Hi,

I want to write a perl code that does these operations:

1- it gets data from keyboard and save it in a structure like this:
struct BookData
{
char bookTitle[51];
char isbn [14];
char author[31];
char publisher[31];
char dateAdded[11];
int qtyOnHand ;
float wholesaleprice ;
float retailprice ;
};
2- then it saves the data structure in a file. let's call it: "book.Dat"
3- it then retreives "book.Dat" to prepare some reports (number of books on hand, sorted list of book names ...)

how do I do this in perl? Thank you.
Aug 14 '08 #1
Share this Question
Share on Google+
2 Replies


numberwhun
Expert Mod 2.5K+
P: 3,503
First, even though you have worded this somewhat non-discretely, it sounds peculiarly like something they would ask you to do in school.

So, what have you tried thus far to do this? Do you have any code to show so far so we can see how far you have gotten with this?

I can think of a couple of ways to do this. One involves a file, as you mentioned, maybe a csv file, the other is to use a database instead of a file and incorporate DBI into the script. That way you can retrieve the information with code instead of having to open the file each time.

How much Perl do you know? Also, is that "struct" you showed in your posting from C? Just wondering if this was a conversion from C to Perl, that's all.

Also, when including code in the forums, please use code tags!


Regards,

Jeff
Aug 14 '08 #2

P: 2
Hi Jeff,

Thanks for your reply. I know C/C++ at intermediate level and I just started learning perl. that is why I used struct example. in C, I define a struct( a structure), read data from keyboard to it and I save it in a file. then I can open the file, get any record I want and get my reports. for example: I sort the data based on booktitle, or quantity on hand, and so on.I want to do the same thing in perl.
what I have written so far is a perl program that gets Book information such as: Book title, ISBN, Author, quantity on hand and wholesale price. it then puts the information in a hash. then user can see a list of available books in data base.

if you run this program you see it is working fine, but when you uncomment those lines related to tie my hash to data base "book.dbm" you will see that program can not provide a list of records in database to user. do u know what the problem is or shall I use a totally different approach? Thank you.
here is the perl code:

Expand|Select|Wrap|Line Numbers
  1. #!/usr/local/bin/perl
  2. use warnings;
  3. #use strict;   # turn off for anonymous reference
  4. use Data::Dumper;
  5. use POSIX;
  6. use SDBM_File;           # or GDBM_File / NDBM_File / AnyDBM_File...
  7.  
  8. sub main_menu();
  9. sub list();
  10. sub add();
  11.  
  12. #my %addressbook;
  13. #my $db_file = "bookinfo.dbm";
  14.  
  15. #tie %addressbook, 'SDBM_File', $db_file, O_CREAT|O_RDWR, 0644;
  16.  
  17. #if (tied %addressbook)
  18. #{
  19. #       print "File $db_file now open.\n";
  20. #}
  21. #else
  22. #{
  23. #    die "Sorry - unable to open $db_file\n";
  24. #};
  25.  
  26. while (1)
  27. {
  28.         main_menu();
  29.         print "\nPlease Enter Your Choice:";
  30.         chomp($_ = <STDIN>);
  31.         if    ($_ eq "2") { list() }
  32.         elsif ($_ eq "3") { list()   }
  33.         elsif ($_ eq "1") { add()   }
  34.         elsif ($_ eq "4") { print "\nGood Bye!"; exit ;  }
  35.         else { print "Sorry, not a recognized option.\n"; }
  36. }
  37. #untie %addressbook;
  38.  
  39. #******** sub add()  *******************************
  40. sub add()
  41. {
  42.         print "\nPlease Enter Following information: \n\n";
  43.         print  "\nBook Title:"; chomp( $title = <STDIN>);
  44.         print "\nISBN number:";chomp( $isbn = <STDIN>);
  45.         print "\nAuthor's Name:"; chomp( $author = <STDIN>);
  46.         print "\nThe quantity of this book being added:";chomp( $qty = <STDIN>);
  47.         print "\nThe wholesale cost of this book:"; chomp( $whole = <STDIN>);
  48.  
  49.          $addressbook{$isbn} = {
  50.          title =>$title  ,
  51.          author=>$author ,
  52.          qtyOnHand => $qty ,
  53.          wholesale => $whole
  54.          };
  55.         print "\nHere is AddressBook so far:" ;
  56.         print Dumper $addressbook{$isbn} ;
  57.  
  58.         return;
  59. }
  60.  
  61. #******** sub list  *******************************
  62. sub list()
  63. {
  64. #print Dumper \%addressbook ;
  65.  foreach (sort keys(%addressbook))
  66.   {
  67.         print "\nISBN: $_ ";
  68.         print "\nBook Title:  ", $addressbook{$_}->{title};
  69.         print "\nAuthor:  ", $addressbook{$_}->{author};
  70.         print "\nQuantity on Hand:  ", $addressbook{$_}->{qtyOnHand};
  71.         print "\nWhole Sale Price:  ", $addressbook{$_}->{wholesale},"\n";
  72.   }
  73.  return;
  74. }
  75.  
  76. #******** sub main_menu   *******************************
  77. sub main_menu()
  78. {
  79.  print<<EOF;
  80.  
  81.           Bookinfo- Perl Version 2.0
  82.  
  83.                     Main Menu:
  84.  
  85.     1- add record to data base
  86.     2- list records in data base
  87.                 3- list records in data base
  88.                 4- Exit
  89. EOF
  90. }
  91.  
  92.  



First, even though you have worded this somewhat non-discretely, it sounds peculiarly like something they would ask you to do in school.

So, what have you tried thus far to do this? Do you have any code to show so far so we can see how far you have gotten with this?

I can think of a couple of ways to do this. One involves a file, as you mentioned, maybe a csv file, the other is to use a database instead of a file and incorporate DBI into the script. That way you can retrieve the information with code instead of having to open the file each time.

How much Perl do you know? Also, is that "struct" you showed in your posting from C? Just wondering if this was a conversion from C to Perl, that's all.

Also, when including code in the forums, please use code tags!


Regards,

Jeff
Aug 14 '08 #3

Post your reply

Sign in to post your reply or Sign up for a free account.