Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

[noob][c++]  how do I use getline?

Posted on 2007-11-16
15
Medium Priority
?
304 Views
Last Modified: 2010-05-18
I am trying to get lines from a text file ,

please show me how to do it with getline.


// open stream file.txt

//  var = getline (file.txt)


but I don't know how to tell it to read from a specific line.
0
Comment
Question by:Troudeloup
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 3
  • 3
  • +1
15 Comments
 
LVL 46

Assisted Solution

by:Kent Olsen
Kent Olsen earned 600 total points
ID: 20299142
Hi Troudeloup,

You're stuck in C syntax.  :)   A very common thing to do.

When you open the stream, you'll assign the stream to a Scalar Variable name.  The variable is the object that you reference.

  MyStream = OpenFile ("file.txt");

  MyStream->GetLine ();



Good Luck,
Kent
0
 

Author Comment

by:Troudeloup
ID: 20299169
I don't understand :(



also, isn't this c++ syntax?



3rd, this is what I plan to do after I have the specific line in the variable







#include <iostream>

using namespace std;



int main ()
{
      string string1 = "I love my bike";
      int pos;
      pos = string1.find ( "I" );
      cout << pos << endl;
      return 0;
}









hence the need to specify a specific line to read from.
0
 
LVL 40

Assisted Solution

by:evilrix
evilrix earned 600 total points
ID: 20299180
Example...
#include <sstream>
 
size_t const MAX_LINE = 50;
 
int _tmain(int argc, _TCHAR* argv[])
{
	std::stringstream ss("hello\nworld\nfrom\nvienna");
 
	for(;;)
	{
		char line[MAX_LINE];
		ss.getline(line,MAX_LINE);
 
		if(ss.eof()) { break; }
		if(ss.bad()) { throw -1; }
 
		// Do something with line
	}
}

Open in new window

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Troudeloup
ID: 20299191
what is this  O_o


_tmain  ?



that doesn't look like C++ to me (omg)
0
 
LVL 40

Assisted Solution

by:evilrix
evilrix earned 600 total points
ID: 20299242
>> _tmain  ?

That's just VC8.0 -- it's a unicode friendly version of main()

How to read the whole buffer in one go...
int _tmain(int argc, _TCHAR* argv[])
{
	// Pretend file stream
	std::stringstream ss("hello\nworld\nfrom\nvienna");
 
	// Get stream size
	ss.seekg(0, std::ios_base::end);
	std::streamsize const nSize = ss.tellg();
	ss.seekg(0, std::ios_base::beg);
 
	// Throw on invalid size
	if(nSize < 0) { throw -1; }
 
	// Make buffer the size we need
	std::string sBuf(nSize, 0);
 
	// Read buffer
	ss.read(&sBuf[0], nSize);
 
	// Throw on read error
	if(ss.bad()) { throw -1; }
 
	// Do something with sBuf
}

Open in new window

0
 

Author Comment

by:Troudeloup
ID: 20299330
uhhhh i have no idea what that does


where is getline?
0
 
LVL 46

Assisted Solution

by:Kent Olsen
Kent Olsen earned 600 total points
ID: 20299885
Hi Troudeloup,

Sorry -- broke for lunch.  :)

Here's a link to a short example where streams are used to read and write files.  Take a look and see if the example clears things up fo ryou.

  http://www.wellho.net/resources/ex.php4?item=c235/file01.cpp

Good Luck,
Kent
0
 
LVL 40

Assisted Solution

by:evilrix
evilrix earned 600 total points
ID: 20299900
>> uhhhh i have no idea what that does

It reads in the whole file into the std::string for you. Why don't you try it -- compile it and step through it and see what it does!

-Rx
0
 
LVL 53

Accepted Solution

by:
Infinity08 earned 800 total points
ID: 20300039
Take a look here :

        http://www.cplusplus.com/reference/string/getline.html

There's also example code on that page.

And a general tutorial for file I/O :

        http://www.cplusplus.com/doc/tutorial/files.html
0
 

Author Comment

by:Troudeloup
ID: 20300399
ok i got this from
http://www.cplusplus.com/reference/string/getline.html



what I don't understand is that if it uses a while to print lines until eof is reached, then shouldn't there
be some sort of a counter?

in
                  getline (myfile,line);
                  cout << line << endl;

what's the thing that tells it to read the next line?



// reading a text file
#include <iostream>
#include <fstream>
#include <string>


using namespace std;

int main ()
{
      string line;
      ifstream myfile ("example.txt");
      
      
      
      if ( myfile.is_open() )
      {
            while ( ! myfile.eof() )
            {
                  getline (myfile,line);
                  cout << line << endl;
            }
      
      myfile.close();
      }

      else cout << "Unable to open file";

      return 0;
}
0
 

Author Comment

by:Troudeloup
ID: 20300407
btw, that is THE code I need to figure out how to use, and then I can do something :D
0
 
LVL 53

Assisted Solution

by:Infinity08
Infinity08 earned 800 total points
ID: 20300620
>> what's the thing that tells it to read the next line?

If you call getline, then it will read the next line from the stream. The stream pointer will then point AFTER that line, so the next time getline gets called, it reads the next line.
0
 

Author Comment

by:Troudeloup
ID: 20300678
ahhhhh


i see, a built-in.

what if I want to read from the top, or just any line?
0
 
LVL 53

Assisted Solution

by:Infinity08
Infinity08 earned 800 total points
ID: 20300707
>> what if I want to read from the top,

Just re-open the file, or re-wind the file. And the stream will be re-set to the beginning of the file.


>> or just any line?

You'll have to read lines until you reach the line you want.
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question