Thread: Adding stuff to strings from files..

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    22

    Adding stuff to strings from files..

    This is probably very stupid, but I'm a beginning programmer and I'd like your help.
    I have a text-file with lines like this:

    bla bla bla !end!

    Now I want my program to ad all the words from a line to a variable, until it reaches "!end!". So if my program would read from the above line the value of the variable would be "bla bla bla". I tried it with this (only a small piece):

    Code:
    ifstream my_file("rode.txt");	
    my_file>>part_of_total;
    while (part_of_total!="!end!")
    {
    strcat(totalname," ");
    strcat(totalname,part_of_total);
    my_file>>part_of_total;
    }
    I know, my coding in general sucks But do you guys have tips or something for me to achieve this?
    Doesn't have to be an anhanced methode of my coding, new coding would rock
    Thanx in advance!

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    look up strcmp() in your help files or favourite text book. You cannot compare C style strings with != operator.
    strcmp() returns 0 if the strings are equal.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    You can't simply do:
    while(x!="!end!")

    You must either use a strtok function or parse the string letter by letter.

    Like:

    for(i = 0; i<strlen(string); i++)
    {
    if(string[i] == '!')
    break;

    else
    temp[j] = string[i];

    j++;
    }
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  4. #4
    Registered User
    Join Date
    Feb 2002
    Posts
    22
    Thanx fot the info both! As you can see, I'm very new

  5. #5
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,138
    use strcmp to compare and then use strcat to combine the strings.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strings Program
    By limergal in forum C++ Programming
    Replies: 4
    Last Post: 12-02-2006, 03:24 PM
  2. files, strings, comparation ...
    By milan in forum C Programming
    Replies: 4
    Last Post: 12-19-2002, 09:26 AM
  3. Help...using strings and txt files
    By Juicehead in forum C Programming
    Replies: 0
    Last Post: 11-26-2002, 09:01 AM
  4. adding onto strings
    By Granger9 in forum C Programming
    Replies: 6
    Last Post: 09-11-2002, 01:06 PM
  5. Strings and stuff
    By casanova0o7 in forum C++ Programming
    Replies: 4
    Last Post: 01-23-2002, 08:37 PM