Why wont filestream write whitespaces into files cpp? [duplicate] - c++

This question already has answers here:
How to cin Space in c++?
(8 answers)
Closed 7 years ago.
i have a cpp code
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
int main()
{
string s,s1;
ofstream f("new.txt");
cin>>s;
f<<s;
f.close();
ifstream f1("new.txt");
while(!f1.eof())
{
f1>>s1;
cout<<s1;
}
return 0;
}
if i give the input string as "MAGICAL STRING WITH SPACES" . The ofstream object only writes "MAGICAL" into the textfile, what do i do to write and read whitespaces from string variables into an output file stream ?

This is because you use cin.
Try getline() instead!
string newArgument = "";
getline(cin, newArgument);

Related

Read string with spaces in C++ [duplicate]

This question already has answers here:
Why does std::getline() skip input after a formatted extraction?
(5 answers)
Closed 4 years ago.
In this C++ code:
#include<bits/stdc++.h>
using namespace std;
int main()
{
string S;
getline(cin,S);
cout<<S;
return 0;
}
It prints the string with spaces, eg:
Input:
abc def
Output:
abc def
However, in this code:
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n;
cin>>n;
string S;
getline(cin,S);
cout<<S;
return 0;
}
It does not print any output, eg:
Input:
1
abc def
Output:
How is that even possible? Please help. Where am I going wrong?
You read a number and then a line. Think carefully about what is going on, a newline is not part of a number. So if the input is 123 newline, you read the number 123 and then getline reads the newline. Whatever is on the next line hasn't been read at all.

How to take variable input from stdin in C++ [duplicate]

This question already has answers here:
How to read from std::cin until the end of the stream?
(2 answers)
How to break loop by Enter (c++)?
(3 answers)
End of File(EOF) of Standard input stream (stdin)
(6 answers)
Closed 5 years ago.
I was trying to take input variable number of strings and numbers.Found this solution link:
I tried this for numbers:
#include<iostream>
using namespace std;
int main(){
int np;
while (cin>>np){
cout << np<<endl;
}
return 0;
}
for strings:
#include<iostream>
#include<string>
using namespace std;
int main(){
string line;
while (getline(cin, line)){
cout << line <<endl;
}
return 0;
}
But,when I run the code,even if I just press enter it doesn't come out of the loop.The loop should terminate if just an enter key is pressed,but that does not happen.
Please provide suggestions how to achieve this functionality.
You can write
while (std::getline(std::cin, line) && !line.empty()) {
// ...
}
Only keep looping when the fetched string is nonempty.

how to open file by passing its name in variable in c++ linux [duplicate]

This question already has answers here:
I can't open variable file name
(3 answers)
Closed 6 years ago.
#include<iostream>
#include<conio.h>
#include<fstream>
#include<string>
#include<string.h>
#include<vector>
using namespace std;
void main()
{
string read;
string name;
vector<string> myvec;
const char * word;
ifstream in;
in.open("w.txt");
ofstream out;
getline(in,read);
word = strtok(&read[0],",");
while(word != NULL)
{
name = word;
cout<<"Word: "<<word<<endl;
name = name + ".txt";
myvec.push_back(name);
out.open(name);
cout<<"Name: "<<name<<endl;
word = strtok(NULL,",");
out.close();
}
in.close();
system("pause");
}
The problem I am having is when i run it in ubuntu terminal it throws an error, which generally says that out.open() will not take string name.
I have to create different files taking input from a file. So is there a way to resolve the issue?
In C++03, ofstream::open() takes a const char*, not a std::string; see http://www.cplusplus.com/reference/fstream/ofstream/open/. If you can use C++11 (gcc -std=c++11), you can pass a std::string.

Writing to a file, compiles, creates a file but doesn't write [duplicate]

This question already has answers here:
Why does std::getline() skip input after a formatted extraction?
(5 answers)
Closed 7 years ago.
I've read the answers to the "The Questions that may already have your answer" but it didn't help me.
I suppose my problem has a quick solution.
I want to write a program in which
The user inputs the name of the file they want to create.
then
In the same line they input an integer amount=the number of lines of the text of the newly created txt file.
In a new line the user input the first line of the text, in a new line the next line and so on.
And all those lines are saved to the created file.
For example file.txt 3
bhjudshsu 565 jdd
hcxjs
jdckisa jsdjs
And after opening the file named file.txt I should see the above three lines of the text.
Here is my code sample:
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
int main()
{
int amount;
string fileName, line;
ofstream start;
cin>>fileName>>amount;
ofstream out(fileName.c_str());
for(int i=0; i<amount; i++)
{
getline(cin, line);
start<<line<<endl;
}
start.flush();
start.close();
return 0;
}
The problem is that the file is created but it is empty.
Moreover, if I input file.txt 3 the program is closed after I input only two lines.
Could you tell me what I am doing wrong?
You have two ostreams, start and out. You attach out to the file (and write nothing to it), and write to start (which is attached to nothing).
In addition to Beta's answer, cin>> will leave newlines in the buffer when the user presses enter. getline() reads this as the user having pressed enter to "skip" the input.
You can use cin.ignore() to get rid of those extra characters before using getline().
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
int main()
{
int amount;
string fileName, line;
cin >> fileName >> amount;
ofstream start(fileName.c_str());
for(int i = 0; i < amount; i++)
{
cin.ignore();
getline(cin, line);
start << line << endl;
}
start.flush();
start.close();
return 0;
}

How to store a complete sentence with white spaces in a string in cpp? [duplicate]

This question already has answers here:
tell cin to stop reading at newline
(6 answers)
equivalent of Console.ReadLine() in c++
(3 answers)
Closed 7 years ago.
I want to store "What is wrong with you?" in a string str in cpp but when I run this code it only stores "What" in ·str·.
How can I store the complete sentence in the string.
#include<iostream>
#include<string.h>
using namespace std;
int main()
{
string str;
cin>>str;
cout<<str;
}
Input:
What is wrong with you?
Output:
What
was already answered. But here is the solution:
std::string str;
std::getline( std::cin, str);