i'm studying file processing. when i execute the second code that Reading Data from a Sequential Access File something goes wrong with it. i couldn't find what is wrong. is there anyone who can help me?

/*creating a sequential file*/
#include<stdio.h>
int main()
{
    int account;
    char name [30];
    double balance;
    FILE *cfPtr; 
    if((cfPtr=fopen("client.dat","w"))==NULL)
    printf("file could not be opened\n");
    else
    {
        printf("enter account name and balance\n");
        printf("enter eof to end input \n");
        printf("?");
        scanf("%d%s%lf",&account,name,&balance);
        while(!feof(stdin)){
                            fprintf(cfPtr,"%d%s%.2f\n",account,name,balance);
                            printf("?");
                            scanf("%d%s%lf",&account,name,&balance);
                            }
                            fclose(cfPtr);
                            }
                            return 0;
                            }
/* Reading Data from a Sequential Access File */
#include<stdio.h>
#include<conio.h>
int main()
{
    int account;
    char name[30];
    double balance;
    FILE *cfPtr; 
    if((cfPtr=fopen("client.dat","r"))==NULL)
    printf("file could not be opened\n");
    else
    {
        printf("%-10s%-13s%s\n","account","name","balance");
        fscanf(cfPtr,"%d%s%lf",&account,name,&balance);
        while(!feof(cfPtr)){
        printf("%-10d%-13s%7.2f\n",account,name,balance);
        fscanf(cfPtr,"%d%s%lf",&account,name,&balance);
        }
        fclose(cfPtr);
        }
        getch();
        return 0;
        }

when i execute first code i enter these values:
100 jones 24.98
200 doe 345.67
300 White 0.00
400 stone -42.16
500 rich 224.62

and when i execute the second code to read from the file i get this result:

Probably because your input text file doesn't have a space delimiter between each field as indicated by the statement below from your first source code file which creates this input text file:

fprintf(cfPtr,"%d%s%.2f\n",account,name,balance);

Try using this statement in your first source code file:

fprintf(cfPtr,"%d %s %.2f\n",account,name,balance);

it is OK now. Thank you so much for your help.

Probably because your input text file doesn't have a space delimiter between each field as indicated by the statement below from your first source code file which creates this input text file:

fprintf(cfPtr,"%d%s%.2f\n",account,name,balance);

Try using this statement in your first source code file:

fprintf(cfPtr,"%d %s %.2f\n",account,name,balance);

i have a problem too with the file processing..

/*Create a sequential file*/
/*"w" is a made for creating a file for writing*/
/*if the file already exists, it will discard the current contents*/
/*%s stands for string is a placeholder for type char*/

#include <stdio.h>

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

	FILE*cfPtr;/*cfPtr=clients.txt file pointer*/ /*what is a file pointer?*/

	clrscr();
	if((cfPtr=fopen("clients.dat","w"))==NULL) /*what is the function of fopen*/
	printf("file could not be opened\n");

	else
	{
	printf("Enter the account,name, and balance.\n");
	printf("\nEnter EOF to end input.\n");
	printf("?");
	scanf("%d%s%1f",&account,name,&balance);


	while(!feof(stdin)){
		fprintf(cfPtr,"%d %s %.2f\n",account,name,balance);	/*what is the function of fprintf?*/
		printf("?");
		scanf("%d%s%1f",&account,name,&balance);


	}
	fclose(cfPtr);		/*what is the function of fclose?*/
	}
	return 0;
	}

the output shown on the attachment.

what seems to be the problem?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.