![]() |
|
|
|
| ||||||
|
Welcome to the The ProgrammersTalk Community forums. You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today! If you have any problems with the registration process or your account login, please contact contact us. |
![]() |
![]() | | LinkBack | Thread Tools | Display Modes | ![]() |
| |
| ||||
| Make a counter per line and add the if statement before read() ![]() |
| The Following User Says Thank You to HelloWorld For This Useful Post: | ||
MrPickle (04-13-2008) | ||
| |||
| as HelloWorld said, the way to do it is to keep a counter for each call to getline() that you issue to the fstream. "lines" as you probably know them are indicated by a '\n' (newline) character. When you open a file, your program has absolutely no idea which characters reside in your file until you actually read them - there's no built-in mechanism within the language which counts the number of '\n' characters automatically, so you have to do it yourself. (since '\n' isn't really a special character, its just the same as any other character, as far as the language is concerned) |
| The Following User Says Thank You to Bench For This Useful Post: | ||
MrPickle (04-13-2008) | ||
| |||
| Again, getline will do that for you. if you don't care about the rest of a line, then you could also do an ignore, instead of a getline into a dummy string. The principles and methods here are identical to the problem of the common "how do I empty cin", in that you're skipping all the remaining chars up to, and including the next newline character, except that you're reading a file stream (all streams in C++ are basically the same). Code: std::ifstream myfile; //etc.. myfile.ignore ( std::numeric_limits<std::streamsize>::max(), '\n' ); |
| ||||
| Quote:
![]() |
| The Following User Says Thank You to HelloWorld For This Useful Post: | ||
MrPickle (04-15-2008) | ||
| |||
| No, that's not possible. remember that a stream is not a file, but a "pipe" which acts as an input or output interface to/from a file. physical access to a file isn't supported by standard C++, since its O/S dependent (There might be some OS-specific libraries which do this). You're limited to the things which a stream can do - when you read from a stream, the bytes are removed from that stream, there's no 'going back', nor is there any built-in buffer which remembers anything that you've read from the stream (Though you could write one). |
| The Following User Says Thank You to Bench For This Useful Post: | ||
MrPickle (04-15-2008) | ||
| ||||
| Quote:
|
![]() |
| Thread Tools | |
| Display Modes | |
| |