How to read certain lines from a txt file?

Hello, I was just wondering if there was a way I can view certain entries that I write into a text file. I have a program that lets the user keep track of a diary. When I read the entries, I have to go line by line. Is there a way I can skip lines and lets say go to my entry for yesterday without having to read my entries for the previous two weeks?

I believe one of the flags lets you choose where the read cursor starts and stops. I can't tell you offhand what the code is for that though I guess I could look for you. Otherwise what you could do is possibly read in each entry to a string and put each string in a vector. Then you could have the user type in a date and go to which over entry that is in the vector.

Oh now I remember it is seekg and tellg I don't remember how to use them though maybe check out the istream references.
http://www.cplusplus.com/reference/istream/istream/
Last edited on
ooooo sounds logical except i don't know what a vector is lol. lemme do my research real quick
here's a little explanation on seekg(), it takes two arguments, the first is some type of signed integer, the second is an ios flag, lets talk about the first, it determines how many bytes the get pointer should move, if the number is positive then the movement will be towards the end of the file, if negative, the movement will be towards the beginning of the file, and as i said, the number determines how many bytes the pointer will move.
secondly the second argument determines the location where the seeking will start from, it can be one of those three values:
ios::beg.....the seeking will start from the beginning of the file.
ios::cur.....the seeking will start from the current location of the get pointer.
ios::end.....the seeking will start from the end of the file.

seekg() is a member of the ios class hierarchy, so you should call it via the dot operator.
this was a simple and practical explanation about it, if you want some truly scientific definition, you should probably check a reference about it.

a vector is defined in the STL library, you can read about STL to know more info about vectors, don't just start in STL before learning the basics, it's really a huge domain, for now, if you need to use a vector you can easily learn it and it will cost you a few hours before you get comfy with it.

about the basic problem here:
make the user insert a timestamp before he starts writing his diary, and then you can seek to the last timestamp in the file, probably seek from the end of the file until you find a timestamp pattern and it should be the last one, this way you can always easily open any entry in the diary (just look for timestamp patterns).

another but really lame way is to create a special string that english doesn't use in normal diary writing, and use it as a seperator, to seperate different records in the file.
this should be easier but really lame.

hope that was useful.
If each record is of a fixed length, the idea of using seekg() makes sense. Alternatively, if you have designed a file structure with an index listing the start position of each record, you might use seekg().

However, I'm guessing that neither of these applies here.
Possibly the simplest solution would be to start each record with the date (and possibly time). Then it is possible to read the file one line at a time and stop when a record is found matching the required date.

This requires a little bit of planning to ensure that the date field is always there in the expected place, but is otherwise pretty straightforward. Doing it this way you don't have to use an array (or vector), but it can simplify searching if say you wanted to read an entry and then go back to the previous entry - easy if the records are held in memory, but a bit more messy if you are reading direct from the file.
@chervil are you saying if I declare the date to a string or something, I can just find that string? If so how would I go about doing that?
Oh I didn't even think of this before...You could output the date then on the nextline output the "diary info"

1
2
3
6
ofstream out( "diary.txt" );
out << date << std::endl << diary_info << std::endl;


Say for example you put an entry yesterday and an entry today
06/10/2013 (Be sure to use the same thing for all dates dd/mm/yyyy might be the easiest. ) Today I wrote something to my diary.
06/11/2013 Today I am reading my file by date.

would look like this in the txt file
06/09/2013
Today I am learning c++.
06/10/2013
Today I wrote something to my diary.
06/11/2013 Today I am reading my file by date.


Then you could do something like this
1
2
3
4
5
6
7
8
9
10
std::string search( "06/10/2013" ) , temp_date( "" ) , diary_info( "" );
std::ifstream in( "diary.txt" );
while( in.good() && temp_date != search )
{
    getline( in , temp_date );
    getline( in , diary_info );
}

std::cout << "On " << temp_date << " your diary info was:" << std::endl;
std::cout << diary_info << std::endl;


You could also output like date|diary_entry
then read like getline( in , info , '|' ); either way I'm sure you'll figure it out.
Sorry Giblit, I don't know if we are using the same c++ language lol. Im too new at this and I only know the Ascii c++.
As for the date format, I tend to prefer yyymmdd (with or without separators) as it makes sorting easier - though of course you could store it in the file in a different format - so long as it is consistent. If there could ever be more than one record per day, I would add the time too, at least hh:mm or even hh:mm::ss - again, be consistent.

If the text is free-format, possibly with line-breaks and so on, then the entry for a particular day could occupy multiple lines. In that case, the line identifying the date could start with some special character, one which the user would not normally type. You might chose some mundane character such as # or @, or maybe something more exotic. Then when reading the file, just test the first character of the line for this special character.

Of course I'm not explaining this very well in terms of how to write the code - but my point is you first have to design the layout of the information in the file. Once you have some rules to follow, writing the code becomes (relatively) simple.
Last edited on
errrrrrrrrrrrrrrrrrrrg now i is confused
when i run the program now, the program doesn't stop. Do you think, you can compile my program and see what I did wrong?
it should be a simple task for an experienced programmer to examine your program and see what's wrong, but i really advise you to debug it yourself, this will make you discover the common mistakes a programmer make, and eventually you'll easily avoid them.
Topic archived. No new replies allowed.