从文件C ++读取时出现奇怪的字符

时间:2019-12-01 02:27:08

标签: c++ file parsing split

从inv.txt文件读取内容时,我一开始总是出现奇怪的字符。我用谷歌搜索了一段时间,但找不到解决方案。以下是我的代码以及文件内容

void installDataToInventory(string fileName) {


// Open file to read data
std::ifstream myFile;
myFile.open(fileName);

// Continue if file is open 
if (myFile.is_open()) {
    std::string line;
    // initial count to for putting book info 
    // to approriate slot of inventory array 
    int count = 0;

    // Loop through the end of the file 
    while (getline(myFile, line)) {
        // Ignore empty line in data file
        if (line == "") continue; 
        std::stringstream iss_line(line);
        iss_line.clear();
        std::cout << line << std::endl;
        //std::cout << iss
        // declare array with size 10 
        // each slot of array represend one field in Book 
        // book[0] - ISBN
        // book[1] - title 
        // book[2] - author 
        // book[3] - publisher
        // book[4] - quantity 
        // book[5] - wholesale 
        // book[6] - retail price
        // book[7] - day
        // book[8] - month
        // book[9] - year 
        std::string arrayString[10];

        for (int i = 0; i < 10; i++) {
            std::getline(iss_line, arrayString[i], '|');
        }

    } 
 }   

下面是inv.txt的内容

978-1-60309-2395M|merican Elf 1999|James Kochalka|New York|465|12.53|14.94|22|2|1994
978-1-891830-37-2|The Barefoot Serpent (softcover)|Scott Morse|New York|338|20.41|23.06|29|9|1993
978-1-891830-40-2|Beach Safari|Mawil|Berkley|358|20.2|22.91|16|5|1994
978-1-891830-56-3|Bighead|Jeffrey Brown|Berkley|274|20.95|22.41|1|1|2017
978-1-891830-19-8|Box Office Poison|Alex Robinson|Berkley|576|19.09|21.21|13|1|2019
978-0-9713597-6-5|Clumsy|Jeffrey Brown|Berkley|878|16.02|18.92|6|10|1994
978-1-891830-81-5|Cry Yourself to Sleep|Jeremy Tinder|Berkley|63|11.6|14.54|13|3|2007
978-1-891830-12-9|Dear Julia|Hoa Handsome Ly|Berkley|517|12.91|16.2|14|11|2008
978-1-891830-92-1|Death Chocolate|Chocolate|Berkley|877|17.96|20.3|8|5|1999
978-1-891830-77-8|Every Girl is the End of the World for Me|Jeffrey Brown|Pittsburgh Publisher|524|14.25|16.17|16|2|2013
978-0-9585783-4-9|From Hell|Alan Moore|Pittsburgh Publisher|262|19.54|21.25|10|11|2000
978-1-891830-02-0|Hey Mister Vol 1|Mister|Pittsburgh Publisher|520|16.5|17.56|28|5|2001
978-1-891830-25-9|Hey Mister Vol 2|Mister|Pittsburgh Publisher|881|14.07|17.44|13|9|2012
978-1-891830-17-4|Hutch Owen (Vol 1): The Collected|Tom Hart|Pittsburgh Publisher|645|19.47|23.3|29|10|1999
978-1-891830-55-6|Hutch Owen (Vol 2): Unmarketable|Tom Hart|Pittsburgh Publisher|890|12.56|14.03|24|8|1997
978-1-891830-65-5|The King |Rich Koslowski|Pittsburgh Publisher|219|17.88|20.56|19|11|2014
978-1-891830-51-8|Less Than Heroes|David Yurkovich|Pittsburgh Publisher|218|16.61|19.04|3|8|2015
978-1-891830-69-3|Lone Racer|Nicolas Mahler|Sam Club|349|20.31|23.62|1|2|2014
978-1-891830-33-4|Magic Boy and the Robot Elf|James Kochalka|Sam Club|156|20.56|22.07|12|11|1991
978-1-891830-45-7|The Mirror of Love|Alan Moore|Sam Club|406|19.45|23.27|6|8|2002
978-1-891830-36-5|Monkey vs. Robot (Vol 2): Crystal of Power|Koch|Sam Club|94|18.33|22.02|7|3|2002
978-1-891830-68-6|Mosquito|Dan James|Sam Club|957|11.36|13.51|28|12|1997
978-1-891830-29-7|Pinky & Stinky|James Kochalka|Sam Club|725|14.93|17.54|24|8|2018
978-1-891830-14-3|Speechless|Peter Kuper|Sam Club|774|20.24|22.12|29|6|2006
978-1-891830-50-1|Spiral-Bound|Aaron Renier|Sam Club|594|15.7|18.9|5|9|2018
978-1-891830-31-0|Three Fingers|Rich Koslowski|Sam Club|584|12.12|14.15|27|2|2017
978-1-891830-70-9|The Ticking|Renée French|New York Times|221|18.4|22|16|2|2005
978-1-891830-04-4|Top Shelf #7: On Parade Edited|Warnock|New York Times|264|20.65|24.6|20|3|2013
978-1-891830-11-2|Top Shelf #8: Under the Big Top|Brett Warnock|New York Times|245|19.69|23.23|3|8|2016
978-1-891830-32-7|Top Shelf #9: Asks the Big Questions Edited|Warnock|New York Times|503|18.6|21.36|6|2|2002
978-1-891830-73-0|Tricked|Alex Robinson|New York Times|102|14.15|17.06|18|3|1999
978-1-891830-41-9|Unlikely|Jeffrey Brown|New York Times|216|19.64|23.63|1|5|2003
978-1-891830-38-9|Van Helsing's Night Off|Nicolas Mahler|New York Times|178|11.91|15.87|29|10|2012
978-1-60309-035-3|Voice of the Fire|Alan Moore|New York Times|705|18.39|20.11|1|5|2005
978-1-891830-72-3|Will You Still Love Me If I Wet the Bed|Liz Prince|New York Times|832|15.7|17.76|8|12|2019

并且,下面是输出图片,在打印出来时,它在开头处有一个奇怪的字符。 enter image description here

1 个答案:

答案 0 :(得分:0)

如果您在与要读取文件的操作系统不同的操作系统上创建文件,则很可能会遇到编码问题。有多种解决方法,其中一些方法是:清除正在读取的缓冲区将您的文件放入,解析文件中超出范围的ASCII字符,然后重新创建文件。