C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old February 17th, 2005, 06:52 PM
MMayfield45 MMayfield45 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2005
Posts: 2 MMayfield45 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 17 m 42 sec
Reputation Power: 0
string problems

i have got this code

Code:
  ofstream a_file ( "example.txt" );
  a_file<<"hello";
  a_file.close();
	
  char *buffer;
  long size;

  ifstream file ("example.txt", ios::in|ios::binary|ios::ate);
  size = file.tellg();
  file.seekg (0, ios::beg);
  buffer = new char [size];
  file.read (buffer, size);
  file.close();

  if (buffer == "hello"){
    cout << "ok\n";
  }
  cout << buffer;


the output is 'hello'. How come the comparison of
(buffer == "hello")
does not equal true?

Reply With Quote
  #2  
Old February 18th, 2005, 08:30 AM
Anibal Anibal is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2005
Posts: 176 Anibal User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 4 h 20 m 48 sec
Reputation Power: 4
Hi MMayfield45. I've worked with C (not so much with C++) but...aren't you supposed to use
strcmp() to see if the strings match?

strcmp(buffer,"hello")

I don't know if strings can be compared with "==" in C++ (at least in C you cann't).

Good Luck

Anibal.

PS: I apologise if I'm mistaken....simply trying to help, as I was helped before!!

Reply With Quote
  #3  
Old February 18th, 2005, 01:58 PM
MMayfield45 MMayfield45 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2005
Posts: 2 MMayfield45 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 17 m 42 sec
Reputation Power: 0
thanks, i got the reply of another forum.

Code:
#include <fstream>
#include <iostream>
#include <string>

using namespace std;

int main() {
    ofstream a_file ( "example.txt" );
    //learn to check return values!
    a_file<<"hello";
    a_file.close();

    string buffer, tmp;

    ifstream file ("example.txt", ios::in);
    //learn to check return values!
    while (!file.eof()){
        getline(file, tmp);
        buffer += tmp;
    }
    file.close();

    if (buffer == "hello") {
        cout << "ok\n";
    }

    cout << buffer << endl;

    return 0;
}

Reply With Quote
  #4  
Old February 19th, 2005, 04:50 PM
MadCowDzz's Avatar
MadCowDzz MadCowDzz is offline
I'm Internet Famous
Dev Articles Frequenter (2500 - 2999 posts)
 
Join Date: Jan 2003
Location: Toronto, Canada
Posts: 2,890 MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 1 Week 16 h 14 m 9 sec
Reputation Power: 8
So the problem was all with this line?
ifstream file ("example.txt", ios::in|ios::binary|ios::ate);

I agree with Anibal; I think it's better practice to use strcmp.

Reply With Quote
  #5  
Old March 7th, 2005, 12:57 AM
Cirus Cirus is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2005
Posts: 273 Cirus User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 11 h 35 m 2 sec
Reputation Power: 4
Question

Quote:
Originally Posted by MadCowDzz
So the problem was all with this line?
ifstream file ("example.txt", ios::in|ios::binary|ios::ate);

I agree with Anibal; I think it's better practice to use strcmp.


Please explain , why the blue -line is trouble maker.

Reply With Quote
  #6  
Old March 7th, 2005, 08:01 AM
MadCowDzz's Avatar
MadCowDzz MadCowDzz is offline
I'm Internet Famous
Dev Articles Frequenter (2500 - 2999 posts)
 
Join Date: Jan 2003
Location: Toronto, Canada
Posts: 2,890 MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 1 Week 16 h 14 m 9 sec
Reputation Power: 8
Oh, don't ask me... I was simply comparing the two scripts...
That was the only difference I had noticed...

Perhaps MMayfield45 can explain how the problem was solved.

Reply With Quote
  #7  
Old March 24th, 2005, 10:44 AM
Zak Zak is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2005
Posts: 1 Zak User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 m 49 sec
Reputation Power: 0
The reason thr first script didn't work is because

The reason thr first script didn't work is because you cannot open a text file with the ios::binary option.

-Zak

Reply With Quote
  #8  
Old March 29th, 2005, 08:17 AM
MadCowDzz's Avatar
MadCowDzz MadCowDzz is offline
I'm Internet Famous
Dev Articles Frequenter (2500 - 2999 posts)
 
Join Date: Jan 2003
Location: Toronto, Canada
Posts: 2,890 MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 1 Week 16 h 14 m 9 sec
Reputation Power: 8
Thanks for clearing that up Zak

Reply With Quote
  #9  
Old March 29th, 2005, 11:03 AM
BoolBooB BoolBooB is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2005
Posts: 36 BoolBooB User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 9 h 35 m 42 sec
Reputation Power: 4
Quote:
Originally Posted by Zak
The reason thr first script didn't work is because you cannot open a text file with the ios::binary option.

-Zak

Why not? You may not get the results you want, but you should alway be able to open a file as binary. After all, it's stored as bits and bytes. And if you know what you are doing it will always work.

Reply With Quote
  #10  
Old March 29th, 2005, 11:04 AM
BoolBooB BoolBooB is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2005
Posts: 36 BoolBooB User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 9 h 35 m 42 sec
Reputation Power: 4
Thumbs up

Quote:
Originally Posted by MadCowDzz
So the problem was all with this line?
ifstream file ("example.txt", ios::in|ios::binary|ios::ate);

I agree with Anibal; I think it's better practice to use strcmp.

Yep. You gotta use strcmp().

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > string problems


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway