Trouble while writing a file that can be inputed - c++

Here is my code:
void commands(){
string CMDS = "CMDS";
cin >> CMDS;
if(CMDS == "CMDS"){
cout << "CLS- Clear command line" << endl;
cout << "Logout- Logs you out of the system" << endl;
cout << "TFile- Creates a text file" << endl;
commands();
}
else if(CMDS == "CLS"){
system("cls");
commands();
}
else if(CMDS == "Logout"){
cout << "Logging out" << endl;
system("cls");
Sleep(500);
mainTitle();
enterClass();
}
else if(CMDS == "TFile"){
system("cls");
std::string textf;
cout << "--Enter Your Text--" << endl;
ofstream file_;
file_.open("Text.txt");
std::getline(std::cin, textf);
file_ << textf << endl;
file_.close();
}
else{
cout << "Incorrect choice" << endl;
commands();
}
}
I need help getting it to work, because the std::getline just closes the program right after I try to write a text file. The program is suppose to let you input text that uses spaces and creates a text file.

Related

How to remove certain lines when reading FASTA files?

I'm trying to read in the database down below into a vector of strings while removing the header lines (lines with >db) and imputing each sequence below it into a string.
This is the code I have right now:
void read_in_database_file(string db_file) {
ifstream fin;
fin.open(db_file);
if (!fin.is_open())
{//if
cerr << "Error did not open file" << endl;
exit(1);
}//if
vector<string> database;
string line = "";
while(getline(fin, line, '>'))
{
database.push_back(line);
}
finding_kmers(database);
}
int main() {
cout << "\n" << endl;
cout << "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++" << endl;
cout << "------------------------------------ BLAST -----------------------------------" << endl;
cout << "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++" << endl;
cout << endl;
cout << "Welcome to BLAST" << endl;
cout << "Please enter the name of your datafile" << endl;
string db_file = "";
getline(cin, db_file);
cout << endl;
read_in_database_file(db_file);
}
Currently, all it does is simply remove the ">" but print that line and everything else as is. How do i solve this?
database file:
>db_1
GATCTTGCATTTAGAAAATCATAAGAAATTTACTAAAAAGTATTAGGACTCATGAACAAATTTAAGAATG
TAACACTATATAAGATTGGTATACAAAAATAACTGTACTTCTTCACCAAGAAATCAAGAATCCAAAAATG
>db_2
TAGTTTGTTCTAGGATTTATGTGTTTCCTTAAAGTCTTAGTTTGATTATGTTACATTTAGCATGAGTGAC
TCCATTTTGGTTTGGTTTGGTCTGTTGGGACCTATTGCATGAGTTTAGTTCAAAACAATGGCCTCCCATA
>db_3
TTGTCCTTGCGATAGTTTACTGAGAATGATGATTTCCAATTTCATCCATATCCCTACAAAGGACATGAAC
TCATCATTTTTTATGGCTGCATAGTATTCCATGGTGTATATGTGCCATAATTTCTCAATCCAGTCTATCG

Searching for artist name and song from a file in C++

