2010-02-02 2 views
0

내 응용 프로그램이 서버에서 가져 와서 로컬로 저장하는 텍스트 파일 (songlist.txt)이 있습니다. 내 응용 프로그램에서 파일의 내용을 사용하려고하면 예기치 않은 결과가 발생합니다. 이 문제를 조사했을 때 텍스트 파일의 NSString 내용의 끝 부분에 때로는 이상한 문자가 나타나는 것을 발견했습니다.
NSString의 끝 부분에있는 이상한 '가짜 캐릭터'는 무엇입니까?

왜 여기에 이상한 문자가 있습니까?

1) I
2) UPP †
3) MUL 4) ID


† : 문자열의 END 이상하게 문자

예 : (아래 내 소스이다) TEXTFILE의

// 정확한 내용 (songlist.txt)

노래 One.mp3
노래 Two.mp3당신이 가져올 때 노래 Three.mp3
노래 Four.mp3
노래 Five.mp3 그것은 인코딩과 같은

//Downloading Songlist from the internet, Saving it to Documents folder 
-(void)downloadSongList 
{ 
    NSString *songlistString; 

    NSString *URLString_list = @"http://et.bicdoss.com/app/songlist.txt"; 
    NSURL *URL_list = [NSURL URLWithString:[URLString_list stringByAddingPercentEscapesUsingEncoding:       NSASCIIStringEncoding]]; 

    // Get Data of Contents (for Write to Disk) 
    NSData *songData = [NSData dataWithContentsOfURL:URL_list]; 

    if(songData == nil) { 

     NSLog(@"SONGLIST.TXT NOT DOWNLOADED"); 

    }else{ 

    // Write Songlist.txt to disk 
    NSString *filePath = [[self documentsDirectory] stringByAppendingPathComponent:@"Music/songlist.txt"]; 
    [songData writeToFile:filePath atomically:YES]; 
     NSLog(@"SONGLIST.TXT DOWNLOADED"); 
    } 

} 

// Retrieve songlist.txt from local documents folder 
-(NSString*)getLocalSongList 
{ 
NSData *textFileData = [NSData dataWithContentsOfFile:[[self documentsDirectory] stringByAppendingPathComponent:@"Music/songlist.txt"]]; 
NSString *textFileString = [NSString stringWithUTF8String:[textFileData bytes]]; 

return textFileString;   
} 

//Create NSMutableArray of contents of songlist.txt (seperated by linebreaks) 

... 
NSMutableArray *files_catalog; 
    files_catalog = [[NSMutableArray alloc] init]; 
    [files_catalog addObjectsFromArray:[[self getLocalSongList] componentsSeparatedByString: @"\n"]]; 
    [files_catalog retain]; 

    for(int a = 0; a<[files_catalog count]; a++){ 
     NSLog(@"Contents of Object %i (files_catalog) %@", a, [files_catalog objectAtIndex:a]); 
/* 

NOTE: 
The above NSLog statement always gives strange output (note strange characters at the end of last filename:) 

(output) 
2010-02-02 21:22:27.145 SomeApp[19670:207] >>>>>>>>>>>>>>>>>>>> Cntnts of Objct 0 (files_catalog) Song One.mp3 
2010-02-02 21:22:27.145 SomeApp[19670:207] >>>>>>>>>>>>>>>>>>>> Cntnts of Objct 1 (files_catalog) Song Two.mp3 
2010-02-02 21:22:27.146 SomeApp[19670:207] >>>>>>>>>>>>>>>>>>>> Cntnts of Objct 2 (files_catalog) Song Three.mp3 
2010-02-02 21:22:27.146 SomeApp[19670:207] >>>>>>>>>>>>>>>>>>>> Cntnts of Objct 3 (files_catalog) Song Four.mp3 
2010-02-02 21:22:27.146 SomeApp[19670:207] >>>>>>>>>>>>>>>>>>>> Cntnts of Objct 4 (files_catalog) Song Five.mp3upp† 


Some other examples of strange characters at the end were: 

I 
upp† 
mul† 
ID 

*/ 
} 

... 

답변

2

는 엉망지고있다. stringWithContentsOfFile : encoding : error : 메소드를 사용해보십시오.

[NSString stringWithContentsOfFile:[[self documentsDirectory] stringByAppendingPathComponent:@"Music/songlist.txt"] encoding:NSUTF8StringEncoding error:nil]; 

더하기 NSData 단계를 건너 뜁니다.

관련 문제