String C++;为什么我的新行被曲解了? 我正在编写一个C++程序驱动程序,最终需要把两个字符串传递给我在一个单独的文件中写入的函数。我正在从如下格式的文件中读取数据: ac: and amo: love amor: love animal: animal annus: year ante: before, in front of, previously antiquus: ancient ardeo: burn, be on fire, desire arma: arms, weapons atque: and aurum: gold aureus: golden, of gold aurora: dawn

String C++;为什么我的新行被曲解了? 我正在编写一个C++程序驱动程序,最终需要把两个字符串传递给我在一个单独的文件中写入的函数。我正在从如下格式的文件中读取数据: ac: and amo: love amor: love animal: animal annus: year ante: before, in front of, previously antiquus: ancient ardeo: burn, be on fire, desire arma: arms, weapons atque: and aurum: gold aureus: golden, of gold aurora: dawn,string,file,newline,String,File,Newline,我试着把拉丁语的单词放到一个字符串中,把英语的对应词放到另一个字符串中。此外,每次我得到一个英语等价物时,我希望能够将这两个字符串发送到我的函数。我的代码当前如下所示: #include <iostream> #include <fstream> #include <string> using namespace std; //#include "tree.h" int main(int argc, char* argv[]) { string

我试着把拉丁语的单词放到一个字符串中,把英语的对应词放到另一个字符串中。此外,每次我得到一个英语等价物时,我希望能够将这两个字符串发送到我的函数。我的代码当前如下所示:

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

//#include "tree.h"

int main(int argc, char* argv[])
{
    string latinWord   = "",
           englishWord = "";

    char   buffer;

    bool   isLatinWord = true;

    ifstream vocabFile;
    vocabFile.open(argv[1]);

    if (!vocabFile)
       cout << "File open failed." << endl;

    while(vocabFile.get(buffer))
    {

        if (isLatinWord)
        {
            if (buffer == ':')
                isLatinWord = false;

            else
                latinWord+= buffer;
        }

        else
        {
            if (buffer == ',') // indicates 1 of multiple equivs processed
            {
                cout << englishWord << " = " << latinWord << endl;
                englishWord = "";
            }

            else if (buffer == '\n') // indicates all english equivs processed
            {
                cout << englishWord << " = " << latinWord << endl;
                isLatinWord = true;
                englishWord = latinWord = ""; // reset both strings
            }

            else
                englishWord+= buffer;
        }
    }
}
[EDIT]这是修复isLatinWord标志后我的输出。 我认为我的代码以错误的方式识别换行符,我想知道是否有人看到任何错误或有任何建议

谢谢,
Ben

新行也可以表示为\r\n字符,我也会检查这一点。

使用
getline
读取(部分)行直到所需的分隔符:

#include<iostream>
#include<fstream>
#include<sstream>
using namespace std;

int main() {
    string word;
    ifstream data("data.txt");

    string latin_word;
    while(getline(data,latin_word,':')) { // Read up to, but not including, the colon. But it does *discard* the colon
        cout << "Read latin word: <" << latin_word << '>' << endl;
        // Read the rest of the line
        string rest_of_line;
        getline(data, rest_of_line);
        // Now, we want to split it on commas. Easiest way is to build a stream object wrapped around this string
        istringstream rest_of_line_stream(rest_of_line);
        string english_phrase;
        while(
                  rest_of_line_stream >> std:: ws, 
                  getline(rest_of_line_stream, english_phrase,',')
             ) {
            cout << '@' << latin_word << "@\t@" << english_phrase << '@' << endl;
        }
    }
}
#包括
#包括
#包括
使用名称空间std;
int main(){
字符串字;
ifstream数据(“data.txt”);
字符串拉丁字母;
while(getline(data,拉丁文单词,,:){//读取到冒号,但不包括冒号。但它确实*放弃*冒号
不能这条线

latinWord = true;
应该是

isLatinWord = true;

一种可能更高级别的方法是读取文件中的每一行。然后根据以下内容“拆分”每一行:分隔符。我将首先创建一个函数来读取每一行,尽管API中可能已经有一行了,然后我将编写另一个函数来分隔冒号处的行,尽管API中也可能已经有一行了。您所做的第一次读取操作是将单个字符添加到
缓冲区
。这是一个奇怪的设计我想问题是,代码中的换行条件在到达第一个英文单词之前被认为是真的。我应该编写一些代码来避免\r\n字符吗?理论上,C++库应该识别操作系统C的“标准”换行顺序。因此,我认为,你不应该考虑它。但是Windows上的新行是非常奇怪的!我会写一个if语句,用于R\N,让我们看看这是否解决了你的问题,无论如何,AaronMcDaid,是的,你应该是正确的,但是使用下面的GETLINE,而不是用缓冲区读取。因此,即使在那时,我也看到它不起作用了!:-/谢谢!它似乎有一些正确的逻辑,但仍然不能解决我的换行问题。出于某种原因,一些英语单词显示得很好,但其他单词只是空白!
isLatinWord = true;