I am not getting all numbers but only last number while readind data from file.
And it has to be done with fscanf function.

/*  input n nunbers fom user and write it in the file.find it's sum
	 and  write the ans. in another file                   */
#include <stdio.h>
#include <conio.h>
void main()
{
	int i,n,no,sum=0;
	FILE *fp,*fs;
	fp=fopen("d:\\tc\\bin\\strtNo.txt","w");
	printf("%s","Enter value for n : ");
	scanf("%d",&n);
	for(i=0;i<n;i++)
	{
		printf("\n Enter the no : ");
		scanf("%d",&no);
		fprintf(fp,"%d\n",no);
	 	sum+=no;
	}
	fprintf(fp,"%d\n",99); 
	fclose(fp);
	fs=fopen("d:\\tc\\bin\\strtSum.txt","w");
	fprintf(fs,"%d",sum);
	fclose(fs);
	fp=fopen("d:\\tc\\bin\\strtNo.txt","r");
	if(fp==NULL)
	{
		printf("ERROR");
	}
	while((fscanf(fp,"%d\n",no))!=99)
	{
		printf("%d\n",no);
	}
	fclose(fp);
	getch();
}

I am not getting all numbers but only last number while readind data from file.
And it has to be done with fscanf function.

See the comments:

/*  input n nunbers fom user and write it in the file.find it's sum
	 and  write the ans. in another file                   */
#include <stdio.h>
#include <conio.h>        // do not use this header, it's not portable.
void main()               // main is an INT function, not VOID
{
	int i,n,no,sum=0;
	FILE *fp,*fs;
	fp=fopen("d:\\tc\\bin\\strtNo.txt","w");
	printf("%s","Enter value for n : ");
	scanf("%d",&n);
	for(i=0;i<n;i++)
	{
		printf("\n Enter the no : ");
		scanf("%d",&no);
		fprintf(fp,"%d\n",no);
	 	sum+=no;
	}
	fprintf(fp,"%d\n",99); 
	fclose(fp);
	fs=fopen("d:\\tc\\bin\\strtSum.txt","w");
	fprintf(fs,"%d",sum);
	fclose(fs);
	fp=fopen("d:\\tc\\bin\\strtNo.txt","r");
	if(fp==NULL)
	{
		printf("ERROR");
	}
	while((fscanf(fp,"%d\n",no))!=99)  // What is the return value of fscanf?
	{                                  // You need to look it up...
		printf("%d\n",no);
	}
	fclose(fp);
	getch();           // Don't use -- it's not portable.  But GETCHAR() is!
}
/*  while reading from file, the pointer do not increases 
and the "no" variable contains the last value which we have written to that file  while retrieving              */
#include <stdio.h>
#include <conio.h>
int main()
{
	int i,n,no,tno,sum=0;
	FILE *fp,*fs;
	fp=fopen("d:\\tc\\bin\\strtNo.txt","w");
	printf("%s","Enter value for n : ");
	scanf("%d",&n);
	for(i=0;i<n;i++)
	{
		printf("\n Enter the no : ");
		scanf("%d",&no);
		fprintf(fp,"\n%d\n",no);
	  //putw(no,fp);   binary form
		sum+=no;
	}
	fprintf(fp,"\n%d\n",-1); //as we are dealing with int so explicit EOF is neede
	fclose(fp);
	fs=fopen("d:\\tc\\bin\\strtSum.txt","w");
	fprintf(fs,"%d",sum);
	fclose(fs);
	fp=fopen("d:\\tc\\bin\\strtNo.txt","r");
	if(fp==NULL)
	{
		printf("ERROR");
	}
	while((tno=(fscanf(fp,"\n%d\n",no)))!='-1')
	{
		printf("%d\n",tno);
	}

	fclose(fp);
	getche();
	return(0);
}

So it's working, right?

To:WaltP

I have written the problem in comment above. And the problem is still the same

First, don't just add comments detailing errors. Add information in the post. The comments in the code are used for additional help.

Second, I don't understand the comments. Please give more detail.

please run the program once, you will understand the problem better.


when we read value of no,the last value will remain in "no"
and while we use fscanf function to get the value of "no" back to screen it shows the last value which it holds.

while it should read the first number as we are closing the file and open the file in read mode.And at that time the pointer must point at the first position.

so if numbers('no variable) are : 1 2 3 4 5

than it shows me only '5' infinite times.

What does this mean?

fprintf(fp,"\n%d\n",-1); //as we are dealing with int so explicit EOF is neede

1 2 3 4 -1//at last -1 will be added and when u read numbers it will show numbers until you get -1.

I still don't understand. You are talking about EOF. And you're writing a -1. Why? Is -1 part of the data? Or are you writing -1 thinking that's an EOF? It's not. EOF is EOF. It's what you get when you read past the End Of File. you don't need to help the system, it knows.

So take out all that EOF crap and post again.

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.