Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I want to test constructor's functionality and an interesting problem was encountered. After compiling that code i get the linking error LNK2019 referencing to main(). How to be able to read and copy the content of one.txt line by line in my case? I'm referring to the book "Thinking in C++" ex 7.01 but since working with Visual Studio I cannot use main(int argc, char* argv[]) version..
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
class Text
{
string text;
public:
Text();
Text(const string& name)
{
ifstream infile;
infile.open(name);
if(!infile.good())
{
cout << "File is not open";
}
else
{
string line;
while(getline(infile,line))
text = text + line + '\n';
}
};
string contetns()
{
return text;
};
};
int main()
{
Text o1;
Text o2("one.txt");
cout << "content: " << o1.contetns() << endl;
cout << "content: " << o2.contetns() << endl;
system("pause");
}
As tmaric already says, you need an empty constructor:
//Text();
Text(){};
You have to define your empty destructor.
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 months ago.
Improve this question
I am trying to write to a .txt file within a program, and I am using the following code snippet. I want to output a line of the form:
a(space)b
but I get nothing on the txt file.
#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;
int main(){
string a = "string";
int b = 0;
fstream f;
f.open("filename.txt",ios::in | ios::out );
f << a << ' ' << b << endl;
f.close();
return 0;
}
If you try this piece of code:
f.open("filename.txt", ios::in | ios::out);
if (!f.is_open()) {
cout << "error";
}
you will see that the file is never opened. ios::in requires an already existing file (std::fstream::open()):
It should work if you should only pass std::ios::out to f.open():
f.open("filename.txt", ios::out);
Read here why you shouldn't be using namespace std;.
Try this version. Few changes:
Include the correct header <string> instead of <cstring>
No using namespace std;
Use std::ofstream for output
No .open(): pass the filename in the constructor
Check if the file is valid after opening
No .close(). Let the destructor do its job.
No std::endl if '\n' is enough.
#include <iostream>
#include <fstream>
#include <string>
int main()
{
std::string a = "string";
int b = 0;
std::ofstream f("filename.txt");
if (!f) {
return EXIT_FAILURE;
}
f << a << ' ' << b << '\n';
return EXIT_SUCCESS;
}
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I recently started learning C++. I must create a program that asks the user for their name and a file name after; then displays the file name with type (eg .cpp) 1st and the name after. Must be able to accept negative values.
I'm using repl.it to write the code 1st then pasting in to a .cpp file to compile. Then will compile it with the makefile using the command make hello and check that the generated program compiles and runs correctly.
Sample run:
What's your name? John
What's the name of the output file? gen
gen.cpp:
#include <iostream>
int main() {
std::cout << "Hello John!\n";
}
Attempt:
#include <iostream>
using namespace std;
int main ()
{
string a;
cout << "What's your name?\n";
string b;
cout<< "Whats the name of this file?";
getline (cin,b);
cout<<"//"<<b;
getline (cin, a);
cout << "Hello " << a;
return 0;
}
Except it displays in the console:
What's your name?
Whats the name of this file? //e.g.c
//c
The output is wrong, so what mistake did I make? What's the correct method to get the expected values?
Edit: This is what the assignment says:
As mentioned in comments, your calls to getline() are in the wrong places. Try this instead:
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string username;
cout << "What's your name?\n";
getline (cin, username);
string filename;
cout << "Whats the name of this file?";
getline (cin, filename);
cout << "//" << filename << ".cpp" << endl;
cout << "Hello " << username << endl;
return 0;
}
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
When I ran the below program, it gave me an error:
no operator "!=" matches these operands
The error line is while (infile.get(ch) != 0).
#include <iostream>
#include <fstream>
#include <process.h>
using namespace std;
int main(int argc, char* argv[])
{
if (argc != 2)
{
cerr << "\nFormat:otype filename";
exit(-1);
}
char ch;
ifstream infile;
infile.open(argv[1]);
if (!infile)
{
cerr << "\nCan't open " << argv[1];
exit(-1);
}
while (infile.get(ch) != 0)
cout << ch;
}
istream::get() returns an isteam& reference to the stream itself. istream does not implement any operator!=, let alone one that takes an int ass input, which is why you are getting the error.
istream does, however, implement a conversion operator that you can use directly in the if. That operator returns true (or, prior to C++11, a non-null void* pointer) if the stream is not in an error state. So you can change the while statement to the following instead:
while (infile.get(ch))
cout << ch;
while (infile)
{
infile.get(ch);
cout << ch;
}
I solved with this way
and
while (infile.get(ch))
cout<<ch;
this way.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I have never used stringstream before and was given a sample code but with no explanation of what was happening in the code. If someone could explain each line's purpose that would be great. I have looked in multiple places but cant seem to pin down the second line.
#include <sstream> // i know this line includes the file
stringstream ss(aStringVariable);// this line in particular
ss >> aVariable;
getline(ss, stringVariable2HoldValue, ‘|’);
There's a constructor for std::stringstream that takes a std::string as a parameter and initializes the stream with that value.
#include <iostream>
#include <sstream>
#include <string>
int main() {
std::stringstream ss("foo bar");
std::string str1, str2;
ss >> str1 >> str2;
std::cout << "str1: " << str1 << std::endl;
std::cout << "str2: " << str2 << std::endl;
}
This code initializes a stringstream, ss, with the value "foo bar" and then reads it into two strings, str1 and str2, in the same way in which you would read from a file or std::cin.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I need to read from file line by line and print it on the screen:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
ofstream out("note.txt");
for (int i = 0; i < 10; i++)
out << i << " " << (i<<1) << "\n";
out.close();
ifstream fin;
fin.open("note.txt");
string line;
for (int i = 0; i < 10; ++i)
{
getline(fin, line);
cout << line << "\n";
}
return 0;
}
Is this approach correct? Cant I do it without a string variable (without string line in code)?
Instead of using a for loop you can use a while loop:
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
int main() {
string line;
ifstream out("note.txt");
while(getline(out, line)) {
cout << line << endl;
}
out.close();
}
If you are forced not to use strings then you can try a char buffer char buf[1024]. It must be pointed out that this approach is dangerous and error prone. If a line has more than 1024 characters then a buffer overflow will occur. Buffer overflow is the cause of many vulnerabilities and crashes. That being said, if you really have to use this method I would suggest you to be very careful by making the appropriate checks.
Copying a file verbatim is a simple as streaming out its stream buffer:
ifstream fin;
fin.open("note.txt");
std::cout << fin.rdbuf();