0% found this document useful (0 votes)
20 views9 pages

Files

The document contains multiple C++ programs that demonstrate file handling operations such as writing squares of numbers to a file, reading data from files, generating random numbers, sorting, and filtering odd/even numbers. Each program is structured to perform specific tasks like storing, reading, and processing data from various text files. The examples illustrate basic file I/O concepts and manipulation of numerical data in C++.

Uploaded by

farhafouadd
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views9 pages

Files

The document contains multiple C++ programs that demonstrate file handling operations such as writing squares of numbers to a file, reading data from files, generating random numbers, sorting, and filtering odd/even numbers. Each program is structured to perform specific tasks like storing, reading, and processing data from various text files. The examples illustrate basic file I/O concepts and manipulation of numerical data in C++.

Uploaded by

farhafouadd
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Files

//Write a program to store squares of numbers


from 1 to 10
//in a file

void main(void)
{
​ int k;
​ ofstream ofp;
​ ofp.open("mosalah.txt");
​ for(k = 1 ; k <= 10 ; k++)
​ ​ ofp << k << "\t" << k*k << "\n";
​ ofp.close();
}

Write a program to read a file mydata


//then, print the data inside it

void main(void)
{
​ ifstream​ ifp;
​ ifp.open("mydata.txt");
​ if(ifp.is_open() == true)​ ​ ​ cout << "Input file is opened \n";
​ else​ ​ ​ ​ ​ { cout << "Error, input file could not be
opened \n"; return; }
​ int a , b;
​ while(ifp >> a)
​ {
​ ​ ifp >> b;
​ ​ cout << a << "\t" << b << "\n";
​ }
​ getch();
}
Write a program to store squares of even
numbers from 6 to 40
//in a file called salah
//Then, reopen the file and print its contents

void main(void)
{
​ int k;
​ ofstream toto;
​ toto.open("salah.txt");
​ for(int k = 6 ; k <= 40 ; k=k+2)
​ ​ toto << k << "\t" << k*k << "\n";
​ toto.close();
}
*/
/*
void main(void)
{
​ ifstream ifp;
​ ifp.open("salah.txt");
​ if(ifp.is_open() == true)​ ​ ​ cout << "Input file is opened \n";
​ else​ { cout << "Error, input file could not be opened \n"; getch();​ ​
return; }
​ int a , b;
​ while(ifp >> a)
​ {
​ ​ ifp >> b;
​ ​ cout << a << "\t" << b << "\n";
​ }
​ getch();
}

// Write a program to print contents of the


previous
//file squares.txt
/*
void main(void)
{
​ FILE *pf2;
​ pf2 = fopen("salah.txt","r");
​ if(pf2 == NULL)
​ {
​ ​ cout << "ERROR; File can not be opened\n";
​ ​ return;
​ }

​ int k , n , m;
​ for(k = 1 ; k <= 9 ; k++)
​ {
​ ​ fscanf(pf2,"%d %d ",&n,&m);
​ ​ printf("%d \t %d \n",n,m);
​ }
​ fclose(pf2);
}
*/

//Write a program to generate random


unrepeated numbers with values less
//than 100. Store the generated numbers in a
file called
//"random_numbers.txt". If the generated
number is equals
//to zero, stop the program.
/*
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <ctime>

using namespace std;

int main() {
srand(time(NULL));
int nums[99];

// 1. 99 ‫ إلى‬1 ‫نمأل المصفوفة من‬


for (int i = 0; i < 99; i++) {
nums[i] = i + 1;
}

// 2. ‫ نعمل‬shuffle (‫)تبديل عشوائي‬


for (int i = 0; i < 99; i++) {
int j = rand() % 99;
// ‫تبادل العناصر‬
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}

// 3. ‫نكتب األرقام في الملف‬


ofstream outFile("random_numbers.txt");
for (int i = 0; i < 99; i++) {
outFile << nums[i] << "\n";
cout << nums[i] << "\t";
}
outFile.close();

return 0;
}

If repeated
void main(void)
{
​ srand(time(NULL));
//​ srand(3);
​ ofstream ofp;
​ ofp.open("random_numbers.txt");

​ int num ;
​ do{
​ ​ num = rand() % 100;
​ ​ cout << num << "\t";
​ ​ ofp << num << "\n";
​ }while(num != 0);
​ ofp.close();
​ getch();
}

//Write a program to read integer numbers from


a file called
//"random_numbers.txt" store odd numbers in
a file called
//"odd_numbers.txt" and even numbers in
//another file called "even_numbers.txt".
/*
void main(void)
{
​ ofstream oddp , evenp;
​ ifstream ifp;
​ ifp.open("random_numbers.txt");
​ oddp.open("odd_numbers.txt");
​ evenp.open("even_numbers.txt");
​ int num;
​ while(ifp)
​ {
​ ​ ifp >> num ;
​ ​ if(num % 2 == 0)​ ​ evenp << num << "\n";
​ ​ else​ ​ ​ ​ ​ ​ ​ oddp << num << "\n";
​ ​ cout << num << "\t";
​ }
​ ifp.close();
​ oddp.close();
​ evenp.close();
​ getch();
}

//Write a program to copy a file calld


"random_numbers.txt"
//into another file called "mydata.txt"
/*
void main(void)
{
​ FILE *pf1 , *pf2;
​ char ch;
​ pf1 = fopen("random_numbers.txt","r");
​ if(pf1 == NULL)
​ {
​ ​ printf( "ERROR; random_numbers.txt file can not be opened\n");
​ ​ return;
​ }
​ pf2 = fopen("mydata.txt","w");
​ if(pf2 == NULL)
​ {
​ ​ printf("ERROR; mydata.txt file can not be opened\n");
​ ​ return;
​ }

​ while((ch = getc(pf1)) != EOF )


​ {
//​ ​ ch = getc(pf1);
//​ ​ if(ch == EOF)​ ​ break;
//​ ​ ch++;
​ ​ putc(ch,pf2);
​ ​ cout << ch;
​ }

​ fclose(pf1);
​ fclose(pf2);
}
*/

//Write a program to read a key number.


//Count the number of times this key
//number exists in a file called
"odd_numbers.txt"
/*
void main(void)
{
​ ifstream ifp("odd_numbers.txt");
​ int key , n , counter = 0 ;
​ if(!ifp)
​ {
​ ​ printf( "ERROR; random_numbers.txt file can not be opened\n");
​ ​ return;
​ }
​ cout << "Enter key number: ";
​ cin >> key;
​ while(ifp)
​ {
​ ​ ifp >> n;
​ ​ if(n == key)​ counter++;
​ ​ cout << n << " ";
​ }
​ cout << "\n The number " << key << " is found " << counter << " times \n";
​ ifp.close();
​ getch();
}

//Write a program to read numbers from a file


called
//"random_numbers.txt"
//sort these numbers, store the numbers after
sorting in a
//file called sorted_random_numbers.txt
/*
void main(void)
{
​ int a , b , size , *arr , temp;
​ ifstream​ ifp;
​ ofstream​ ofp;
​ ifp.open("random_numbers.txt");
​ if(ifp.is_open() == true)​ ​ ​ cout << "Input file is opened \n";
​ else​ { cout << "Error, input file could not be opened \n"; return; }
​ size = 0;
​ while(ifp >> a)​ { cout << a << "\t" ;​ size++; }
​ ifp.close();
​ arr = new int[size];
​ ifp.open("random_numbers.txt");
​ for(a = 0 ; ifp >> arr[a] ; a++)​ ​ ​ continue;
//​ for(a = 0 ; a < size ; a++)​ ​ ​ ifp >> arr[a];
​ ifp.close();

​ for(a = 0 ; a <= size -2 ; a++)


​ ​ for(b = a+1 ; b <= size-1 ; b++)
​ ​ ​ if(arr[a] > arr[b])
​ ​ ​ { temp = arr[a] ; arr[a] = arr[b] ; arr[b] = temp ; }

​ cout << "\n\n";


​ ofp.open("sorted_random_numbers.txt");
​ for(a = 0 ; a < size ; a++)
​ {​ ​
​ ​ ofp << arr[a] << "\n";
​ ​ cout << arr[a] << "\t";
​ }
​ ofp.close();
}
*/

//Write a program to read numbers from a file


called
//"random_numbers.txt"
//delete repeated numbers, store the numbers
after deleting in a
//file called unrepeated_data.txt
void main(void)
{
​ int a , b , size , *arr , temp;
​ ifstream​ ifp;
​ ofstream​ ofp;
​ ifp.open("random_numbers.txt");
​ if(ifp.is_open() == true)​ ​ ​ cout << "Input file is opened \n";
​ else​ { cout << "Error, input file could not be opened \n"; return; }

​ size = 0;
​ while(ifp >> a)​ { cout << a << "\t" ;​ size++; }
​ ifp.close();
​ arr = new int[size];
​ ifp.open("random_numbers.txt");

​ a = 0;
​ while( ifp >> temp)
​ {​ ​
​ ​ for(b = 0 ; b < a ; b++)
​ ​ ​ if(temp == arr[b])​ ​ break;
​ ​ if(b == a)​ { arr[a] = temp;​ ​ a++; }
​ }
​ ifp.close();

​ cout << "\n\n";


​ ofp.open("unrepeated_random_numbers.txt");
​ for(b = 0 ; b < a; b++)
​ {​ ​
​ ​ ofp << arr[b] << "\n";
​ ​ cout << arr[b] << "\t";
​ }
​ ofp.close();
}

You might also like