File Handling C++ Error - c++

I wanted write a program which allows users to write some random stuff, but i got an
error saying no matching call to which I am not able to figure it out. please Help me.
while you are trying to answer to this question, try to be more noob specific.
here is my code
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
int main()
{
string story;
ofstream theFile;
theFile.open("Random.txt");
while(cin.get(story,5000)!=EOF)
{
theFile<< story;
}
return 0;
}

cin.get with 2 arguments expects char* as first argument and you are trying to pass string as the first argument.
If you want to read std::string instead of C-string until the end of line use getline(cin, story)
If you want to read string until the next space or newline or another blank symbol use cin >> story;

You seem to be trying to write the content of cin to a file. You could just use stream operators:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
string story;
ofstream theFile;
theFile.open("Random.txt");
if(cin >> story)
{
theFile << story.substr(0, 5000);
}
return 0;
}
I am assuming that you only want the first 5000 characters in Random.txt...

Related

No newline after output (cin)

I am trying to output a string that I have previously read using cin>>input until EOF. The problem that I am facing is that after I output the string there is no new line when asking for the next string. I already tried using getline(), but the problem was that it was not skipping whitespaces. Any clues ?
#include <iostream>
#include <string>
using namespace std;
int main()
{
string input;
while (cin>>input)
{
cout<<input;
}
return 0;
}
Here is an example :
Input:test test
Output: test test
Input:test test... (Previous input remains in stdin)
Sorry in advance if this is a duplicate, but I could not find anything similar.

Hello! I am new to c++ and am trying to make a script that will print an if statement to a file

This is what I have tried so far. I am pretty sure freopen is getting caught up in the loop somehow. Thank you in advanced for your help! Sorry If I messed up the formatting. Once cin is used once, it doesn't allow for further input and just prints an identical if statement 5 times.
#include <iostream>
using namespace std;
#include <string>
int main(){
string x;
string z;
int y;
string t;
for(int i=0; i<6; i++){
cout<<"name"<<endl;
cin>>x;
cout<<"Pokemon name capatalized"<<endl;
cin>>z;
cout<<"Pokedex number"<<endl;
cin>>y;
cout<<"type"<<endl;
cin>>t;
freopen( "file.txt", "a", stdout );
cout<<"else if(pokename==\""<<x<<"\" || pokename==\""<<z<<"\" || p=="<<y<<")""{cout<<\"This is "<<z<<", \"; "<<t<<"();}"<<endl;
}
return 0;
}
If you wish to write to a file you can do so by using the ofstream class:
Here is a code snippet that you can use to write to a file:
#include<fstream>
#include<string>
using namespace std;
int main() {
string file = "myfile.txt";
ofstream out(file.c_str());
string output = "writting...";
out << output;
out.close();
}
Let me explain what this piece of code does:
The ofstream out statement declares an object of type ofstream.
The constructor takes a c_style string as an argument and therefore the method c_str() is used to convert the string class to a c_style string. Then, using the << operator and the ofstream object out I am writing to the file.

How to read names into a pointer array and output them?

