coding matter please help
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;
}
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