Please Help (fread, fwrite) 
Author Message
 Please Help (fread, fwrite)

I'm "trying" to write a program to read and write in Customer Information
from a disk (drive A:)into a structure array called customer_info and two
other
arrays, one for the customer number "cnum" and the subscript array. The cnum
array contains the customer number with the subscript array pointing to the
stucture array. So when you ask for customer number 000001 it will pull up
the customer data from the stucture array. The first thing the program
should do is read drive A (31/4 disk)for any information and if none say
Error opening file . Then the main menu comes up where I should be able to
add, del, list and what ever else is required "see code". When I add
customers, the cnum  array is sorted, with the subscript array pointing to
the data for the structure array. Every time I add a new customer the cnum
is sorted, structure array gets filled one after other and the subscript
array points to the proper sturcture array. For option 6 "save the customer
information", I would just write to Drive A all customer_info (structure
array) , cnum array (sorted), and its subscript array. How do I read from
the disk and store it into the arrays and cnum sorted. I haven't even tried
to write to the disk!! I really need help to get me going on this. I have
include my code for your pickings.

Thanks in advance.

Clinton

#include <stdio.h>
#include <stdlib.h>

struct customer       /* Customer List Data Structure */
{

  char name[21];      /* Customer Name */
  char addr[21];      /* Address */
  char city[11];      /* City */
  char prov[11];      /* Province */
  char pcode[7];      /* Postal Code */

Quote:
}  cust_type;

load_cust();  /*Prototype*/
add_cust();
mark_cust_del();
list_cust_num();
list_specific_cust_number();
list_cust_name();
save_cust();
cust_menu();
quit_main_screen();

const char cust_file[80] = "CUSTOMER.PRO"; /*file to be read in*/

void main(void)
{
  clrscr();
  load_cust(); /*done when first started program */
  cust_menu();

Quote:
}

load_cust()
{
  int size=0;
  int cnum[80];
  int subarr[80];
  FILE * cust_file_ptr = fopen(cust_file,"txt");
  clrscr();

  if (cust_file_ptr == NULL)
  puts("Error opening file");
  else;
  puts("Loading Customer Info. Please wait...");
  fscanf(cust_file_ptr," %d %d %s", cnum[size], subarr[size],
cust_file_ptr[size]);
  size++;
  fclose(cust_file_ptr);
  return;

Quote:
}

cust_menu()
{
  int num;
  clrscr();
  puts("\n\n");
  puts("\t\t\t\t*MAIN MENU*\n\n\n");
  puts("\t\t\t1) Add Customer Info");
  puts("\t\t\t2) Mark Customer for Deletion");
  puts("\t\t\t3) List All Customer by Number");
  puts("\t\t\t4) List Specific Customer by Number");
  puts("\t\t\t5) List Customer by Name");
  puts("\t\t\t6) Save Customers  to File");
  puts("\t\t\tQ) Quit the Program\n\n");
  printf("\t\t\tPlease Enter Choice: ");
  scanf("%d", &num);

   switch (num)
     {
       case (1): {add_cust(); break;}
       case (2): {mark_cust_del(); break;}
       case (3): {list_all_cust_num(); break;}
       case (4): {list_specific_cust_number(); break;}
       case (5): {list_cust_name(); break;}
       case (6): {save_cust_info(); break;}
       case (7): {break;}
     }
 return;

Quote:
}

add_cust()
{
  clrscr();
  puts("\n\n");
  puts("\t\t\tPlease Enter New Customer");
  getch();
  cust_menu();
  return;

Quote:
}

mark_cust_del()
{
  clrscr();
  puts("\n\n");
  puts("\t\t\tDeleting Inventory Files...");
  getch();
  cust_menu();
  return;

Quote:
}

list_all_cust_num()
{
  clrscr();
  puts("\n\n");
  puts("\t\tListing All Customers By Number. Please wait...");
  getch();
  cust_menu();
  return;

Quote:
}

list_specific_cust_number()
{
  clrscr();
  puts("\n\n");
  puts("\t\tList Specific Customer by Number. Please wait...");
  getch();
  cust_menu();
  return;

Quote:
}

list_cust_name()
{
  clrscr();
  puts("\n\n");
  puts("\t\t\tListing Customer Name.  Please wait...");
  getch();
  cust_menu();
  return;

Quote:
}

save_cust_info()
{
  clrscr();
  puts("\n\n");
  puts("\t\t\tSaving Customer Info. Please wait...");
  getch();
  cust_menu();
  return;
Quote:
}

quit_main_screen()
{
return;

Quote:
}

--



Fri, 10 Aug 2001 03:00:00 GMT  
 Please Help (fread, fwrite)
Please - what is this:

<< FILE * cust_file_ptr = fopen(cust_file,"txt"); >>

Please read a textbook description of fopen(). And this:

puts("\t\t\tSaving Customer Info. Please wait...");
getch();

This segment invites the user to wait (not press a key), then awaits a
keypress that will never come.

This pattern is repeated throughout your program -- many identical routines
(they should be implemented as a single function), all of which repeat the
same mistake.

