相关疑难解决方法(0)

从文件中读取C++会产生三个奇怪的字符

当我通过字符串从文件字符串中读取时,>> operation获取第一个字符串,但它以"i"开头.假设第一个字符串是"street",而不是"istreet".

其他字符串也没关系.我尝试了不同的txt文件.结果是一样的.第一个字符串以"i"开头.问题是什么?

这是我的代码:

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;

int cube(int x){ return (x*x*x);}

int main(){

int maxChar;
int lineLength=0;
int cost=0;

cout<<"Enter the max char per line... : ";
cin>>maxChar;
cout<<endl<<"Max char per line is : "<<maxChar<<endl;

fstream inFile("bla.txt",ios::in);

if (!inFile) {
    cerr << "Unable to open file datafile.txt";
    exit(1);   // call system to stop
}

while(!inFile.eof()) {
    string word;

    inFile >> word;
    cout<<word<<endl;
    cout<<word.length()<<endl;
    if(word.length()+lineLength<=maxChar){
        lineLength +=(word.length()+1);
    }
    else {
        cost+=cube(maxChar-(lineLength-1));
        lineLength=(word.length()+1); …
Run Code Online (Sandbox Code Playgroud)

c++ file-io byte-order-mark

4
推荐指数
1
解决办法
3845
查看次数

Perl 两个相同的字符串比较为不相等

这是我的代码:

    my $self = shift;
    my $h    = shift;

    print "$h\n";

    my @headers = split /,/, $h;

    foreach my $el (@{$expected}) {
            my $t = shift @headers;
            chomp ($t);
            chomp ($el);
            print Dumper($el cmp $t, $el, $t);
            print "test: \'$el\' eq \'$t\' ";
            unless ($el eq $t) {
                    print "not ok $el ne $t\n";
                    return 0;
            } else {
                    print "ok\n";
            }
    }
    return 1;
Run Code Online (Sandbox Code Playgroud)

在我的第一个单元测试中,我将一个字符串传递给 $h,该字符串与 $expected 匹配。然后我有一个单元测试,它确保函数在传递不匹配的字符串时失败。这两个测试的行为符合预期。

服务器、Jira 项目、问题类型、摘要、描述、受托人、标签、Epic 链接

$VAR1 = 0;

$VAR2 = '服务器';

$VAR3 = …

perl string-comparison

2
推荐指数
1
解决办法
138
查看次数

标签 统计

byte-order-mark ×1

c++ ×1

file-io ×1

perl ×1

string-comparison ×1