CrazyEngineers
  • coding matter please help

    Deitel

    Member

    Updated: Oct 25, 2024
    Views: 1.1K
    i am trying to write and read a simple program using these codes. writing code seems to work fine,it prompts for data and after it terminates properly.but the reading code only give me the error "file could not be opened". I am using visual studio 2008 express. thank you!!

    writing code

    #include <stdio.h>



    int main(void)
    {
    int account;
    char name[30];
    double balance;

    FILE *cptr;
    if ((cptr=fopen("clients.dat","w"))==NULL){
    printf("hello world");
    }
    else{
    printf("enter the account details\n");
    printf("enter EOF\n");
    printf("?");
    scanf("%d%s%lf",&account,name,&balance);
    while(!feof(stdin))
    {
    fprintf(cptr,"%d%s%.2f\n",account,name,balance);
    printf("?");
    scanf("%d%s%lf",&account,name,&balance);
    }
    fclose(cptr);
    }


    return 0;
    }

    reading code

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

    int main(void)
    {
    int account;
    char name[30];
    double balance;

    FILE *cptr;

    if (( cptr = fopen("clients.dat","r")) == NULL){
    printf("file cannot be opened\n");
    }
    else
    {
    printf("%-10s%-13s%s\n","Account","Name","Balance");
    fscanf(cptr,"%d%s%lf",&account,name,&balance);
    while (!feof(cptr)){
    printf("%-10d%-13s%7.2f",account,name,balance);
    fscanf(cptr,"%d%s%lf",&account,name,&balance);
    }
    fclose(cptr);
    }
    getch();
    return 0;
    }
    0
    Replies
Howdy guest!
Dear guest, you must be logged-in to participate on CrazyEngineers. We would love to have you as a member of our community. Consider creating an account or login.
Replies
  • Deitel

    MemberJun 18, 2011

    i am an administrator in the computer,do you think I might not have to right to write on the disk,if so is there a way to change it😔
    Are you sure? This action cannot be undone.
    Cancel
  • PraveenKumar Purushothaman

    MemberJun 28, 2011

    Hey, this code runs only in Turbo C and not Visual Studio. 😀 Please download Borland C for Windows and try it out... 😀
    Are you sure? This action cannot be undone.
    Cancel
Home Channels Search Login Register