Here is my advice -- learn basic programming principles, then write some
small, easily managed programs, and over time work your way up to a program
as complex as this one. Don't do it backwards.

On a less important note, getch() and clrscr() are not part of C. They will
make your program non-portable. And main() must return int.

Good luck!
--

Paul Lutus
www.arachnoid.com

<snip>

--



Fri, 10 Aug 2001 03:00:00 GMT  
 Please Help (fread, fwrite)
fopen("a:\myfile","r")...
Better to not hardcode filename/locations into the code though. Some of
us don't have a: drives but /mnt/floppy instead!

I won't even mention the non-standard clrscr(), or improper function
declaration/definition (i.e. should be void func(void) with no return
statement if it is void, and main is always int main, and returns and
int). Oops, I mentioned it!
--
"Many who have learned much posses no sense." - Democritus
--



Fri, 10 Aug 2001 03:00:00 GMT  
 Please Help (fread, fwrite)


Quote:
>I'm "trying" to write a program to read and write in Customer Information
>from a disk (drive A:)into a structure array called customer_info and two
>other
>arrays, one for the customer number "cnum" and the subscript array. The cnum
>array contains the customer number with the subscript array pointing to the
>stucture array. So when you ask for customer number 000001 it will pull up
>the customer data from the stucture array. The first thing the program
>should do is read drive A (31/4 disk)for any information and if none say
>Error opening file . Then the main menu comes up where I should be able to
>add, del, list and what ever else is required "see code". When I add
>customers, the cnum  array is sorted, with the subscript array pointing to
>the data for the structure array. Every time I add a new customer the cnum
>is sorted, structure array gets filled one after other and the subscript
>array points to the proper sturcture array. For option 6 "save the customer
>information", I would just write to Drive A all customer_info (structure
>array) , cnum array (sorted), and its subscript array. How do I read from
>the disk and store it into the arrays and cnum sorted. I haven't even tried
>to write to the disk!! I really need help to get me going on this. I have
>include my code for your pickings.

>Thanks in advance.

>Clinton

>#include <stdio.h>
>#include <stdlib.h>

>struct customer       /* Customer List Data Structure */
>{

>  char name[21];      /* Customer Name */
>  char addr[21];      /* Address */
>  char city[11];      /* City */
>  char prov[11];      /* Province */
>  char pcode[7];      /* Postal Code */
>}  cust_type;

>load_cust();  /*Prototype*/
>add_cust();
>mark_cust_del();
>list_cust_num();
>list_specific_cust_number();
>list_cust_name();
>save_cust();
>cust_menu();
>quit_main_screen();

>const char cust_file[80] = "CUSTOMER.PRO"; /*file to be read in*/

