Programming This 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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
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.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
02-26-2006, 04:37 PM
|
#1
|
|
LQ Newbie
Registered: Feb 2006
Posts: 5
Rep:
|
Reading Large Text File in C++
Hi all,
I am facing one problem in my research project.
I have a large file containing text as:
1 4000000.55555+ 2 4 50000000.0002+ 2.....
......
......
9 3.000000+ 2 5 2.000111+ 3......
I need to read this file reading each tab seperated string and storing them as vector element in some vector in C++.
Ho do I reading each line in the file identifying each string and storing them simultaneously?
Please suggest me the solution to this problem.
I would appreciate all input in this.
I am in need of urgent solution.
regard
RAJ
|
|
|
|
02-26-2006, 05:09 PM
|
#2
|
|
Senior Member
Registered: Mar 2005
Location: USA::Pennsylvania
Distribution: Slackware
Posts: 1,065
Rep:
|
search for fstream and vector, they have everything you need..
|
|
|
|
02-26-2006, 11:50 PM
|
#3
|
|
LQ Newbie
Registered: Feb 2006
Posts: 5
Original Poster
Rep:
|
More Help needed?
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
|
|
|
|
02-27-2006, 09:34 PM
|
#4
|
|
LQ Newbie
Registered: Feb 2006
Posts: 5
Original Poster
Rep:
|
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
|
|
|
|
02-28-2006, 11:29 AM
|
#5
|
|
Member
Registered: Aug 2005
Location: Germany
Distribution: Ubuntu, Debian
Posts: 189
Rep:
|
First, I won't write the code for you! But I can give you some hints, so that you (hopefully) will be able to write it yourself:
Begin by going through this tutorial about file-streams (until you understand everything!):
http://www.cplusplus.com/doc/tutorial/files.html
A detailed documentation of the "iostream library" with all classes and members you will find here:
http://www.cplusplus.com/ref/iostream/
A basic introduction to STL ('vector' and 'string' is part of this) is located here:
http://www.msoe.edu/eecs/ce/courseinfo/stl/index.htm
A complete list of all STL-classes (including vector and string/basic_string) and members is here:
http://hea-www.harvard.edu/MST/simul..._contents.html
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
Last edited by Flesym; 02-28-2006 at 11:50 AM.
|
|
|
|
03-01-2006, 02:52 PM
|
#6
|
|
LQ Newbie
Registered: Feb 2006
Posts: 5
Original Poster
Rep:
|
Thank you.Flesym..One more Question
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
|
|
|
|
03-04-2006, 08:54 AM
|
#7
|
|
Member
Registered: Aug 2005
Location: Germany
Distribution: Ubuntu, Debian
Posts: 189
Rep:
|
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 ;-)
}
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 06:37 AM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|