Sign in to follow this  

Compare 2 lines of text doesnt seem to work

This topic is 4414 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

hi there, i got this piece of code :
char newsdate[10];
    char testdate[10];

    ofstream newsout("news.txt");
    ifstream newsin("news.txt");

    // if file exsists, compare to see if up 2 date, else download new.
    if (newsout != NULL)
    {
        recv(sock,testdate, 10, 0);
        newsout.write(testdate, 10);
        newsin.read(newsdate, 10);

        if(strcmp(newsdate, testdate) == 0)
        {
            help.WriteStat(hStatusText, "Up2date...");
            Sleep(2000);
            download = 0;
        }
        else
        {
            Sleep(1000);
            download = 1;
        }
    }
    // if file dont exsist, download new
    if(newsout == NULL)
    {
        Sleep(1000);
        newsout.write(testdate, 10);
        download = 1;
    }

    if (download == 1)
    {
        help.WriteStat(hStatusText, "Downloading news...");

the text file it gets / compare with (news.txt): [11/10/05] here is news for.... * new 1 * new 2 [01/01/05] Could be old news of some sort... ---- Anyways, it gets [11/10/05] from webserver (first line of news.txt on server) and should compare with first line from current news.txt on pc, but it always downloads, no mather what. if file exsist, it compares, it not, it downloads it, but why does it always download, and not compare and skip downloading? Thanks

Share this post


Link to post
Share on other sites
Quote:
Original post by Kalasjniekof
ofstream newsout("news.txt");

if (newsout != NULL)

This statement will allways return true, as newsout is created on the stack.
Try if(newsout.is_open())

[EDIT] this won't solve your problem though.

Not true and it has nothing to do with the stack. std::ofstream inherits a conversion operator to void *. This conversion operator returns the NULL pointer if ofstream::fail would return true and a non-NULL pointer otherwise. So the above is perfectly valid and equivalent to if (!newsout.fail()).

OP: I'm not sure that the behaviour of files is defined if you have multiple streams open for the same file. From what I understand from the C++ final draft standard (and the C99 final draft standard, since the C++ final draft just references the C standard (yes, I know it references the original standard, but I only have a copy of the C99 final draft (and yes, I know I'm excessively nesting my parenthesis))) the behaviour is implementation defined. And you should probably be using strncmp if you do need to use raw char since your buffer is only ten characters long.

Enigma

Share this post


Link to post
Share on other sites
What happens when you step through the debugger? Does it skip over strcmp? Are both the strings shown as the same in the debugger? (You're limiting to 10 chars, so it's possible that you cut out before the null terminator, in which case they won't be the same. Enigma told you how to compare it more properly.)

Why are you maintaining two seperate file reading objects on the same object? Why not just use a single fstream?

Also, you do if(newsout != null){} if(newsout==null){}. Why not if(newsout != null){}else{}? Is there some reason you think that newsout will become null?


Personally, I'm really confused by the newsout.write followed by newsin.read. You're writing to news.txt and then reading from news.txt. If you're not planning on writing the rest of the download, why are you writing the date to it?

Finally, ofstream newsout("news.txt") becomes ofstream newsout("news.txt", out | trunc). This deletes news.txt and makes it a blank file. You then immediately open it for reading--after emptying it. When you attempt to read 10 characters you should only get a null terminator. Except you just wrote to news.txt, so you should have read what you just wrote. Except ofstream is buffered, and probably didn't write what you wrote yet. So you probably got a null string in newsdate, and possibly have a non-terminated string in testdate. Check in the debugger to see what is ACTUALLY happening, this will help you IMMENSELY.

Share this post


Link to post
Share on other sites

This topic is 4414 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this