Examples Of Files In C++
تخزين مصفوفة احادية
#include<iostream.h>
#include <fstream.h>
main()
{ int Array[80],i;
for(i=0;i<10;i++)
cin>> Array[i];
ofstream fout;
fout.open("D:\\ar.bin",ios::binary);
fout .write((char *) & Array , sizeof(Array));
fout.close(); }
قراءة محتويات مصفوفة
#include<iostream.h>
#include<iostream.h>
#include <fstream.h>
main()
{ int Array[10],i;
ifstream fout;
fout.open("D:\\ar.bin",ios::binary);
fout .read((char *) & Array , sizeof(Array));
for(i=0;i<10;i++)
cout<< Array[i]<<"\t";
fout.close(); }
لطباعة السجل الرابع فقط
#include<iostream.h>
#include<stdio.h>
#include <fstream.h>
struct password_User
{char username[20];
char password[20]; }on[6];
main()
{ifstream fout;
fout.open("D:\\up.bin",ios::binary);
fout.seekg(3*sizeof(on[0]));
fout .read((char *) & on,sizeof(on[0]));
fout.close();
cout<< "User ID :";
cout << on [0]. username;
cout<< "\tuser Password: ";
cout << on [0]. password;}
لقراءة الملفات
#include <fstream.h>
#include <iostream.h>
main()
{ char array [80];
ifstream fin;
fin.open("D:\\firstExa.txt");
while(!fin.eof())
{fin.getline(array,80);
cout<<array<<endl;}
fin.close(); }
سجل فیة عشرون طالب لكل طالب لھ اسم ملف اخر
#include<iostream.h>
#include<stdio.h>
#include<fstream.h>
struct student
{ char name[10];
int age;
int id;
int degres;
}st[20];
main()
{int i,j,sum,avg;
sum=0;
for(i=0;i<20;i++)
{cout<<"please enter the student information\n";
cout<<"enter his name\n";
gets(st[i].name );
cout<<"enter his age\n";
cin>>st[i].age;
cout<<"enter his ID adress\n";
cin>>st[i].id;
cout<<"enter his degress\n";
for(j=0;j<10;j++){
cin>>st[i].degres;
sum+= st[i].degres ;}
avg=sum/10;
sum=0;
st[i].degres=avg;}
for(i=0;i<20;i++){
if (st[i].degres>=50){ofstream fout("secses.txt",ios::binary,ios::app);
fout .write((char *) & st[i], sizeof( st[i]));
fout.close();
cout<<"this student is succes..his name is "<<st[i].name<<endl;
cout<<"his aveg is "<<st[i].degres<<endl;}
else
{ofstream fout("fail.txt",ios::binary,ios::app);
fout .write((char *) & st[i], sizeof( st[i]));
fout.close();
cout<<"this student is fail..his name is "<<st[i].name<<endl;
cout<<"his aveg is "<<st[i].degres<<endl;} }}
اقرا مصفوفة ثم اخزنھا في ملف وبعدھا استخرجھا منھ وضع اكبر عدد في ملف واصغر عدد في ملف اخر
#include <fstream.h>
#include <string.h>
#include <iostream.h>
int main()
{
char x = 's' ;
char c;
int d = 77;
int b,i,max,min;
int String1[60],Array[60];
cout<<"enter your str\n";
for( i=0;i<5;i++)
cin>>String1[i];
ofstream fout("d:\data.txt");
fout.write( (char*) &String1, sizeof(String1) );
cout << "operation completed.............\n";
fout.close();
ifstream f("d:\data.txt",ios::binary);
f.read( (char*) &Array, 5*sizeof(int) );
f.close();
max=min= Array[0];
for( i=0;i<5;i++)
{cout<<Array[i]<<endl;
if (Array[i]>max)
{max=Array[i];
ofstream maxm("d:\max.txt");
maxm.close();}
if (Array[i]<min)
{min=Array[i];
ofstream minm("d:\min.txt");
minm<<min;
minm.close();}}
return 0;
}
برنامج لطباعة اسماء مستخدمين
#include<iostream.h>
#include<stdio.h>
#include <fstream.h>
struct password_User
{char username[20];
char password[20]; }on[6];
main()
{int i;
ifstream fout;
fout.open("D:\\up.bin",ios::binary);
fout .read((char *) & on ,
sizeof(on));
fout.close();
for(i=0;i<6;i++){
cout << "\nUser ID :\t";
cout << on [i]. username;
cout << "\tuser Password: ";
cout <<on [i]. password;}}
برنامج لخزن اسماء مستخدمين
#include<iostream.h>
#include<stdio.h>
#include <fstream.h>
struct password_User
{char username[20];
char password[20];}on[6];
main()
{int i;
for(i=0;i<6;i++){
cout<<"\nenter user name: ";
gets(on[i]. username );
cout<<"enter password: ";
gets(on[i]. password);}
ofstream fout;
fout.open("D:\\up.txt");
for(i=0;i<6;i++){
fout << "User ID :";
fout << on [i]. username;
fout <<"\tuser Password: ";
fout << on [i]. password;
fout <<"\n";}
fout.close();}
برنامج لكتابة جمل على اكثر من سطر
للكتابة
#include <iostream.h>
#include <fstream.h>
int main()
{ ofstream fout;
fout.open("D:\\firstExa.txt",ios::app);
fout << "HELLOWHUSSIEN AH-ROB.\n"
<< "WELCOME YOU PROGRAM\n"
<< "WHAT DA YOU LIKE OF ME\n";
fout.close();
}
للقراءة
#include <iostream.h>
#include <fstream.h>
main()
{ char array [80];
ifstream fin;
fin.open("D:\\firstExa.txt");
while(!fin.eof())
{fin.getline(array,80);
cout<<array<<endl;}
fin.close(); }
برنامج للكتابة لا يتوقف الا اذا ادخلنا نقطة
#include <fstream.h>
#include <conio.h>
int main()
{char symbol ;
ofstream fout;
fout.open("D:\\note.txt",ios::app);
do{
symbol=getche();
fout << symbol;}
while(symbol != '.');
cout<<"\ncontent is saved";
fout.close();
}
Examples Of Files In C++
Reviewed by حامد طالب العراقي
on
11/07/2015 12:30:00 ص
Rating:
ليست هناك تعليقات: