سؤال

وطلب آخر آسف .. الآن أنا قراءة الرموز في واحدة تلو الأخرى، وأنه يعمل، ولكن أريد أن أعرف عندما يكون هناك خط جديد ..

وإذا احتوى الملف الخاص بي

Hey Bob
Now

يجب أن تعطيني

Hey
Bob
[NEW LINE]
NOW

هل هناك طريقة للقيام بذلك دون استخدام getline؟

هل كانت مفيدة؟

المحلول

ونعم المشغل >> عند استخدامها مع سلسلة قراءة "المساحة البيضاء" فصل الكلمات. يتضمن A 'المساحة البيضاء "علامة التبويب الفضاء وأحرف سطر جديد.

إذا كنت ترغب في قراءة سطر في استخدام الوقت الأمراض المنقولة جنسيا :: getline ()
الخط يمكن بعد ذلك برموز وحدة مع تيار السلسلة.

std::string   line;
while(std::getline(std::cin,line))
{

    // If you then want to tokenize the line use a string stream:

    std::stringstream lineStream(line);
    std::string token;
    while(lineStream >> token)
    {
        std::cout << "Token(" << token << ")\n";
    }

    std::cout << "New Line Detected\n";
}

وبالإضافة الصغيرة:

دون استخدام getline ()

وهكذا كنت تريد حقا أن تكون قادرة على الكشف عن سطر جديد. وهذا يعني أن السطر يصبح نوع آخر من رمزية. لذلك لنفترض أن لديك كلمات مفصولة "المسافات" كرموز والسطر الجديد كما المنوال ذاته.

وبعد ذلك يمكنك إنشاء نوع رمزية.
ثم كل ما عليك القيام به هو كتابة مشغلي تيار لرمز:

#include <iostream>
#include <fstream>

class Token
{
    private:
        friend std::ostream& operator<<(std::ostream&,Token const&);
        friend std::istream& operator>>(std::istream&,Token&);
        std::string     value;
};
std::istream& operator>>(std::istream& str,Token& data)
{
    // Check to make sure the stream is OK.
    if (!str)
    {   return str;
    }

    char    x;
    // Drop leading space
    do
    {
        x = str.get();
    }
    while(str && isspace(x) && (x != '\n'));

    // If the stream is done. exit now.
    if (!str)
    {
        return str;
    }

    // We have skipped all white space up to the
    // start of the first token. We can now modify data.
    data.value  ="";

    // If the token is a '\n' We are finished.
    if (x == '\n')
    {   data.value  = "\n";
        return str;
    }

    // Otherwise read the next token in.
    str.unget();
    str >> data.value;

    return str;
}
std::ostream& operator<<(std::ostream& str,Token const& data)
{
    return str << data.value;
}


int main()
{
    std::ifstream   f("PLOP");
    Token   x;

    while(f >> x)
    {
        std::cout << "Token(" << x << ")\n";
    }
}

نصائح أخرى

وأنا لا أعرف لماذا كنت تعتقد std::getline هو سيئ. يمكنك لا تزال تعترف أسطر جديدة.

std::string token;
std::ifstream file("file.txt");
while(std::getline(file, token)) {
    std::istringstream line(token);
    while(line >> token) {
        std::cout << "Token :" << token << std::endl;
    }
    if(file.unget().get() == '\n') {
        std::cout << "newline found" << std::endl;
    }
}

وهذا هو وسيلة باردة، وأقل من ذلك بكثير مطول آخر جئت عبر لtokenize السلاسل.

vector<string> vec; //we'll put all of the tokens in here 
string token;
istringstream iss("put text here"); 

while ( getline(iss, token, '\n') ) {
       vec.push_back(token);
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top