首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >C++为什么我的新词被误解了?

C++为什么我的新词被误解了?
EN

Stack Overflow用户
提问于 2014-12-03 21:04:34
回答 3查看 64关注 0票数 1

我正在用c++编写一个驱动程序,它最终需要将两个字符串传递给我在一个单独文件中编写的函数。我从一个格式化为以下格式的文件中读取数据:

代码语言:javascript
复制
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

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

代码语言:javascript
复制
#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;
        }
    }
}

它的工作方式应该是,如果有冒号,则表示拉丁语字符串已完成填充(标志设置为false),然后应该开始填充英文字串。应该填充英语单词字符串,直到逗号被击中(此时将单词发送到函数),或命中换行符(重置标志,因为此时检查了所有的英语等价物)。

但是,当我试图输出字符串时,我会将字符串发送给我的函数--它们完全搞砸了。

这是我的输出:

代码语言:javascript
复制
$ ./prog5 latin.txt
 = ac
 = amo
 = amor
 = animal
 = annus
 before = ante
 in front of = ante
 = anteusly
 = antiquus
 burn = ardeo
 be on fire = ardeo
 = ardeo
 arms = arma
 = armas
 = atque
 = aurum
 golden = aureus
 = aureus
 = aurora

编辑--这是我在修复isLatinWord标志之后的输出。--我认为我的代码以错误的方式识别换行符,我想知道是否有人看到任何错误或有任何建议?

谢谢,本

EN

回答 3

Stack Overflow用户

发布于 2014-12-03 21:08:51

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

票数 1
EN

Stack Overflow用户

发布于 2014-12-03 21:34:55

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

代码语言:javascript
复制
#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;
        }
    }
}

更新:我忘了放弃足够多的空白。默认情况下,getline保留任何前导空格。这可能是数据中的:,之后出现的问题。因此,在尝试阅读英语短语之前,我使用rest_of_line_stream >> std:: ws来读取和丢弃任何空白。

内部while循环可能看起来有点奇怪。在while括号中有两件事:rest_of_line_stream >> std:: wsgetline(rest_of_line_stream, english_phrase,',')。用逗号分隔,这是C和C++中的逗号运算符。基本上,它只是意味着第一件事被评估,但是它的结果被忽略了。用于while循环的布尔值则是getline(rest_of_line_stream, english_phrase,',')的结果。

票数 0
EN

Stack Overflow用户

发布于 2014-12-03 21:37:19

这条线

代码语言:javascript
复制
latinWord = true;

应该是

代码语言:javascript
复制
isLatinWord = true;
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27281602

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档