Showing posts with label Simple C plus plus Programs. Show all posts
Showing posts with label Simple C plus plus Programs. Show all posts

Friday, January 22, 2016

C Plus Plus Program to Replace a specified Line in a Text File - cppexamples

Leave a Comment
Questions:

C Plus Plus Program to Replace a specified Line in a Text File

Explanation:

This C Plus Plus Program to Replace a specified Line in a Text File,

Code:

#include<iostream>
#include<fstream>
#include<string>
using namespace std;
void main() {

       fstream file;//creating txt file
       file.open("Reading.txt",ios::in); //opening file in reading mod Reading.txt file is located in your project foler
       int temp2 = 0;
       cout << "Data In Reading File" << endl;
       while (file) {
              string temp;
              getline(file, temp);
              temp2++;
       }
       cout << endl << endl;
       string *data = new string[temp2];
       temp2 = 0;
       file.close();
       file.open("Reading.txt", ios::in);
       cout << endl << endl << endl;
       while (file) {
              string temp;
              getline(file, temp);
              cout <<temp2+1<<"  "<< temp << endl;
              data[temp2] = temp;
              temp2++;
       }
       file.close();

       //writing data on file
       string data2;
       string data3;
       cout << "Enter Data that you want to replace == ";
       getline(cin, data2);
       cout << "Enter Data that you want to replace with == ";
       getline(cin, data3);
       file.open("Reading.txt", ios::out);
       for (int i = 0; i < temp2; i++) {
              if (data[i] == data2) {
                     data[i] = data3;
                     file << data[i] << endl;
              }
              else
                     file << data[i] << endl;
       }
       file.close();

       //printing just data
       file.open("Reading.txt", ios::in);
       temp2 = 0;
       while (file) {
              string temp;
              getline(file, temp);
              cout << temp2 + 1 << "  " << temp << endl;
              temp2++;
       }

}


Output:

C Plus Plus Program to Replace a specified Line in a Text File - cppexamples


Related Articles:


C Plus Plus Program to Create a File & Store Information

C Plus Plus Program to Illustrate Reading of Data from a File
Read More

C Plus Plus Program Delete a specific Line from a Text File - cppexamples

Leave a Comment
Questions:

C Plus Plus Program Delete a specific Line from a Text File

Explanation:

This C Plus Plus Program delete a specific line from a text file.Here is source code of the C Plus Plus Program to delete a specific line from a text file. The C program is successfully compiled and run on a Visual Studio 2015. The program output is also shown below.

Code:

#include<iostream>
#include<fstream>
#include<string>
using namespace std;
void main() {

       fstream file;//creating txt file
       file.open("Reading.txt",ios::in); //opening file in reading mod Reading.txt file is located in your project foler
       int temp2 = 0;
       cout << "Data In Reading File" << endl;
       while (file) {
              string temp;
              getline(file, temp);
              temp2++;
       }
       cout << endl << endl;
       string *data = new string[temp2];
       temp2 = 0;
       file.close();
       file.open("Reading.txt", ios::in);
       cout << endl << endl << endl;
       while (file) {
              string temp;
              getline(file, temp);
              cout <<temp2+1<<"  "<< temp << endl;
              data[temp2] = temp;
              temp2++;
       }
       file.close();

       //writing data on file
       string data2;
       cout << "Enter Data that you want to delete == ";
       getline(cin, data2);
       file.open("Reading.txt", ios::out);
       for (int i = 0; i < temp2; i++) {
              if (data[i] != data2) {
                     file << data[i] << endl;
              }
       }
       file.close();

       //printing just data
       file.open("Reading.txt", ios::in);
       temp2 = 0;
       while (file) {
              string temp;
              getline(file, temp);
              cout << temp2 + 1 << "  " << temp << endl;
              temp2++;
       }

}

Output:

C Plus Plus Program Delete a specific Line from a Text File - cppexamples


Related Articles:


C Plus Plus Program to Create a File & Store Information

C Plus Plus Program to Illustrate Reading of Data from a File

Read More

How to count number of lines in text file in C++ - cppexamples

Leave a Comment


Questions:

How to count number of lines in text file in C++

Explanation:

In this program you will learn how to count number of line in a text file.


Code:

#include<iostream>
#include<fstream>
#include<string>
using namespace std;
void main() {

       fstream file;//creating txt file
       file.open("Reading.txt",ios::in); //opening file in reading mod Reading.txt file is located in your project folder
       int temp2 = 0; //counter
       cout << "Data In Reading File" << endl;
       while (file) {
              string temp;
              getline(file, temp);
              cout << temp << endl;
              temp2++;
       }
       cout << "Number of Lines == " << temp2<<endl;

}


Output:

How to count number of lines in text file in C++
Reading.txt



