473,396 Members | 1,975 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

help to detect errors in this simple proggramme

help to detect errors in this simple proggramme

Expand|Select|Wrap|Line Numbers
  1.  
  2.     #include<stdio.h>
  3.     #include<ctype.h>
  4.     #include<string.h>
  5.     struct customer readCustomer();
  6.     struct slot readSlot();
  7.     struct slot{int day,period;};
  8.     struct customer{
  9.     char name[50],tel[10], paidOrNot[8];};
  10.     struct booking{
  11.         char freeOrNot;//if not free, gives 'n'....
  12.         struct slot slot_;
  13.         struct customer customer_;
  14.     };
  15.     struct bookingFile{
  16.          struct booking array[7][10];   
  17.     };
  18.  
  19.     main(){
  20.         FILE *fp;
  21.         struct bookingFile bf;
  22.         struct slot s;
  23.         fp=fopen("store.txt","r+");
  24.         if(fp==NULL){
  25.             printf("cannot open the file");
  26.             exit(0);
  27.         }
  28.         fread(&bf,sizeof(struct bookingFile),1,fp);
  29.     do{                //validating whether the given slot is free or not....
  30.  
  31.         s=readSlot();
  32.         if(bf.array[s.day][s.period].freeOrNot=='n')
  33.         printf("Sorry,the slot you gave has been already booked.Try another slot!\n");
  34.     }while(bf.array[s.day][s.period].freeOrNot=='n');
  35.  
  36.     bf.array[s.day][s.period].freeOrNot=0;
  37.     bf.array[s.day][s.period].slot_=s;
  38.     bf.array[s.day][s.period].customer_=readCustomer();
  39.     rewind(fp);
  40.     fwrite(&bf,sizeof(struct bookingFile),1,fp)    ;
  41.     rewind(fp);   
  42.             printf("%s\n",bf.array[0][0].customer_.tel);//line a
  43.             printf("%s\n",bf.array[1][1].customer_.tel);//line b
  44.             printf("%s\n",bf.array[2][2].customer_.tel);//line c
  45.             printf("%s\n",bf.array[3][3].customer_.tel);//line d
  46.             fclose(fp);   
  47.     }
  48.  
  49.     struct customer readCustomer(){
  50.         struct customer c;
  51.  
  52.         printf("Enter your name: ");
  53.         gets(c.name);
  54.         printf("Enter the telephone number: ");
  55.         gets(c.tel);
  56.         printf("Are you paying ,just now?(Yes/No):");
  57.         gets(c.paidOrNot);
  58.  
  59.         return c;
  60.     }
  61.     struct slot readSlot(){
  62.         struct slot s;
  63.         printf("Enter the number of day you want\n1.mon\n2.tues\n3.wed\n4.thu\n5.fri\n6.sat\n7.sun: ");
  64.         scanf("%i",&s.day);
  65.         printf("Enter the number of period you want\n1.8-9\n2.9-10\n3.10-11\n4.11-12\n5.12-1\n6.1-2\n7.2-3\n8.3-4\n9.4-5\n10.5-6: ");
  66.         scanf("%i",&s.period);
  67.         return s;
  68.     }


This code compiles fine.But
the data I tried to save to the file,' store.txt' are not permanantly stored. i could assert it by running this code block several times(more than 4), by the output produced by line a,b,c&d.
Can you plese help me to detect the reasons for why it doesn't get stored them permanantly.
Sep 17 '13 #1
9 1449
weaknessforcats
9,208 Expert Mod 8TB
This code:

Expand|Select|Wrap|Line Numbers
  1. rewind(fp);
  2. fwrite(&bf,sizeof(struct bookingFile),1,fp)    ;
  3. rewind(fp);   
positions your file to the beginning before writing, So all of your data gets written on top of previous data at the beginning of the file.

You should use fseek(SEEK_END) before writing.
Sep 17 '13 #2
what I just wanted in this progrrame is that.it means i want to modify the array each time & restore it .so data gets written on top of previous data at the beginning of the file is not a problem.
Sep 18 '13 #3
weaknessforcats
9,208 Expert Mod 8TB
So what is in store.txt after you run the program?
Sep 18 '13 #4
I want to store the finally modified array in the store.txt.