Here is what I got so far:
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
int characterList = 0;
char* dynamo = new char[1000];
char* buffer = dynamo;
ifstream input("wordlist.txt");
if (input.is_open())
{
input >> dynamo[characterList];
while (input.eof())
{
characterList++;
input >> dynamo[characterList];
cout << dynamo[characterList];
}
}
else
{
cout << "File not opened" << endl;
}
return;
}
I'm a beginner so I do apologize if this looks like terrible coding practice. I created a text file with a quote from Bill Cosby that I'm trying to read one word at a time. The quote is "I don't know the key to success, but the key to failure is trying to please everybody." I'm trying to read one word at a time from a text document ignoring punctuation. I know there are a lot of questions similar to this, but they are using code that I have not learned so I'm sorry for having a repeating question. I have not learned getline (I used cin.getline) and #include <string>.
Edit: I forgot to mention, so I'm sorry for not doing so earlier, but I'm studying dynamic memory allocation which is why I'm using the new char[1000].
I'd suggest you to use std::string instead of manually allocating buffers on the heap with new[] and trying to read text manually from the file into those buffers (and don't forget to free the buffer with proper delete[] calls!).
C++ input stream classes like std::ifstream can simply read text into std::string instances thanks to a proper overload of operator<<.
The syntax is as simple as:
string word;
while (inFile >> word)
{
cout << word << endl;
}
Here's a complete compilable sample code for you to experiment and learn:
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
int main()
{
ifstream inFile("test.txt");
if (inFile.is_open())
{
string word;
while (inFile >> word)
{
cout << word << endl;
}
}
else
{
cout << "Can't open file." << endl;
}
}
This is the output I got on a test text file having the content specified in your question:
I
don't
know
the
key
to
success,
but
the
key
to
failure
is
trying
to
please
everybody.
NOTE
Of course, once you have your words read into a std::string instance, you can store them in a container like std::vector<std::string>, using its push_back() method.
I would do something like this:
#include <iostream>
#include <string>
#include <fstream>
int main() {
std::string array[6];
std::ifstream infile("Team.txt");
std::string line;
int i = 0;
while (std::getline(infile, line)) {
array[i++] = line;
}
return 0;
}
based on this answer.
Here, we assume we have to read 6 lines from the file "Team.txt". We use std:::getline() and we put inside a while so that we read all the file.
At every iteration, line holds the current line of the file read. Inside the body we store it in array[i].

Getline only printing out last line when called multiple times

So this is a fairly simple example of a program where I'm trying to output the first two lines of an input text file. The ifstream should be a global variable, and the testGetFile() function is necessary (I have not done the actual text processing needed in this code.) I'm trying to figure out why this is cout-ing only the SECOND line of the input file. Any help will be appreciated!
Thanks in advance!
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
using namespace std;
ifstream input;
string testGetFile(){
string result;
getline(input,result);
return result;
}
int main(){
input.open("testInput.txt");
cout<< testGetFile();
cout<< testGetFile();
return 0;
}

Reading strings and integers from .txt file and printing output as strings only

I'm new to C++, and I'm trying to write a short C++ program that reads lines of
text from a file, with each line containing one integer key and one alphanumeric string value (no embedded whitespace). The number of lines is not known in advance, (i.e., keep reading lines until end of file is reached). The program needs to use the 'std::map' data structure to store integers and strings read from input (and to associate integers with strings). The program then needs to output string values (but not integer values) to standard output, 1 per line, sorted by integer key values (smallest to largest). So, for example, suppose I have a text file called "data.txt" which contains the following three lines:
10 dog
-50 horse
0 cat
-12 zebra
14 walrus
The output should then be:
horse
zebra
cat
dog
walrus
I've pasted below the progress I've made so far on my C++ program:
#include <fstream>
#include <iostream>
#include <map>
using namespace std;
using std::map;
int main ()
{
string name;
signed int value;
ifstream myfile ("data.txt");
while (! myfile.eof() )
{
getline(myfile,name,'\n');
myfile >> value >> name;
cout << name << endl;
}
return 0;
myfile.close();
}
Unfortunately, this produces the following incorrect output:
horse
cat
zebra
walrus
If anyone has any tips, hints, suggestions, etc. on changes and revisions
I need to make to the program to get it to work as needed, can you please
let me know?
Thanks!
See it:
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main()
{
string name;
int value;
ifstream myfile("text.txt", ifstream::in);
while(myfile >> value >> name)
cout << name << endl;
return 0;
}
You are having problems because you attempt to read each line twice: first with getline and then with operator>>.
You haven't actually used std::map in any regard, at all. You need to insert the integer/string pair into the map, and then iterate over it as the output. And there's no need to close() the stream.
Instead of using "! myfile.eof()" use this code it will help.
ifstream is;
string srg;
is.open(filename);
while(getline(is,srg))
{//your code
}