################### Files ############## //
Write a program to store squares of numbers from 1 to 10 //
in a file called mydata.txt //
*/
void main(void)
{
;int k
;ofstream koko
;koko.open("mydata.txt")
for(k = 1 ; k <= 10 ; k++)
;"koko << k << "\t" << k*k << "\n
;)(koko.close
}
/*
Write a program to store squares of even numbers from 2 to 20 //
in a file called mydata2 //
Then, reopen the file and print its contents //
*/
void main(void)
{
;int k
;ofstream ofp
;ofp.open("mydata2.txt")
for(int k = 2 ; k <= 20 ; k=k+2)
;"ofp << k << "\t" << k*k << "\n
;)(ofp.close
;ifp ifstream
;ifp.open("mydaata2.txt")
;"cout << "Input file is opened \n if(ifp.is_open() == true)
{ cout << "Error, input file could not be opened \n"; getch(); return; } else
;int a , b
while(ifp >> a)
{
;ifp >> b
;"cout << a << "\t" << b << "\n
}
;)(ifp.close
;)(getch
}
/*
Write a program to print contents of the previous //
file mydata.txt //
*/
void main(void)
{
;ifstream ifp
;ifp.open("mydata.txt")
if(ifp == NULL)
{
;"cout << "ERROR; File can not be opened\n
;return
}
;int k , n , m
for(k = 1 ; k <= 10 ; k++)
{
;ifp >> n >> m
;"cout << n << "\t" << m << "\n
}
;)(getch
}
/*
Write a program to generate random 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 //
*/
void main(void)
{
;srand(time(NULL))
;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
;"evenp << num << "\n if(num % 2 == 0)
;"oddp << num << "\n else
;"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) //
;break if(ch == EOF) //
;++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
;++counter if(n == key)
;" " << 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
;ifp ifstream
;ofp ofstream
;ifp.open("random_numbers.txt")
;"cout << "Input file is opened \n if(ifp.is_open() == true)
{ cout << "Error, input file could not be opened \n"; return; } else
;size = 0
size++; } { cout << a << "\t" ; while(ifp >> a)
;)(ifp.close
;arr = new int[size]
;ifp.open("random_numbers.txt")
;continue for(a = 0 ; ifp >> arr[a] ; a++)
;ifp >> arr[a] for(a = 0 ; a < size ; 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
;)(getch
}
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
;ifp ifstream
;ofp ofstream
;ifp.open("random_numbers.txt")
;"cout << "Input file is opened \n if(ifp.is_open() == true)
{ cout << "Error, input file could not be opened \n"; return; } else
;size = 0
size++; } { cout << a << "\t" ; while(ifp >> a)
;)(ifp.close
;arr = new int[size]
;ifp.open("random_numbers.txt")
;a = 0
while( ifp >> temp)
{
for(b = 0 ; b < a ; b++)
;break if(temp == arr[b])
a++; } { arr[a] = temp; if(b == 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
}
/******************** End files *****************************/