Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > skipping the lines

Reply
Thread Tools

skipping the lines

 
 
friend.blah@googlemail.com
Guest
Posts: n/a
 
      06-04-2008
Could anyone give a simple idea in solving this
i have a text file which has several millions of lines consisting
around 5 coloums
for example
12345
456789101187837
12536536657588968590680568
36736473647
454785678475767856756
......

the length of string in each line is diffrent...

so when i first read my file i want to take only first line....
when i read it for the second time i want second line skipping the
first line....and so on..

till now what i was doing is

int lineCounter = 1; //declared in initialize function
int row = 1;
bool check = true;

ifstream filein("xxx.txt");
while(check)
{
while(row < LineCounter)
{
filein.ignore(26,'\n'); //here what i am doing is i am
entering the line which has maximum number of characeters .i.e., in
the example my 3rd line has maximum number of characters i.e., 26
row++;
}
getline(filein,Input);
check = false;
}

but i cant check it in file which has millions of lines...

so please tell me smart way to approach this

thanks to all

 
Reply With Quote
 
 
 
 
Jerry Coffin
Guest
Posts: n/a
 
      06-04-2008
In article <4fae62b0-6858-4e9e-830e-9eecf6691d4a@
59g2000hsb.googlegroups.com>, says...
> Could anyone give a simple idea in solving this
> i have a text file which has several millions of lines consisting
> around 5 coloums


[ ... ]

> so when i first read my file i want to take only first line....
> when i read it for the second time i want second line skipping the
> first line....and so on..


Each time you read from the file, keep track of the file position after
reading. When you read the file the next time, seek to the previously-
stored position, then read one line and update the file position.

--
Later,
Jerry.

The universe is a figment of its own imagination.
 
Reply With Quote
 
 
 
 
MiB
Guest
Posts: n/a
 
      06-04-2008
You should separate the file open and read operations into different
functions, so the file can stay open between reads, e.g.:

#include <iostream>
#include <fstream>
#include <string>

class Reader {
public:
Reader( const std::string& fname ) : ifs( fname.c_str() )
{}

void DoSomething() {
while ( GetLine() ) {
// Do your task here, 'line' contains the current line.
std::cout << line << std::endl;
}
}

private:
bool GetLine() { return std::getline( ifs, line ); }

std::ifstream ifs;
std::string line;
};


int main() {
Reader reader( "xxx.txt" );
reader.DoSomething();
return 0;
}

best,

Michael Böhnisch
 
Reply With Quote
 
xyz
Guest
Posts: n/a
 
      06-09-2008
On Jun 4, 10:02*pm, MiB <Michael.Boehni...@gmail.com> wrote:
> You should separate the file open and read operations into different
> functions, so the file can stay open between reads, e.g.:
>
> #include <iostream>
> #include <fstream>
> #include <string>
>
> class Reader {
> public:
> * * * * Reader( const std::string& fname ) : ifs( fname.c_str() )
> * * * * {}
>
> * * * * void DoSomething() {
> * * * * * * * * while ( GetLine() ) {
> * * * * * * * * * * * * // Do your task here, 'line' contains the currentline.
> * * * * * * * * * * * * std::cout <<line<< std::endl;
> * * * * * * * * }
> * * * * }
>
> private:
> * * * * bool GetLine() { return std::getline( ifs,line); }
>
> * * * * std::ifstream ifs;
> * * * * std::stringline;
>
> };
>
> int main() {
> * * * * Reader reader( "xxx.txt" );
> * * * * reader.DoSomething();
> * * * * return 0;
>
> }
>
> best,
>
> * *Michael Böhnisch


you could do this thing
keep track of filepointer after u read the line
int length = 0 //declaare it in intialize

filein.seekg(length,ios::cur);
getline(filein,Input);
length = filein.tellg();

u regularly update the fileposition such that u jump directly to that
line ...u want
 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Method needed for skipping lines Gustaf Python 7 11-01-2007 09:57 AM
Parsing a text file line-by-line: skipping badly-formed lines? denis.papathanasiou@gmail.com Perl Misc 27 05-18-2007 07:07 PM
python skipping lines? lisa.engblom@gmail.com Python 6 11-27-2006 07:35 PM
Asp.Net Calender, how to display 5 lines if there are only 5 lines in one month? Jack ASP .Net 9 10-12-2005 03:44 AM
Modems, Analog Lines and ... Electrical Lines? Sens Fan Happy In Ohio Computer Support 5 09-02-2004 04:15 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57