coding matter please help

Deitel
@deitel-Slr5fW โ€ข Oct 25, 2024

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;
}

Replies

Welcome, guest

Join CrazyEngineers to reply, ask questions, and participate in conversations.

CrazyEngineers powered by Jatra Community Platform

  • Deitel

    @deitel-Slr5fW • Jun 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๐Ÿ˜”

  • PraveenKumar Purushothaman

    @praveenkumar-66Ze92 • Jun 28, 2011

    Hey, this code runs only in Turbo C and not Visual Studio. ๐Ÿ˜€ Please download Borland C for Windows and try it out... ๐Ÿ˜€