Lecture 9
Lecture 9
;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
}
/*
; int num
{do
;num = rand() % 100
;"cout << num << "\t
;"ofp << num << "\n
;while(num != 0)}
;)(ofp.close
;)(getch
}
/*
;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
}
/*
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
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