writing data from right to left in file? - c++

instead of saving data and then reversing it i wanted to know if there is any function that can save the output to the file directly from right to left?
for example if i have this simple code:
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
ofstream fout("filepath");
fout<<"stackoverflow");
return 0;
}
instead of having :
stackoverflow
i want to have:
wolfrevokcats
*this example is just an example don't be mad at me :D

You can reverse the string before writing it. That's the only way I know how to do it because you can't write from "right" to "left"
#include <iostream>
#include <string>
#include <algorithm>
int main(int argc, char **argv) {
std::string thing("my thing");
std::reverse(thing.begin(), thing.end());
std::cout << thing << std::endl;
return 0;
}

Related

How to print Content of currently Executing file C++?

How to print all the content of the currently executing file from in itself in C++?
#include <iostream>
using namespace std;
int main() {
// some code that print current file.
}
expected output:
#include <iostream>
using namespace std;
int main() {
// some code that print current file.
}
there can be many ways possible, easy one.
#include <iostream>
using namespace std;
int main() {
string more = "more ";
more.append(__FILE__);
system(more.c_str());
}
Output:
#include <iostream>
using namespace std;
int main() {
string more = "more ";
more.append(__FILE__);
system(more.c_str());
}

C++ getting rid of empty string rows

Hi so this code worked 5 minutes ago to do exactly what I needed it to do.
The data is:
Set Field [G],Sheet Resistivity (Gavg) [ohm/sqr]
0.0000E+0,
0.0000E+0,7.270620E+2
1.0000E-2,
1.0000E-2,7.271280E+2
-1.0000E-2,
-1.0000E-2,
-1.0000E-2,7.271290E+2
And my code for it is:
#include <iostream>
#include <fstream>
using namespace std;
int main(){
ifstream ip("/Users/10Exahertz/Documents/Hall Data/Test/data.txt");
if(!ip.is_open()) std::cout << "ERROR: File Open" << '\n';
string x;
string y;
while(getline(ip,x,',')){
getline(ip,y,'\n');
if(y!="")
std::cout <<x<<","<< y << '\n';
}
ip.close();
}
Like I said 5 mintues ago this worked, it got rid of the rows with an empty y string and all was good. But then I went back to the original data file and it didnt work there. I was confused so I put the original data into data.txt and now that one is not working either. Im honestly confused, but what would be the best condition in that if loop to make it so this works.
It sounds like you may have some whitespace that's crept in. I'd use the solution from this answer to trim the whitespace from your y string just to be sure:
#include <iostream>
#include <algorithm>
#include <cctype>
#include <locale>
#include <fstream>
using namespace std;
// trim from start (in place)
static inline void ltrim(std::string &s) {
s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](int ch) {
return !std::isspace(ch);
}));
}
int main() {
[...]
while(getline(ip,x,',')){
getline(ip,y,'\n');
ltrim(y);
if(y!="")
std::cout <<x<<","<< y << '\n';
}
}

Vectors and Strings C++

#include <vector>
#include <iostream>
#include <cstring>
#include <string.h>
using namespace std;
int main()
{
vector<string> test;
test.push_back("yasir");
test.push_back("javed");
for(int i=0; i!=test.end();i++)
{
cout << test[i];
}
}
Why is this code giving up an error? I am unable to identify the cause of the error.
Error: No Match for operator !=....
First of all, you are trying to compare int with the iterator of vector.
for(int i=0; i!=test.end();i++)
{
cout << test[i];
}
Here, the test.end() returns the iterator. There is no overloaded operator!= which can compare integer (int i = 0) with that iterator (test.end()).
So your loop should look more like:
for (std::vector<string>::iterator i = test.begin(); i != test.end(); i++)
{
cout << *i;
}
You can replace std::vector<string>::iterator with auto, if using C++11 or newer.
The next thing, you included <string.h> which contains old functions such as: strlen, strcpy. Similarly, <cstring> contains C-style strings.
If you want to you use operator<<, so if you want to write:cout << then you have to do: #include <string>.
As already mentioned, the problem is, that you try to compare an integer with an iterator in the "middle" of your for statement. Try this instead, it's more intuitive from my point of view
#include <vector>
#include <iostream>
#include <cstring>
#include <string.h>
using namespace std;
int main()
{
vector<string> test;
test.push_back("yasir");
test.push_back("javed");
for(int i=0; i<test.size();++i)
{
cout << test[i];
}
}

Why I get address when deferencing begin()?

#include <list>
#include <string>
#include <iostream>
int main()
{
std::list<std::string*> *listStr = new std::list<std::string*>();
listStr->push_back(new std::string("HI"));
std::cout << *(listStr->begin()) << std::endl;
return 0;
}
I think I should be getting HI, but I got address as output
008A2B10 Press any key to continue . . .
I can't find my mistake..or have I misunderstood something?
You're printing the pointer in the container.
You need one dereference for the iterator, and another one for the pointer to the string object.
It prints string *, not string.
To print string:
#include <list>
#include <string>
#include <iostream>
int main()
{
std::list<std::string> *listStr = new std::list<std::string>();
listStr->push_back(std::string("HI"));
std::cout << *(listStr->begin()) << std::endl;
return 0;
}

Cannot push C style strings into std::vector

I'm trying to push some const char* into a vector, but the vector remains unpopulated after performing the operations I would presume to fill it.
Here's my attempt, where dict is my command-line argument.
test.cc
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
using namespace std;
int main(int argc, char **argv)
{
ifstream dict;
size_t dict_size;
dict.open(argv[1]); // Dictionary
vector<const char*> dictionary;
string line;
getline(dict, line);
while(!dict.fail()) {
dictionary.push_back(line.c_str());
getline(dict, line);
}
dict_size = dictionary.size();
for(int i = 0; i < dict_size; i++)
cout << "dictionary[" << i << "] is " << dictionary[i] << endl;
}
dict
Hello
World
Foo
Bar
After compiling this, I get the following output:
dictionary[0] is
dictionary[1] is
dictionary[2] is
dictionary[3] is
However, if I change the dictionary's type to vector and push back line instead of line.c_str(), I get the expected output:
dictionary[0] is Hello
dictionary[1] is World
dictionary[2] is Foo
dictionary[3] is Bar
I'm not terribly familiar with C style strings, so maybe it has something to do with null termination?
You are storing dangling pointers.
std::string::c_str() isn't a pointer to some permanent copy of data — just think, that would be leaked!
Store the std::strings instead.
Your code invokes undefined behavior, because after you do
dictionary.push_back(line.c_str());
On the next line that pointer may get deleted:
getline(dict, line); // line now is a different string
You are pushing into the dictionary pointers that point to the same address and at the last iteration it fills the memory area with an empty string. If you don't care about memory leakage you can try like this:
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
using namespace std;
int main(int argc, char **argv)
{
ifstream dict;
size_t dict_size;
dict.open(argv[1]); // Dictionary
vector<char *> dictionary;
while(!dict.fail()) {
string * line = new string();
getline(dict, *line);
if(line->length()>0)
{
dictionary.push_back((char *)line->c_str());
}
}
dict_size = dictionary.size();
for(int i = 0; i < dict_size; i++)
cout << "dictionary[" << i << "] is " << dictionary[i] << endl;
}