Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

skyx

How to get one line character from a *.txt file

This topic is 5498 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

For example,in a a.txt file, there are some lines: x = 0.4; y = 3.5; z = 3.4; I want to read every line of the file in to an character array and display them using an input argument to decide which line to be read.For example, if I input 2 the line y = 3.5 is read into an character array.Would you please tell me how to do it?Thanks in advance!

Share this post


Link to post
Share on other sites
Advertisement
I think it''s best to simply read the entire file into a buffer and then start seeking for ''\n'' (next line) to the next ''\n'' and use what is in between as your one line.

Share this post


Link to post
Share on other sites
What language are you coding this in?

If you are using C++, you can use std::getline(), defined in the string header to read all the characters up to the newline character ('\n'). In your post, though, you said, "I want to read every line of file in to an character array". So are you using C-style strings? If so, you can use std::istream's getline() method (e.g. std::cin.getline()). If you are actually using C, I'm sure there are functions for this, and if not you can just create your own loop that searches for newline characters ('\n') and uses these to determine the separate lines.

Edit: I'll give a few examples when I get back home. For now, though, you could have a search on Google.

[ Google || Start Here || ACCU || STL || Boost || MSDN || GotW || MSVC++ Library Fixes || BarrysWorld || E-Mail Me ]

[edited by - Lektrix on July 2, 2003 8:53:21 AM]

Share this post


Link to post
Share on other sites
Well if you want to allow the user to choose which line to read in for the text file this is basically how you would do it:

step 1.
open the txt file using ifstream

step 2.
ask the user which line to get

step 3.
position the line pointer to the specified line from step 2
(I believe the seek() function can do the repositioning)

step 4.
If the reposition doesn''t result in a eof then read it into your array.

Those are the basic steps to do what you''re looking for. I''ll leave the actual coding up to you





You fight like a dairy farmer.

Share this post


Link to post
Share on other sites
Thank you! But how can I skip lines using seek function which seeks by bytes?

Share this post


Link to post
Share on other sites
whoops, forgot about that. Seekg probably wouldn't work then unless all the lines you're reading in are of the same length. Better to just use getline instead. Just put it in a loop and have the loop run equal to line number inputted. Doesn't seem like a very nice way to do it but it'll work

But if you know for sure that all the lines in your file are equal in length then using seekg would be better. Just use:

fileinput .seekg(line *length);
//length==bytes needed per line





You fight like a dairy farmer.

[edited by - Greatwolf on July 3, 2003 4:35:23 AM]

Share this post


Link to post
Share on other sites
If you are using C++:

#include <fstream>
#include <vector>
#include <string>

int main()
{
std::ifstream in_file("input.txt");
// test for error (fail bit), etc.


std::vector<std::string> str_lines;
std::string line;
while ( !in_file.eof() )
{
std::getline( in_file, line, ';' ); // I don't know why you are asking for '\n' as the delimiter, when your input file is using ';'

str_lines.push_back(line);
}

std::cout << "Which line would you like to view?" << std::endl;
int num;
std::cin >> num;

std::cout << str_lines[num - 1] << std::endl;

...

return 0;
}

Of course, you could make it a vector of pointer-to-std::strings (and also read straight into the vector, eliminating the need for line, if you really want), but there will be little difference in efficiency with such a small number of input strings.

[ Google || Start Here || ACCU || STL || Boost || MSDN || GotW || MSVC++ Library Fixes || BarrysWorld || E-Mail Me ]

[edited by - Lektrix on July 3, 2003 8:50:43 AM]

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!