How to count number of lines in text file in C++
Code Output


Related Articles:

C Plus Plus Program to Create a File & Store Information

C Plus Plus Program to Illustrate Reading of Data from a File


Read More

C Plus Plus Program to Illustrate Reading of Data from a File -- cppexamples

Leave a Comment
Questions:

C Plus Plus Program to Illustrate Reading of Data from a File

Explanation:

This C Program illustrates reading of data from a file. The program opens a file which is present. Once the file opens successfully, it uses ifstream library call to read the content.

ifstream: This data type represents the input file stream and is used to read information from files.



Code:

#include<iostream>
#include<fstream>
#include<string>
using namespace std;
void main() {
       ifstream file("Temp.txt");//creating txt file
       while (file) {
              string temp;
              getline(file, temp);
              cout << temp << endl;
       }
       file.close();

}

Output:


Related Articles :

C Plus Plus Program to Create a File & Store Information
Read More

Sunday, August 9, 2015

Kelvin to Celsius Converter

Leave a Comment

Temperature Scale Ranges

°C: degree Celsius (centigrade), °Re: Réaumur, °F: degree Fahrenheit, K: Kelvin, °Ra: Rankine
Scale Factor°C°Réaumur°FK°Rankine
Boiling point of water
at 1 atmosphere
10080212373.15671.67
Freezing point of water
at 1 atmosphere
0032273.15491.67




Questions:

Kelvin to Celsius Converter

Code:

// Kelvin to Celsius Converter
#include<iostream>
using namespace std;

//Prototype
float Kelvin_to_Celsius(float);
void main()
{
       //Title
       cout<<"Kelvin to Celsius Converter by cppexamples.blogspot.com"<<endl<<endl;
       float Kelvin=0;
       cout<<"Enter Kelvin = ";
       cin>>Kelvin;
       cout<<"Celsius === "<<Kelvin_to_Celsius(Kelvin)<<endl;
}

//Defination
float Kelvin_to_Celsius(float Kelvin){
return Kelvin*0.8;
}

Output
Kelvin to Celsius Converter
Kelvin to Celsius Converter

Read More

Celsius to Réaumur Converter

Leave a Comment

Temperature Scale Ranges

°C: degree Celsius (centigrade), °Re: Réaumur, °F: degree Fahrenheit, K: Kelvin, °Ra: Rankine
Scale Factor°C°Réaumur°FK°Rankine
Boiling point of water
at 1 atmosphere
10080212373.15671.67
Freezing point of water
at 1 atmosphere
0032273.15491.67

Questions:

Celsius to Réaumur Converter

Code:

// Celsius to Réaumur Converter
#include<iostream>
using namespace std;

//Prototype
float Celsius_to_Réaumur(float);
void main()
{
       //Title
       cout<<"Celsius to Réaumur Converter by cppexamples.blogspot.com"<<endl<<endl;
       float Celsius=0;
       cout<<"Enter Celsius = ";
       cin>>Celsius;
       cout<<"Réaumur === "<<Celsius_to_Réaumur(Celsius)<<endl;
}

//Defination
float Celsius_to_Réaumur(float Celsius){
return Celsius*0.8;
}





Output
Celsius to Réaumur Converter
Celsius to Réaumur Converter

Read More

Celsius to Rankine Converter

Leave a Comment
Questions:

Celsius to Rankine Converter

Code:

// Celsius to Rankine Converter
#include<iostream>
using namespace std;

//Prototype
float Celsius_to_Rankine(float);
void main()
{
       //Title
       cout<<"Celsius to Rankine Converter by cppexamples.blogspot.com"<<endl<<endl;
       float Celsius=0;
       cout<<"Enter Celsius = ";
       cin>>Celsius;
       cout<<"Rankine === "<<Celsius_to_Rankine(Celsius)<<endl;
}

//Defination
float Celsius_to_Rankine(float Celsius){
return Celsius*1.8 + 32 + 459.67;
}




Output
Celsius to Rankine Converter
Celsius to Rankine Converter

Read More

Celsius to Kelvin Converter

Leave a Comment
Questions:

Celsius to Kelvin Converter

Code:

// Celsius to Kelvin Converter by cppexamples.blogspot.com

#include<iostream>
using namespace std;

//Prototype
float Celsius_to_Kelvin(float);
void main()
{
       //Title
       cout<<"Celsius to Kelvin Converter by cppexamples.blogspot.com"<<endl<<endl;
       float Celsius=0;
       cout<<"Enter Celsius = ";
       cin>>Celsius;
       cout<<"Kelvin === "<<Celsius_to_Kelvin(Celsius)<<endl;
}

//Defination
float Celsius_to_Kelvin(float Celsius){
return Celsius+273.5;
}


Output


Read More