Write a program to permanently store the data entered by the user. When user is done with Input, then print the confirmation message as "Content saved successfully" along with the number of character stored.
#include <bits/stdc++.h>
#include <fstream>
using namespace std;
int main(){
string input = "";
int count = 0;
cout<<"Enter a string to store:\n";
getline(cin, input);
char arr[input.length() + 1];
strcpy(arr, input.c_str());
//Convert the string to array
for (int j = 0; j < input.length(); j++){
//Exclude empty characters
if(arr[j] != ' '){
count ++;
}
}
// Create and open a text file
ofstream fp("file.txt");
// Write string to the file
fp << input;
// Close the file after writing
fp.close();
cout <<"Content saved successfully."<<endl;
cout<<"The number of characters are stored is: "<<count<<endl;
return 0;
}
Comments
Leave a comment