ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Thanks for the insight,
But I am seeking more help in this regards.
How do I keep track of space delimited strings in a single and need to check the whole file while storing string in some vector?
Please help me out.
Raj
Hi I have one problem,
I want to store each numerical value in a line of a text file as string in the vector.
If I have the line like this:
1.00000- 5 0.00000+0 1.03912+4 0.00000+0 1.03912+4 1.48000+16457 3 1
14454.4000 1.26000+1 18764.3000 1.13615+1 21379.6000 1.10428+16457 3 1
23453.5000 1.08531+1 30000.0000 1.03848+1 35272.2000 1.01028+16457 3 1
40000.0000 9.92998+0 45365.5000 9.75999+0 57500.0000 9.47555+06457 3 1
65000.0000 9.27821+0 71033.3000 9.08284+0 85448.7000 8.76920+06457 3 1
98085.2000 8.55090+0 113163.000 8.35730+0 137582.000 8.12755+06457 3 1
How do I store each element in a vector of a string?
Please provide me insight in it.
Thanks in advance.
Mahesh
After reading through these sites you will see, that your problem isn't very hard to solve:
Code:
1. declare/open a file-stream for reading (std::ifstream sampleFile("sample.txt");)
2. declare a vector of strings (std::vector<std::string> vec2str;)
3. if you haven't already reached the end of file, then do the following, else goto 8:
4. declare a temporary string that will hold the next line (std::string tmpStr;).
5. fill this temporary string with the current line of your file (std::getline(sampleFile, tmpStr);)
6. add the string to your string-vector (vec2str.push_back(tmpStr);)
7. go back to 3
8. close the file
9. finish
Hi
Thanks for helpful hint.
I have one more question:
Now I am able to store alternate value into the corresponding vector, I want to change them in to the floating point values based on +/- . lets suppose I have string of 1.0000-5 then I want to change it to 0.0000001 and store in new vector.
Can you help me out in this?
Mahesh
I don't know, how you get from "1.0000-5" to "0.0000001"? This notation of floats is no common one, at least as far as I know. However, I guess "1.0000-5" should be the exponential notation? If that is true, then the usual way to write this would be "1.0000e-5". And this is equal to "0.00001". So you first have to format the string properly, so that there is an "e" right before the "+/-". After that you will use "istringstream" (part of the iostream library) to convert it into a 'float'. Here I wrote you a little sample, of how this can be accomplished:
Code:
#include <iostream>
#include <sstream>
using namespace std;
int main(){
string sf = "1.0000-5";
/*
Now find the position of '+' OR '-', beginning at the second(!) char,
because the first one may also be a sign!
*/
int ePos = sf.find_first_of("+-", 1);
if (ePos != string::npos) //if a sign was found, then insert an "e" before it
sf.insert(ePos, "e");
//Now the string has the right format to be used with 'istringstream'
istringstream istr(sf);
float f;
istr >> f; //... and do with 'f' what ever you want ;-)
}
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.