C++: Files - Applications solved


1) It reads a file "TEST" which contains integers separated by space or ENTER. Display the maximum and minimum of the file.
#include <iostream>
using namespace std;
#include<fstream.h>
#include<values.h>

void main()
{
	ifstream f("C:\\test.txt");
 
	/*
	ifstream f;
	f.open("number.txt");
	*/
 
	if(!f) //or f=NULL
	{
		cout<<"Error opening file!"<<endl;
		return;
	}
	int x,max=-MAXINT,min=MAXINT;
 
	f.seekg(0,ios::beg); //position ourselves at the top of file
	while(!f.eof())
	{
	f>>x;
	if (max<x)
		max=x;
	else if (min>x)
		min=x;
	}
 
	f.close();
	cout<<"Maximum is: "<<max<<endl;
	cout<<"Minimum is: "<<min<<endl;
 
	int k;
        cin>>k;
}
2) It reads a text file containing integers separated by space or ENTER. Required to calculate and display the sum of all numeric values from file and the number of elements strictly negative.
#include<iostream.h>
using namespace std;
#include<fstream.h>
#include<values.h>

void main()
{
	ifstream f("C:\\test.txt"); //or: fstream f("C:\\test.txt",ios::in);
	if(!f) //or f=NULL
	{
		cout<<"Eroare la deschiderea fisierului!"<<endl;
		return;
	}
 
	int x[100], n_neg=0, s=0, i;
	f.seekg(0,ios::beg); //position ourselves at the top of file
	i=0;
	while(!f.eof())
	{
		f>>x[i];
		if(x[i]<0)
			n_neg++;
		s+=x[i++];
	}
 
	f.close();
	cout<<"The number of negative elements: "<<n_neg<<endl;
	cout<<"The sum of all elements: "<<s<<endl;
	
	int k;
        cin>>k;
}
3) It reads a file "TEXT" containing integers separated by space or ENTER. To display the maximum and minimum of the file. Will define a class for integers in the file.
#include<iostream.h>
using namespace std;
#include<fstream.h>
#include<values.h>

class number
{
	int n;
	public:
	int& retur();
	friend istream& operator>>(istream&,number&);
};
int& number::retur()
{
	return n;
}
istream& operator>>(istream& a,number& b)
{
	a>>b.n;
	return a;
}
void main()
{
	ifstream f("C:\\test.txt ");
	if(!f)
	{
		cout<<"Error opening file!"<<endl;
		return;
	}
 
	number x;
	int max=-MAXINT;
	int min=MAXINT;
	f.seekg(0,ios::beg);
	while(!f.eof())
	{
	f>>x;
	if (max<x.retur())
		max=x.retur();
	else if (min>x.retur())
		min=x.retur();
	}
 
	cout<<"Minimum is: "<<min<<endl;
	cout<<"Maximum is: "<<max<<endl;
 
	int k;
        cin>>k;
}
4) Reads a string from the keyboard until meeting character "$". After reading the data written to a text file and take the file in question and makes each voice count is in the file.
#include<iostream.h>
#include<fstream.h>
#include<stdio.h>
using namespace std;
void main()
{
	char c;
	int a=0,e=0,i=0,o=0,u=0;
	ofstream f("C:\\test.txt");
 
	if(!f)
	{
		cout<<"Error opening."<<endl;
	}
 
	do{
		c=getchar();
		switch(c)
		{
			case 'a': a++; break;
			case 'e': e++; break;
			case 'i': i++; break;
			case 'o': o++; break;
			case 'u': u++;
		}
 
		if(c!='$')
		f.put(c); //would work and f<<c;
	}while(c!='$');
 
	f.close();
	ofstream g("C:\\test.txt");
	if(!g)
	{
		cout<<"Error opening file of reading."<<endl;
	}
 
	g<<"Vowel a is found  : "<<a<<" times.\n";
	g<<"Vowel e is found  : "<<e<<" times.\n";
	g<<"Vowel i is found  : "<<i<<" times.\n";
	g<<"Vowel o is found  : "<<o<<" times.\n";
	g<<"Vowel u is found  : "<<u<<" times.\n";
	g.close();
	cout<<"The operation counting vowels succeeded!"<<endl;
 
	int k;
        cin>>k;
 }
5) Sum up the numeric values ​​from a text file.
#include<iostream.h>
#include<string.h>
#include<stdlib.h>
#include<fstream.h>
#include<stdio.h>
using namespace std;
 
void main()
{
	char a[255], separator[]=" ,",digits[]="0123456789.+-",*p;
	double s=0;
	fstream f("C:\\test.txt",ios::in);
 
	if(!f)
	{
		cout<<"Failed to open file."<<endl;
		return;
	}
 
	while(!f.eof())
	{
		f.getline(a,255,'\n');
		p=strtok(a,separator); //separate string using space or comma as a separator
		while (p)
		{
			if (strspn(p,digits)==strlen(p))
			s+=atof(p);
			cout<<p<<"\n";
			p=strtok(NULL,separator);
		}
	}
 
	cout<<"The sum is: "<<s<<endl;
	
	int k;
        cin>>k;
}
Cookies help us deliver our services. By using our services, you agree to our use of cookies.