Thread: File input

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    12

    File input

    I get input from a file called "accounts.txt" and try to compare it with a predefined word such as 'blue'. Here's an example:

    int main()
    {
    char word[50];
    ifstream file;
    file.open("accounts.txt");
    file>>word;
    if(word == "blue")
    cout<<"The program works";
    else
    cout<<"The program does not work";
    return 0;
    }
    I never get "The program works" even if the only possible word is blue!
    Help me!!!!!!!

  2. #2
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    it's because you can't compare C-style strings like that... try:

    Code:
    int main()
    {
    char word[50];
    ifstream file;
    file.open("accounts.txt");
    file>>word;
    if(!strcmpi(word,"blue"))
    cout<<"The program works";
    else
    cout<<"The program does not work";
    return 0;
    }
    by the way: use code tags and format your code...
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    If you used a std::string instead it would have worked:

    Code:
    #include <iostream>
    #include <string>
    #include <fstream>
    
    int main()
    {
        std::string word;
        std::ifstream file;
        file.open("accounts.txt");
        file >> word;
        if(word == "blue")
            std::cout << "The program works";
        else
            std::cout << "The program does not work";
        return 0;
    }
    The reason it doesn't work for character arrays is that the character string "blue" is a string literal and its use in your comparison statement in effect is trying to compare the address of the character array word with the address of that string literal in memory. Those two addresses will NEVER be the same.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  2. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 PM
  3. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 01:04 PM
  4. Totally confused on assigment using linked lists
    By Uchihanokonoha in forum C++ Programming
    Replies: 8
    Last Post: 01-05-2008, 04:49 PM
  5. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 03:58 PM