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 7 years ago.
Improve this question
I've seen a lot of these but none of them seem to correspond to my problem. This program runs locally fine but on the server it needs to run on I run into an error.
project1.cpp: In function ‘void insertwords(char*)’:
project1.cpp:54:65: error: ‘transform’ was not declared in this scope
transform(word.begin(), word.end(), word.begin(), ::tolower);
the relevant code:
void insertwords(char *filename) {
ifstream fin;
fin.open(filename);
if(fin.fail())
{
cerr << "File opening failed. Exiting program.\n";
exit (-1);
}
string word;
int count = 0;
while (!fin.eof() ) {
word.clear();
fin >> word;
transform(word.begin(), word.end(), word.begin(), ::tolower);
for (int i = 0, len = word.size(); i < len; i++)
{
if(ispunct(word[i]))
word.erase(i--, 1);
len = word.size();
}
if(!word.empty()) {
insert_word(word);
++count;
}
}
cout << "The number of words found in the file was " << count << "\n";
fin.close();
}
Includes:
#include <iostream>
#include <string>
#include <fstream>
#include <cctype>
#include <locale>
using namespace std;
I know using namespace std; is bad practice but I was told to for the project
You need to #include <algorithm> which is the header where std::transform comes from.
As to why it would compile on one machine and not another, my guess would be that one of your other headers (e.g. <string>) includes <algorithm> in one of the compiler implementations so you got lucky, but not for the other compiler.
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 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 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();
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.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 8 years ago.
Improve this question
I am trying to read a file with full path and get each line and put them into an array. my code is like this:
#include <fstream>
#include <iostream>
using namespace std;
void main(){
int Log[200];
int i;
For(int i=0; i<30; i++)
{
getline(/var/asl/data/audit/20130502/20130502-0611/20130502-61157-UYHEZX8AAAEAAAbKRvKAAAAC, line);
Log[i] = line;
cout << Log[i] < "\n";
}
}
but the below errors come to me and I do not how to solve them. Can anyone help me?
log1.cpp:7: error: :main must return int
log1.cpp: In function int main():
log1.cpp:12: error: expected primary-expression before int
log1.cpp:12: error: expected before token
Another question I have is that if I want to search a special character that is in the line that stored in arrays,(I mean search in an array) what can I do?
Thanks a lot dear users for your reply. I tried the code and it does not have any errors. But when i run it nothing happen. My file is not in text format. it is as like as Apache server logs format. Should it be in text format? The other question is if i put these line in arrays can i search a special value in it?
Thanks for your reply in advance.
Salam ,Try this:
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
int main(){
string line;
ifstream myfile ("example.txt"); //file address
string Log[200];
int i=0;
if (myfile.is_open())
{
while ( myfile.good() )
{
getline (myfile,line);
Log[i] = line;
i++;
cout << line << endl;
}
myfile.close();
}
return 0;
}