I have written some code for the search function in C++ to find artist name and song. My code doesn't seem to work. A message displays that the artist and song is found and then the menu repeats itself infinite times.
My full program code is as below.
#include "Music.h"
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
void Music::menu()
{
int input;
do {
cout << "Select an option from the menu" << endl;
cout << "1. Display records" << endl;
cout << "2. Search for records" << endl;
cout << "3. Writing records to a file" << endl;
cin >> input;
switch (input) {
case 1:
displayfile();
break;
case 2:
searchrecords();
break;
case 3:
writefile();
break;
default:
cout << "Invalid option entered. Try again" << endl;
menu();
}
} while (input != 4);
}
void Music::displayfile()
{
string line;
ifstream myfile;
myfile.open("Music.txt");
if (myfile.is_open()) {
while (getline(myfile, line)) {
cout << line << endl;
}
myfile.close();
}
else {
cout << "Unable to open file" << endl;
}
}
void Music::writefile()
{
ofstream write;
write.open("Music.txt", ofstream::app);
if (write.is_open()) {
cin.ignore();
cout << "Enter a title of song to the playlist" << endl;
cin.getline(title, 255).get();
write << " " << title;
cout << "Enter the artist of the song to playlist" << endl;
cin.getline(artist, 255).get();
write << " " << artist;
cout << "Enter the year of the song" << endl;
cin >> year;
write << "" << year;
cout << "Enter the duration of the song" << endl;
cin >> duration;
write << " " << duration;
cout << "Writing to a file is successful" << endl;
}
else {
cout << "File couldn't be open" << endl;
}
write.close();
displayfile();
}
bool Music::searchrecords()
{
bool found = true;
string search;
string line;
ifstream myfile;
myfile.open("Music.txt");
cout << "Enter the artist you want to search for: " << endl;
getline(cin, search).get();
cout << "Enter for the song you want to search for: " << endl;
getline(cin, search).get();
if (myfile.is_open()) {
while (!myfile.eof()) {
if (found) {
cout << "The artist is found" << search << endl;
cout << "The song is found" << search << endl;
return found;
}
else {
cout << "The artist is not found" << endl;
return false;
}
getline(myfile, line);
myfile.close();
displayfile();
}
}
}
Appreciate it if you could help me as I am stuck on what to do.
You have bool found = true; right in the beginning of Music::searchrecords function, so the first iteration of looking through file immediately returns.
You do getline(cin, search).get(); twice in a row, while apparently you want to read in two different string variables, one for artist, one for song
You do a loop with this if on each iteration
if (found) {
cout << "The artist is found" << search << endl;
cout << "The song is found" << search << endl;
return found;
}
else {
cout << "The artist is not found" << endl;
return false;
}
In both cases you terminate your function by calling return, so the very first iteration of looking through file returns from function (you don't read the file completely)
You call
myfile.close();
displayfile();
on each iteration, so even if you hadn't had your if right above, then your program would crash anyway, because you close file inside the loop that's iterating over it.
You don't compare line to check if you indeed found your song
In total, your program doesn't work at all, and StackOverflow isn't a forum to completely write the entire program instead of you. So you better use a debugger in your IDE to see what your program does step by step, or take a piece of paper and a pen to write down how you would accomplish your task

File opration cpp

bool UserDeatiles::loginUser(){
string vEmail,vPasswd;
UserDeatiles trm;
ifstream fin("User.txt" , std::ios::in);
if(!fin.is_open()){
//throw CustomException(fin , "File could not be opened ");
cout << "File could not be opened." << endl;
exit(1);
}
cout << "Enter the valid username : ";
cin >> vEmail;
cout << "Enter the password : ";
cin >> vPasswd;
while(fin.read((char*)&trm , sizeof(trm))){
if(vEmail.compare(trm.returnEmail()) == 0 && vPasswd.compare(trm.returnPasswd())==0){
cout << "Wellcome " << trm.name << endl;
fin.close();
//trm.clear();
return true;
}
//fin.read((char*)&trm , sizeof(trm));
}
if(fin.bad()){
cout << "Error in file operation." << endl;
exit(1);
}
cout << "Invalid Login credentials !.." << endl;
fin.close();
return false;
}
In the above login function gives the segmentation fault after it successfully return to main function in main function here is where it loginUser() returns to main
switch (choice){
case 1 : loginCheck = temp.loginUser();
if(loginCheck){
cout << "you are login successfully .!";
}else{
cout << "If you don't have any account sighup " << endl;
}
break;
I stuck here can u please help me .

How to add multiple lines to a file in C++?

I am new to C++ and write a little todo list on the console.
I am only able to add one line to a text file but when I try to add more it just won't appear on my text file.
Please take a look what I am doing wrong
//output-file stream
ofstream file;
file.open("output.txt", std::ios_base::app); //append
bool isRunning = true;
while (isRunning) {
cout << "Please select an action:" << endl;
cout << "add - adding tasks to the list" << endl;
cout << "del - deleting tasks to the list" << endl;
cout << "list - show the list" << endl;
cout << "x - to exit program" << endl;
string input;
cin >> input;
string addedTask;
if (input == "add") {
cout << "Please enter a task you like to add: " << endl;
cin.ignore();
if (std::getline(std::cin, addedTask)) {
file << addedTask << "\n";
}
else {
cout << "Failed to read line" << endl;
}
}
Why can I only add one string line? I still can't figure out the problem or am I missing something?
Did you try replacing your
file << addedTask << "\n";
by
file << addedTask << endl;
I think it should work (for me it's working)

How to rename a file in C++

The part of code where I rename the file just won't work. I tried writing it separately in another project, it works. Help me please.
#include <iostream>
#include <stdio.h>
#include <fstream>
using namespace std;
int main () {
char address[] = "";
char newname[] = "";
int action;
char confirm;
int result;
cout << "File Manipulator 1.0" << endl;
cout << "--------------------" << endl << endl;
cout << "Type the full address of a file you wish to manipulate." << endl << endl;
ADDRESS:cin >> address;
fstream file(address);
if (!file.good()) {
cout << "The selected file does not exist! Try again. ";
goto ADDRESS;
} else {
cout << endl << "-----------------------------------" << endl;
cout << "Type 1 to move the selected file." << endl;
cout << "Type 2 to rename the selected file." << endl;
cout << "Type 3 to delete the selected file." << endl;
cout << "-----------------------------------" << endl << endl;
ACTION:cin >> action;
if (action == 1) {
cout << 1;
} else if (action == 2) {
cout << "Enter the new name: ";
cin >> newname;
cout << "Are you sure you want to rename the selected file? Y/N ";
CONFIRM:cin >> confirm;
if (confirm == 'Y' || 'y') {
result = rename(address, newname);
if (result == 0) {
cout << "renamed";
} else {
perror("not renamed");
}
} else if (confirm == 'N' || 'n') {
cout << "No";
} else {
cout << "You typed an invalid command! Try again. ";
goto CONFIRM;
}
} else if (action == 3) {
cout << 3;
} else {
cout << "You typed an invalid command! Try again." << endl;
goto ACTION;
}
}
return 0;
}
BTW the whole code is not finished, so check just the renaming part. Thanks.
Well, this is the solution.
#include <iostream>
#include <cstdio>
#include <fstream>
#include <string>
using namespace std;
int main() {
string address;
string newname;
Here you can see I used strings instead of char arrays.
char input;
int action;
char confirm;
int result;
cout << "File Manipulator 1.0" << endl;
cout << "--------------------" << endl << endl;
cout << "Type the full address of a file you wish to manipulate." << endl << endl;
getline(cin, address);
ifstream myfile(address.c_str());
I used ifstream with c_str() function which passes contents of a std::string into a C style string.
// try to open the file
if (myfile.is_open())
{
When the condition is met, you must close the opened file in order to be able to manipulate/work with it later.
myfile.close();
CREATE:cout << endl << "-----------------------------------" << endl;
cout << "Type 1 to move the selected file." << endl;
cout << "Type 2 to rename the selected file." << endl;
cout << "Type 3 to delete the selected file." << endl;
cout << "-----------------------------------" << endl << endl;
cin >> action;
switch (action)
{
case 1:
{
// do nothing.
}
break;
case 2:
{
// rename file.
cout << "Enter the new name" << endl << endl;
cin.ignore();
I used here the ignore() function to ignores the amount of characters I specify when I call it.
getline(cin, newname);
cout << "Are you sure you want ot rename the selected file ? Y/N" << endl << endl;
cin >> confirm;
if (confirm == 'Y' || confirm == 'y')
{
Same case with c_str() that i explained earlier.
rename(address.c_str(), newname.c_str());
}
}
break;
case 3:
{
// delete file.
remove(address.c_str());
}
break;
default:
{
cout << "You typed an invalid command!" << endl;
}
break;
}
}
else
{
cout << "The selected file does not exist! Would you like to create it? ";
cin >> input;
If the file name you input doesn't exist, you are prompted to create a file with the specified name, then you are redirected with goto to the manipulation menu.
if (input == 'y' || input == 'Y')
{
// create the file.
ofstream output(address.c_str());
output.close();
cout << "File created";
goto CREATE;
}
}
return 0;
}
Thanks for trying anyway :)