Thread: data file problem,the pointer can't go to next record..Attn. all expert. urgent

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    3

    Question data file problem,the pointer can't go to next record..Attn. all expert. urgent

    To all expert ..

    I create a data file call bill_system.dat,and i create a structure call telephone to store the record in this data file .And this all part ,is no problem, and the problem is when i retrieve the data ,what ever previow i key in how many record in to data file,he only return the first data only,and i try a many time i also dun no y the file pointer is can't move to next position.

    below is my code it is have 3 code ,first is create the record data file, second is add record code ,third is retrieve data code.

    //first code
    # include <stdio.h>

    struct telephone{
    int indexkey;
    int day;
    int month;
    int year;
    char week[10];
    int hour;
    int minute;
    int end_h;
    int end_m;
    int total;
    //char name[20];
    char telephone[10];
    float cost;
    };

    main()
    {
    int i;
    struct telephone blankreport = {0,0,0,0,"",0,0,0,0,0/*,""*/,"",0.0};
    FILE *cfPtr;

    if ((cfPtr=fopen("bill_system.dat","w")) == NULL)
    printf("File could not be opened.\n");
    else {
    for (i=1; i<=1000;i++)
    fwrite(&blankreport,sizeof(struct telephone),1,cfPtr);
    fclose (cfPtr);
    }
    return 0;

    //Second code is add record

    #include<string.h>
    #include<stdio.h>
    #include <conio.h>
    #include<dos.h>

    struct telephone{
    int indexkey;
    int day;
    int month;
    int year;
    char week[10];
    int hour;
    int minute;
    int end_h;
    int end_m;
    int total;
    //char name[20];
    long int telephone;
    float cost;
    };

    main()
    {
    FILE *cfPtr;
    struct telephone report;
    struct date d;
    struct time t;
    int total_h;
    int i;
    char *buf1 = "Mon", *buf2 = "Tue", *buf3 = "Wed",*buf4 = "Thur",
    *buf5 = "Fri",*buf6 = "Sat",*buf7 = "Sun";
    int ptr;

    if ((cfPtr = fopen("bill_system.dat","r+")) == NULL)
    printf("FILE NOT FOUND.\n");
    else {
    getdate(&d);
    printf("Today date %d/%d/%d\n", d.da_day,d.da_mon,d.da_year);
    gettime(&t);
    printf("The current time is: %2d:%02d\n",
    t.ti_hour, t.ti_min);
    printf ("Enter account number" "(1 to 1000, 0 to end the process)\n? ");
    scanf("%d",&report.indexkey);

    while (report.indexkey != 0){

    i=0;
    while(i<=2){
    printf("Enter day (1-31): ");
    scanf("%d",&report.day);
    if ((report.day > 31)||(report.day<=0)||(report.day==' ')){
    printf("Warning,unlogical data ,please try again !!!\n");
    i++;
    }
    if ((report.day <= 31)&&(report.day>= 1)){
    i=4;
    }
    if (i == 3){
    printf("Process terminate ,illegal operate!!!");
    return 0;
    }
    }
    i=0;
    while(i<=2){
    printf("Enter month (1-12): ");
    scanf("%d",&report.month);
    if ((report.month > 12)||(report.month <=0)||(report.month==' ')){
    printf("Warning,unlogical data ,please try again !!!\n");
    i++;
    }
    if ((report.month <= 12)&&(report.month >= 1)){
    i=4;
    }
    if (i == 3){
    printf("Process terminate ,illegal operate!!!");
    return 0;
    }
    }

    i=0;
    while(i<=2){
    printf("Enter year (yyyy): ");
    scanf("%d",&report.year);
    if ((report.year < 1980)||(report.year > 3000)||(report.year==' ')){
    printf("Warning,unlogical data ,please try again !!!\n");
    i++;
    }
    if ((report.year > 1980)&& (report.year < 3000)){
    i=4;
    }
    if (i == 3){
    printf("Process terminate ,illegal operate!!!");
    return 0;
    }
    }

    i=0;
    while(i<=2){
    printf("Enter today date (Mon,Tue,Wed,Thur,Fri,Sat,Sun) : ");
    scanf("%s",&report.week);
    //if (report.week== " ")

    ptr = strcmp(report.week, buf1);
    if (ptr != 0)

    ptr = strcmp(report.week, buf2);
    if (ptr != 0)

    ptr = strcmp(report.week, buf3);
    if (ptr != 0)

    ptr = strcmp(report.week, buf4);
    if (ptr != 0)

    ptr = strcmp(report.week, buf5);
    if (ptr != 0)

    ptr = strcmp(report.week, buf6);
    if (ptr != 0)

    ptr = strcmp(report.week, buf7);
    if (ptr != 0)
    {
    printf("Warning,unlogical data ,please try again !!!\n");
    i++;
    }

    if (ptr == 0){
    i=4;
    }
    if (i == 3){
    printf("Process terminate ,illegal operate!!!");
    return 0;
    }
    }

    i=0;
    while(i<=2){
    printf("Enter Starting time ((hour)01 to 24): ");
    scanf("%d",&report.hour);
    if ((report.hour >= 25)||(report.hour==' ')){
    printf("Warning,unlogical data ,please try again !!!\n"); i++;
    }
    if (report.hour<= 24){
    i=4;
    }
    if (i == 3){
    printf("Process terminate ,illegal operate!!!");
    return 0;
    }
    }

    i=0;
    while(i<=2){
    printf("Enter Starting time ((minute)00 to 59): ");
    scanf("%d",&report.minute);
    if ((report.minute >= 60)||(report.minute==' ')){
    printf("Warning,unlogical data ,please try again !!!\n");
    i++;
    }
    if (report.minute <= 59){
    i=4;
    }
    if (i == 3){
    printf("Process terminate ,illegal operate!!!");
    return 0;
    }
    }

    i=0;
    while(i<=2){
    printf("Enter Ending time (hour)00 to 24)): ");
    scanf("%d",&report.end_h);
    if ((report.end_h >= 25)||(report.end_h==' ')){
    printf("Warning,unlogical data ,please try again !!!\n");
    i++;
    }
    if (report.end_h <= 24){
    i=4;
    }
    if (i == 3){
    printf("Process terminate ,illegal operate!!!");
    return 0;
    }
    }

    i=0;
    while(i<=2){
    printf("Enter Ending time ((minute)00 to 59): ");
    scanf("%d",&report.end_m);

    if ((report.end_m >= 60)||(report.end_m ==' ')){
    printf("Warning,unlogical data ,please try again !!!\n");
    i++;
    }
    if (report.end_m <= 59){
    i=4;
    }
    if (i == 3){
    printf("Process terminate ,illegal operate!!!");
    return 0;
    }
    }

    i=0;
    while(i<=2){
    printf("Enter phone number of you call : ");
    scanf("%ld",&report.telephone);
    if (report.telephone==' '){
    printf("Warning,unlogical data ,please try again !!!\n");
    i++;
    }
    i=4;
    }
    /*i=0;
    while(i<=2){
    printf("Enter caller name : ");
    scanf("%s",&report.name);
    if (report.name==" "){
    printf("Warning,unlogical data ,please try again !!!\n");
    i++;
    }
    i=4;
    }*/
    total_h = (report.end_h - report.hour)* 60;
    report.total = (report.end_m + total_h)- report.minute;

    //if((report.hour >= 19)||(report.hour <= 7)){
    report.cost = report.total * 0.15;
    //}else
    //{
    //report.cost = report.total * 0.25;
    //}
    printf("This phone call charge is RM %.2f\n",report.cost);
    printf("--------------------------------------------------\n\n");
    fseek(cfPtr, (report.indexkey -1) *
    sizeof(struct telephone), SEEK_SET);
    fwrite(&report,sizeof(struct telephone),1,cfPtr);
    printf ("Enter account number" "(1 to 1000, 0 to end the process)\n? ");
    scanf("%d",&report.indexkey);

    }
    }
    fclose(cfPtr);
    return 0;
    }

    }


    //Third Code

    #include<string.h>
    #include<stdio.h>
    #include <conio.h>
    #include<dos.h>

    struct telephone{
    int indexkey;
    int day;
    int month;
    int year;
    char week[10];
    int hour;
    int minute;
    int end_h;
    int end_m;
    int total;
    long int telephone;
    float cost;
    };
    main()
    {
    FILE *cfPtr;
    struct telephone report;
    int i,a=1,y,z;
    char *x;
    //long int ttemp;
    int temp1=0,htemp=0,m1temp=0,hlast=0,m1last=0;
    int dtemp=0,mtemp=0,ytemp=0;
    int dlast=0,mlast=0,ylast=0;

    float ctemp=0.0;

    while (a!=0){
    if ((cfPtr = fopen("bill_system.dat","r")) == NULL)
    printf("FILE COULD NOT BE OPENED.\n");
    else {
    i=0;
    while(i<=2){
    printf("Enter month (1-12): ");
    scanf("%d",&y);
    if ((y > 12)||(y <=0)){
    printf("Warning,unlogical data ,please try again !!!\n");
    i++;
    }
    if ((y <= 12)&&(y>=1)){
    i=4;
    }
    if (i == 3){
    printf("Process terminate ,illegal operate!!!");
    return 0;
    }
    }
    i=0;
    while(i<=2){
    printf("Enter year (yyyy): ");
    scanf("%d",&z);
    if ((z < 1980)|| (z > 3000)){
    printf("Warning,unlogical data ,please try again !!!\n");
    i++;
    }
    if ((z > 1980)&& (z < 3000)){
    i=4;
    }
    if (i == 3){
    printf("Process terminate ,illegal operate!!!");
    return 0;
    }
    }

    if(y ==1)
    x ="January";
    if(y ==2)
    x = "February";
    if(y ==3)
    x ="March";
    if(y ==4)
    x ="April";
    if(y ==5)
    x ="May";
    if(y ==6)
    x ="June";
    if(y ==7)
    x ="July";
    if(y ==8)
    x ="August";
    if(y ==9)
    x ="September";
    if(y ==10)
    x ="October";
    if(y ==11)
    x ="November";
    if(y ==12)
    x ="December";

    clrscr();
    gotoxy(20,3);
    printf("The Monthly Phone Call Report(%s)\n",x);
    printf("------------------------------------------------------------------------\n");
    gotoxy(10,5);
    printf("Total Call :");
    gotoxy(10,6);
    printf("First Call Date :");
    gotoxy(10,7);
    printf("First Call Time :");
    gotoxy(10,8);
    printf("Last Call Date :");
    gotoxy(10,9);
    printf("Last Call Time :");
    gotoxy(10,10);
    printf("Total Calling Time :");
    gotoxy(10,11);
    printf("Total Calling Cost :");
    i=0;
    while(!feof(cfPtr)){
    fread(&report,sizeof(struct telephone),1,cfPtr);
    if((report.month == y)&&(report.year == z))
    gotoxy(1,13);
    printf("%d",report.day);
    //check the lastcall
    if((dtemp==0)||(dtemp <= report.day)){/*&&(mtemp <= report.month)&&(ytemp <= report.year))){*/
    if((htemp == 0)||(htemp <= report.hour)){

    dtemp=report.day;
    mtemp=report.month;
    ytemp=report.year;
    htemp=report.hour;
    m1temp=report.minute;
    }

    }
    //Check the first call
    /*if((dlast == 0)||((dlast >= report.day)&&(mlast >= report.month)&&(ylast >= report.year))){
    if((hlast == 0)||(htemp < report.hour))*/
    if((report.month == y)&&(report.year == z)){
    if((dlast==0)||(dlast <= report.day)){/*&&(mtemp <= report.month)&&(ytemp <= report.year))){*/
    if((hlast == 0)||(hlast<= report.hour)){


    dlast=report.day;
    mlast=report.month;
    ylast=report.year;
    hlast=report.hour;
    m1last=report.minute;
    }


    }
    }
    ctemp=ctemp + report.cost;
    temp1=temp1 + report.total;
    i++;
    }
    gotoxy(32,5);
    printf("%d",i);
    gotoxy(32,6);
    printf("%d/%d/%d",dlast,mlast,ylast);
    gotoxy(32,7);
    printf("%d:%d ",hlast,m1last);
    gotoxy(32,8);
    printf("%d/%d/%d",dtemp,mtemp,ytemp);
    gotoxy(32,9);
    printf("%d:%d ",htemp,m1temp);
    gotoxy(32,10);
    printf("%d minute",temp1);
    gotoxy(32,11);
    printf("%.2f \n",ctemp);

    }



    gotoxy(2,13);
    printf("Do you want go to next monthly report? (1=continue,0 =stop) : ");
    scanf("%d",&a);

    if (a==0){
    a=0;
    fclose(cfPtr);
    }else{
    a=1;
    temp1=0,htemp=0,m1temp=0,hlast=0,m1last=0;
    dtemp=0,mtemp=0,ytemp=0;
    dlast=0,mlast=0,ylast=0;
    ctemp=0.0;
    clrscr();
    fclose(cfPtr);
    }
    }
    fclose(cfPtr);
    return 0;
    }

    suppose this code will compile together
    so , i hold all expert help me to solve this problem,because this is my assingnment must passup before 10.10.2001..

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    Well there are a number of problems
    1. struct telephone{ isn't the same in all 3 files. Absolutely nothing will work until this is fixed. Put the structure into a .h file, and #include it in all your source files as needed.

    2. if ((cfPtr=fopen("bill_system.dat","w")) == NULL)
    When working with binary files, use the open modes of "rb" and "wb".

    3. char *buf1 = "Mon", *buf2 = "Tue",
    Use an array
    char *days[] = { "Mon", "Tue", "Wed", ... };

    > ptr = strcmp(report.week, buf1);
    Then this becomes a for loop operating on that array
    for ( i = 0 ; i < 7 ; i++ ) {
    if ( strcmp( report.week, days[i] ) == 0 ) ...
    }

    4. Read up on scanf
    > scanf("%d",&report.day);
    > if ((report.day > 31)||(report.day<=0)||(report.day==' ')){
    report.day is not a space character if you type in non-numeric data to scanf.
    You need something like
    if ( scanf("%d",&report.day) == 1 ) {
    &nbsp; // check range of report.day
    } else {
    &nbsp; // non-numeric input, try again
    }

    5. You have several repeated block of code which begin with
    > i=0;
    > while(i<=2){
    With a bit of parameterisation, you could make this a function, and considerably simplify the code.

    6. watch out for the feof() 'bug'
    > while(!feof(cfPtr)){
    > fread(&report,sizeof(struct telephone),1,cfPtr);
    Should be
    while( fread(&report,sizeof(struct telephone),1,cfPtr) == 1 ) {
    Otherwise, you will process the last record in the file twice.

    And read this before posting more code
    http://www.cprogramming.com/cboard/m...bbcode#buttons
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  2. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  3. HUGE fps jump
    By DavidP in forum Game Programming
    Replies: 23
    Last Post: 07-01-2004, 10:36 AM
  4. Need help fixing bugs in data parsing program
    By daluu in forum C Programming
    Replies: 8
    Last Post: 03-27-2003, 06:02 PM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM