using codeblocks
-------------------------------------------------------------------------------------------------
//using fprintf() to store data(one integer) and fscanf()() to read (one integer)that data from an opened //file
#include <stdio.h>
#include<stdlib.h> // header file for system("cls")
#include<conio.h>
int main()
{
FILE *p; //pointer declaration
char ch;
int i;
system("cls"); //clears the screen/console
p=fopen("data1.txt","w"); //file opening in write mode
printf("first we store/write data in an opened file\n");
for(i=0;i<=40;i++) //executes the loop
{
fprintf(p,"%d\n",i); // write the entered integer in that file
}
fclose(p); //closes the file
getch();
printf("data/numbers stored successfully!!!\n");
printf("now we are going to read that stored data\n");
p=fopen("data1.txt","r"); // openes the file in read only mode
while((fscanf(p,"%d",&i))!=EOF) //read an integer using fscanf() from opened file util it ends.
{
printf("%d,",i); //prints that integer on screen
}
fclose(p); //closes the file
return 0;
}
//note: The second part (reading file), we can include in
//different file rather in same file as written here.
// But we have to know its data file name.
//i mean to say, the code would be :
/*
#include <stdio.h>
int main()
{
FILE *p; //pointer declaration
char ch;
p=fopen("data1.txt","r"); // openes the file in read only mode
while((fscanf(p,"%d",&i))!=EOF) //read an integer using getw from opened file.
{
printf("%d,",i); //prints that integer on screen
}
fclose(p); //closes the file
return 0;
}
*/
-------------------------------------------------------------------------------------------------
//using fprintf() to store data(one integer) and fscanf()() to read (one integer)that data from an opened //file
#include <stdio.h>
#include<stdlib.h> // header file for system("cls")
#include<conio.h>
int main()
{
FILE *p; //pointer declaration
char ch;
int i;
system("cls"); //clears the screen/console
p=fopen("data1.txt","w"); //file opening in write mode
printf("first we store/write data in an opened file\n");
for(i=0;i<=40;i++) //executes the loop
{
fprintf(p,"%d\n",i); // write the entered integer in that file
}
fclose(p); //closes the file
getch();
printf("data/numbers stored successfully!!!\n");
printf("now we are going to read that stored data\n");
p=fopen("data1.txt","r"); // openes the file in read only mode
while((fscanf(p,"%d",&i))!=EOF) //read an integer using fscanf() from opened file util it ends.
{
printf("%d,",i); //prints that integer on screen
}
fclose(p); //closes the file
return 0;
}
//note: The second part (reading file), we can include in
//different file rather in same file as written here.
// But we have to know its data file name.
//i mean to say, the code would be :
/*
#include <stdio.h>
int main()
{
FILE *p; //pointer declaration
char ch;
p=fopen("data1.txt","r"); // openes the file in read only mode
while((fscanf(p,"%d",&i))!=EOF) //read an integer using getw from opened file.
{
printf("%d,",i); //prints that integer on screen
}
fclose(p); //closes the file
return 0;
}
*/
--------------------------------------------------------------------------------
using turboc++
------------------------------------------------------------------
//using fprintf() to store data(one integer) and fscanf()() to read (one integer)that data from an opened //file
#include <stdio.h>
#include<stdlib.h> // header file for system("cls")
#include<conio.h>
int main()
{
FILE *p; //pointer declaration
char ch;
int i;
system("cls"); //clears the screen/console
p=fopen("data1.txt","w"); //file opening in write mode
printf("first we store/write data in an opened file\n");
for(i=0;i<=40;i++) //executes the loop
{
fprintf(p,"%d\n",i); // write the entered integer in that file
}
fclose(p); //closes the file
getch();
printf("data/numbers stored successfully!!!\n");
printf("now we are going to read that stored data\n");
p=fopen("data1.txt","r"); // openes the file in read only mode
while((fscanf(p,"%d",&i))!=EOF) //read an integer using getw from opened file.
{
printf("%d,",i); //prints that integer on screen
}
fclose(p); //closes the file
return 0;
}
//note: The second part (reading file), we can include in
//different file rather in same file as written here.
// But we have to know its data file name.
//i mean to say, the code would be :
/*
#include <stdio.h>
int main()
{
FILE *p; //pointer declaration
char ch;
p=fopen("data1.txt","r"); // openes the file in read only mode
while((i=getw(p))!=EOF) //read an integer using getw from opened file.
{
printf("%d,",i); //prints that integer on screen
}
fclose(p); //closes the file
return 0;
}
*/