ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 파일 C에서 읽는 동안 이상한 문자
    카테고리 없음 2020. 8. 12. 16:39

    질문

    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

    그리고 아래는 출력 사진인데, 출력시 처음에 이상한 문자가 있습니다. 여기에 이미지 설명 입력

     

    답변1

    읽고 있는 OS와 다른 OS에서 파일을 생성하는 경우 인코딩 문제와 같은 문제가 발생할 수 있습니다. , 범위를 벗어난 ASCII 문자에 대해 파일을 파싱하고 없는 파일을 다시 만듭니다.



     

     

     

     

    출처 : https://stackoverflow.com/questions/59122051/weird-character-while-reading-from-file-c

    댓글

Designed by Tistory.