从文件读取C ++放三个怪异的字符 [英] C++ reading from file puts three weird characters

查看:143
本文介绍了从文件读取C ++放三个怪异的字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我通过字符串从文件字符串中读取时,>>操作获取第一个字符串,但是它以i开头。假设第一个字符串是街道,比起istreet。



其他字符串都可以。我尝试了不同的txt文件。结果是一样的。第一个字符串以我开始。问题是什么?



这是我的代码:

 #包括< iostream> 
#include< fstream>
#include< string>
#include< vector>
使用namespace std;

int立方体(int x){return(x * x * x);}

int main(){
$ b $ int maxChar;
int lineLength = 0;
int cost = 0;

cout<<输入每行最大字符数...;
cin>> maxChar;
cout< endl<<每行的最大字符数是:<<< maxChar<< endl;

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

if(!inFile){
cerr<< 无法打开文件datafile.txt;
exit(1); (!inFile.eof()){
字符串单词; //调用系统停止
}



inFile>>字;
cout<<单词<< endl;
cout<<< word.length()<< endl; (word.length()+ lineLength< = maxChar){
lineLength + =(word.length()+1);
}
else {
cost + = cube(maxChar-(lineLength-1));
lineLength =(word.length()+ 1);




$ b $ div class =h2_lin>解决方法

您看到一个UTF-8 字节顺序标记(BOM)。它是由创建该文件的应用程序添加的。



要检测并忽略标记,您可以尝试这个(未测试)函数:


$ b $ Skip $




$ char test [4] = {0};
in.read(test,3);
if(strcmp(test,\xEF\xBB\xBF)== 0)
return true;
in.seekg(0);
返回false;
}


When i read from a file string by string, >> operation gets first string but it starts with "i" . Assume that first string is "street", than it gets as "istreet".

Other strings are okay. I tried for different txt files. The result is same. First string starts with "i". What is the problem?

Here is my code :

#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);
    }   
}

}

解决方案

You're seeing a UTF-8 Byte Order Mark (BOM). It was added by the application that created the file.

To detect and ignore the marker you could try this (untested) function:

bool SkipBOM(std::istream & in)
{
    char test[4] = {0};
    in.read(test, 3);
    if (strcmp(test, "\xEF\xBB\xBF") == 0)
        return true;
    in.seekg(0);
    return false;
}

这篇关于从文件读取C ++放三个怪异的字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