![]() |
|
|
|
| ||||||
|
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. |
| Tags: input, iostream, output, read, write |
![]() |
![]() | | LinkBack | Thread Tools | Display Modes | ![]() |
| |
| |||
| I'm not sure about tokenization (not sure what that actually is), but one thing that your code is doing is checking whether a file is equal to false or 0 after you open it. The method you should be using is if (!infile.is_open()), for example. Another, perhaps better, way you could check is to check the failbit using if (infile.fail()) since the failbit is set if the requested file is not opened or the variable already represents an open file (calling open() on the same variable without closing it properly makes the call to open() fail, setting the failbit). As for file comparison, perhaps you would prefer binary read/write instead of text. That way, you could compare the bytes rather than just text, where text may not actually be text. Also, if you wanted to, you could shorten your code a bit: Code: //just remove the "| ifstream::binary" if you want to use text
ifstream infile("pdf1.txt", ifstream::in | ifstream::binary);
ifstream infile2("pdf2.txt", ifstream::in | ifstream::binary); __________________ "C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do it blows your whole leg off." -- Bjarne Stroustrup, creator of what is now known as C++ For more quotes by Bjarne Stroustrup, check out http://www.research.att.com/~bs/bs_faq.html#really-say-that. |
| The Following User Says Thank You to rpgfan3233 For This Useful Post: | ||
HelloWorld (08-03-2007) | ||
| ||||
| Just found this bit of code: Quote:
Hope it helps somewhat, Lee. |
| The Following User Says Thank You to Lee For This Useful Post: | ||
HelloWorld (08-03-2007) | ||
| |||
| When reading lines from files in a loop, the best place to put the getline is inside the while condition itself - this is the simplest way to make sure the file doesn't read over the end, and that the final iteration of the loop doesn't cause undefined behaviour Code: string str;
while( getline( infile, str ) )
{
// ... str was read from infile OK
} Sometimes its necessary to put the getline elsewhere, maybe if the sequence of file reads is a more complicated process, you want to put checks at every point along the way make sure each step is successful Code: string str;
int i;
while( true )
{
infile >> i;
if ( !infile )
break;
getline( infile, str );
if ( ! infile )
break;
// read was successful - can do something with str and 'i'
} Last edited by Bench : 08-03-2007 at 05:44 PM. |
![]() |
| Thread Tools | |
| Display Modes | |
| |