In the each time this code run I read the previosly stored array and append new data to it.
for example think that in the previous session I have filled the array cell 1.then next session I fill the array cell 2.

Like that I continued the filling array,But previously stored data should remained,(although it overriden the array each time:because previously stored array is read in the current session & modify it & rewrite it in the file)shoudn't that?

But it doesn't happen as I supposed.Previously stored data to the array sometimes get vanished.
Sep 18 '13 #5

Sep 18 '13 #6
weaknessforcats
9,208 Expert Mod 8TB
This is the time you need to start stepping through the code using your debugger. Embedded printf statements are not a good debugging method.

If you don't know how to use your debugger, then this is a great opportunity t learn it. Most debuggers are very easy to use and they let you see the values in your variables each step of the way. Just start at the beginning of main() and step through the code a line at a time. Be sure to step into each of your functions. Verify the variables have correct values at all times.
Sep 18 '13 #7
Banfa
9,065 Expert Mod 8TB
Line 32/34 you check freeOrNot to see if it is 'n' to indicate if the slot is taken but at line 36 you set freeOrNot to 0 when actually writing a record.

You have named the file storer.txt which suggests you think it is a text file but you use fread/fwrite which read/write binary data.
Sep 19 '13 #8
yes Banfa,you are correct.this is a mistake happened when I post it.result is same even after 0 replaced by n.
can't we actually use fread/fwrite for text files?then how can i fix it?what should be the extention?
Sep 19 '13 #9
weaknessforcats
9,208 Expert Mod 8TB
All files are binary files.

A "text file" is just a binary file where each byte is an ASCII character between 0 and 127. The normal process is to unpack struct members into arrays of a fixed length and then write the array to the file. You need to know the order in which the arrays were written in order to read them back.

When you read the arrays back you pack the arrays back into a struct. Since you know the order in which they were written, you know how to load the struct

Often and "record mark". Usually a \n, but sometimes a \0, is written to indicate the end of a piece of data. In cases like this you can read the data in a file as strings.

The name of the file has no connection to the data in the file.
Sep 19 '13 #10

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

Similar topics

3
by: Lodewijk van Haringhal | last post by:
I'am new with javascritping not with programming. Is there nobody who can help me with ths simple promblem? :) Please, please give me a hint. Please help me with this script. I have two lists in...
1
by: Proteus | last post by:
Any help appreciated on a small perl project I need to write for educator/teaching purposes. I have not programmed perl for some time, need to get up to speed, maybe some kind souls hrere will help...
0
by: Flemming Jensen | last post by:
The idea behind this code is to standardize all my webpages in an application to look the same by inheriting the pages from a userdefined class. But it gives me problems regarding sessionstate... ...
1
by: Miguel Dias Moura | last post by:
Hello, i am creating an ASP.net / VB web site with Dreamweaver MX 2004. I have a form and a "Insert Record Behavior" to insert the form values in the database. Dreamweaver puts this code in...
3
by: Milsnips | last post by:
hi there, i am currently using the following code to generate an XML object, then save it, then read the contents of the file - but i dont wanna save it, instead read it directly after i;ve...
2
by: RC | last post by:
http://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_sort2 You can see above link or read below i copy/paste from above link <script type="text/javascript"> function sortNumber(a, b)...
5
by: jeffsnox | last post by:
Here's the code which I'm saving in an HTML and opening in a Java enabled browser. It's giving me the following error: Line: 34 Char: 1 Error: Object Expected Many thanks in advance for any...
0
by: clemrock | last post by:
Help w/ errors.add_to_base between controller and model Hello, I'm having trouble in routing some errors between model and controller. The errors produced in the controller...
14
by: helpfulmaid | last post by:
Hello, I want a function that when it detects the left mouse button is pressed down, will generate and cycle 5 random number/letter combinations as fast as it can in a printed space. If I let off...
3
by: cmrhema | last post by:
Hi, I need help in phrasing this simple query. I have two tables 1. Category table, it consists of categoryid and productid 2. product table, it consists of categoryid, productid, productname,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.