If drive A is not in your searchpath (and I don't think it is) then
put the complete path in your constant like "A:\\CUSTOMER.PRO".

- Show quoted text -

Quote:

>void main(void)
>{
>  clrscr();
>  load_cust(); /*done when first started program */
>  cust_menu();
>}

>load_cust()
>{
>  int size=0;
>  int cnum[80];
>  int subarr[80];
>  FILE * cust_file_ptr = fopen(cust_file,"txt");
>  clrscr();

>  if (cust_file_ptr == NULL)
>  puts("Error opening file");
>  else;
>  puts("Loading Customer Info. Please wait...");
>  fscanf(cust_file_ptr," %d %d %s", cnum[size], subarr[size],
>cust_file_ptr[size]);
>  size++;
>  fclose(cust_file_ptr);
>  return;
>}

>cust_menu()
>{
>  int num;
>  clrscr();
>  puts("\n\n");
>  puts("\t\t\t\t*MAIN MENU*\n\n\n");
>  puts("\t\t\t1) Add Customer Info");
>  puts("\t\t\t2) Mark Customer for Deletion");
>  puts("\t\t\t3) List All Customer by Number");
>  puts("\t\t\t4) List Specific Customer by Number");
>  puts("\t\t\t5) List Customer by Name");
>  puts("\t\t\t6) Save Customers  to File");
>  puts("\t\t\tQ) Quit the Program\n\n");
>  printf("\t\t\tPlease Enter Choice: ");
>  scanf("%d", &num);

>   switch (num)
>     {
>       case (1): {add_cust(); break;}
>       case (2): {mark_cust_del(); break;}
>       case (3): {list_all_cust_num(); break;}
>       case (4): {list_specific_cust_number(); break;}
>       case (5): {list_cust_name(); break;}
>       case (6): {save_cust_info(); break;}
>       case (7): {break;}
>     }
> return;
>}

>add_cust()
>{
>  clrscr();
>  puts("\n\n");
>  puts("\t\t\tPlease Enter New Customer");
>  getch();
>  cust_menu();
>  return;
>}

>mark_cust_del()
>{
>  clrscr();
>  puts("\n\n");
>  puts("\t\t\tDeleting Inventory Files...");
>  getch();
>  cust_menu();
>  return;
>}

>list_all_cust_num()
>{
>  clrscr();
>  puts("\n\n");
>  puts("\t\tListing All Customers By Number. Please wait...");
>  getch();
>  cust_menu();
>  return;
>}

>list_specific_cust_number()
>{
>  clrscr();
>  puts("\n\n");
>  puts("\t\tList Specific Customer by Number. Please wait...");
>  getch();
>  cust_menu();
>  return;
>}

>list_cust_name()
>{
>  clrscr();
>  puts("\n\n");
>  puts("\t\t\tListing Customer Name.  Please wait...");
>  getch();
>  cust_menu();
>  return;
>}

>save_cust_info()
>{
>  clrscr();
>  puts("\n\n");
>  puts("\t\t\tSaving Customer Info. Please wait...");
>  getch();
>  cust_menu();
>  return;
>}
>quit_main_screen()
>{
>return;
>}

--



Fri, 10 Aug 2001 03:00:00 GMT  
 Please Help (fread, fwrite)

[snip]

Quote:
>On a less important note, getch() and clrscr() are not part of C. They will
>make your program non-portable. And main() must return int.

Can you recommend a portable alternative to clrscr()?

--

It is the mark of an instructed mind to rest satisfied with the degree of
precision which the nature of a subject admits, and not to seek exactness
when only an approximation of the truth is possible ...
 ---------------  Aristotle (330 B.C.) -------------

--



Sat, 11 Aug 2001 03:00:00 GMT  
 Please Help (fread, fwrite)
There isn't one -- that's my point. Each platform requires a different
definition.
--

Paul Lutus
www.arachnoid.com

Quote:

>Can you recommend a portable alternative to clrscr()?

--



Sat, 11 Aug 2001 03:00:00 GMT  
 Please Help (fread, fwrite)


Quote:
>There isn't one -- that's my point. Each platform requires a different
>definition.

More than that, you can only meaningfully define clrscr() if your
hardware has a screen (in the sense of some 2D display device)

Quote:
>--

>Paul Lutus
>www.arachnoid.com


>>Can you recommend a portable alternative to clrscr()?

Francis Glassborow      Chair of Association of C & C++ Users
64 Southfield Rd
Oxford OX4 1PA          +44(0)1865 246490
All opinions are mine and do not represent those of any organisation
--



Wed, 15 Aug 2001 03:00:00 GMT  
 Please Help (fread, fwrite)
Sure, and he's used one appropriate for his platform. That's my point. There
isn't a portable alternative.

--

It is the mark of an instructed mind to rest satisfied with the degree of
precision which the nature of a subject admits, and not to seek exactness
when only an approximation of the truth is possible ...
 ---------------  Aristotle (330 B.C.) -------------

Quote:

>There isn't one -- that's my point. Each platform requires a different
>definition.
>--

>Paul Lutus
>www.arachnoid.com


>>Can you recommend a portable alternative to clrscr()?
>--


--



Wed, 15 Aug 2001 03:00:00 GMT  
 Please Help (fread, fwrite)


|>  
|>  [snip]
|>  
|>  >On a less important note, getch() and clrscr() are not part of C. They will
|>  >make your program non-portable. And main() must return int.
|>  
|>  
|>  Can you recommend a portable alternative to clrscr()?

Not 100% portable, but the curses library has been widely ported.

--

GABI Software, 22 rue Jacques-Lemercier, 78000 Versailles, France
Conseils en informatique oriente objet --
              -- Beratung in objektorientierter Datenverarbeitung
--



Thu, 16 Aug 2001 03:00:00 GMT  
 Please Help (fread, fwrite)
Groovy hepcat Bob Jacobs was jivin' on Tue, 23 Feb 1999 05:21:02 GMT
in comp.lang.c.moderated.
Re: Please Help (fread, fwrite)'s a cool scene! Dig it!

Quote:
>Can you recommend a portable alternative to clrscr()?

  No. Such things are essentially non-portable in nature. There are,
however, some common or "fairly portable" ways. For example, in some
implementations sending a form feed character ('\f') will do it.
Another way, though system dependant, is to do this:

#include <stdlib.h>
...
system("CLS");

Another common method of clearing the screen is to send enough newline
characters to the console to over-scroll the screen. There may be
other ways. Read the FAQ.
--

----- Dig the EVEN NEWER, MORE IMPROVED news sig!! -----

-------------- Shaggy was here! ---------------
    http://aardvark.apana.org.au/~phaywood/
============= Ain't I'm a dawg!! ==============

--



Fri, 17 Aug 2001 03:00:00 GMT  
 
 [ 11 post ] 

 Relevant Pages 

1. Please Help (fread, fwrite)

2. Help regarding fread and fwrite

3. Help with fread() and fwrite()

4. fread, fwrite probs

5. fwrite(), fread() portability.

6. problem with fwrite / fread

7. fopen() fseek() fread() fwrite()

8. Problem with struct/array inside a struct using fread/fwrite

9. Strange byte order using fread() and fwrite()

10. fread and fwrite

11. fread and fwrite sync

12. microsoft endian problem with fread/fwrite

 

 
Powered by phpBB® Forum Software