Thread: string and char

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    7

    SOLVED: TQstring and char

    hi...

    im newbies in C...
    ive got a problem hope ur guys please take a look at it...

    Code:
    int main () {
        string line;
      char line1[60], c[] ="object Class" ;
      ifstream myfile ("example.txt");
      if (myfile.is_open())
      {
        while (! myfile.eof() )
        {
          getline (myfile,line); //--->problem here
            line1 = line;
          if (line1 == line2) 
              {cout << line2 << endl;}
        }
        myfile.close();
      }
    
      else cout << "Unable to open file"; 
    
      return 0;
    }
    line - registered as string
    line1 - registered as char with size of 60
    line2 - is char "object Class"

    error - Error E2277 test6.cpp 15: Lvalue required in function main()

    how can i compare line and line2?
    how can a string (line) can be converted to char (line1)?

    really need help...
    shatred
    Last edited by shatred; 12-28-2006 at 11:28 PM.

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
    while (! myfile.eof() )
    Read the FAQ entitled "Why not to use feof() to control a loop". http://faq.cprogramming.com/cgi-bin/...&id=1043284351

    Also see (for your main problem) http://faq.cprogramming.com/cgi-bin/...&id=1043284385
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    7
    oh sorry...i really thinks its C prog, thanks anyway, but to convert from string (read from file) to char (strcmp), i got a little problem...

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    This is C? I don't think so.
    Code:
    string line;
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Why do you think you need character arrays? Why not use only strings?

    To convert from string to character array, you call the .c_str() function of the string object. To copy that into your own character array, you need to use strcpy which copies C style strings. However, under most circumstances you should not need to do this, as the C++ string class should be good enough.

    >> Read the FAQ entitled "Why not to use feof() to control a loop".
    The problems listed in that FAQ will not cause an issue here, although the solution is still appropriate: while (getline (myfile,line)).

  6. #6
    Registered User
    Join Date
    Oct 2006
    Posts
    7
    tq Dave & dwks...

    yeah and thank you so much for the reply, i do that because
    i want to read a file that contain string like below

    object Class A
    object Attribute A
    i use getline for the string right?
    and the output should be 2 string line ....
    but after that i want to trace if the 1st string line (getline) have the string = "object Class" or not...
    if yes, only the "A" string after the "object Class" string will be stored into another file...
    and the 2nd is not, so it will not be store,
    the main problem is "A" string could be anything, eg. "BOOK"...
    and the line also can be more than 10...
    what is the recomended solution for the problem...and what is whitespace, is it spacebace in text...
    could u assist ur opinion...
    Last edited by shatred; 12-28-2006 at 04:46 PM.

  7. #7
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    In addition to the objects and member functions you are using you will need these:

    http://www.cppreference.com/cppstring/find.html
    Use find() to locate instances of "object Class" in data which you've read from input;

    http://www.cppreference.com/cppstring/substr.html
    Use substr() to take out what you need to write as output or to another file.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char Handling, probably typical newbie stuff
    By Neolyth in forum C Programming
    Replies: 16
    Last Post: 06-21-2009, 04:05 AM
  2. How do i un-SHA1 hash something..
    By willc0de4food in forum C Programming
    Replies: 4
    Last Post: 09-14-2005, 05:59 AM
  3. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 11:06 PM
  4. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM