Help answer threads with 0 replies.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 02-26-2006, 04:37 PM   #1
LQ Newbie
Registered: Feb 2006
Posts: 5

Rep: Reputation: 0
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.
Old 02-26-2006, 05:09 PM   #2
Senior Member
Registered: Mar 2005
Location: USA::Pennsylvania
Distribution: Slackware
Posts: 1,065

Rep: Reputation: 45
search for fstream and vector, they have everything you need..
Old 02-26-2006, 11:50 PM   #3
LQ Newbie
Registered: Feb 2006
Posts: 5

Original Poster
Rep: Reputation: 0
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.
Old 02-27-2006, 09:34 PM   #4
LQ Newbie
Registered: Feb 2006
Posts: 5

Original Poster
Rep: Reputation: 0
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.
Old 02-28-2006, 11:29 AM   #5
Registered: Aug 2005
Location: Germany
Distribution: Ubuntu, Debian
Posts: 189

Rep: Reputation: 31
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!):
A detailed documentation of the "iostream library" with all classes and members you will find here:

A basic introduction to STL ('vector' and 'string' is part of this) is located here:
A complete list of all STL-classes (including vector and string/basic_string) and members is here:

After reading through these sites you will see, that your problem isn't very hard to solve:
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.
Old 03-01-2006, 02:52 PM   #6
LQ Newbie
Registered: Feb 2006
Posts: 5

Original Poster
Rep: Reputation: 0
Thank you.Flesym..One more Question

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?
Old 03-04-2006, 08:54 AM   #7
Registered: Aug 2005
Location: Germany
Distribution: Ubuntu, Debian
Posts: 189

Rep: Reputation: 31
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:
#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
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
reading text from a file mrobertson Programming 16 06-28-2005 12:39 PM
Parsing large text file with perl smaida Programming 5 09-13-2004 04:33 AM
C++ reading from text file query lrt2003 Programming 5 05-15-2004 04:25 AM
reading a text file and outputting to another. Hardw1re Programming 28 11-03-2003 08:51 AM
Script file to replace large text blocks in files? stodge Linux - Software 0 09-27-2003 10:53 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:31 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration