2012-07-03 88 views
0

可能重複:
Read Text file Programmatically using Objective-C閱讀從.txt文件 - 目標C

我使用的代碼以下行::

- (void)sync:(NSString *)savedName{ 

NSLog(@"saved name is this :: %@", savedName); 

NSString *savedDir = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"]; 
NSString *saveFilePath = [savedDir stringByAppendingPathComponent:savedName]; 
saveFilePath = [saveFilePath stringByAppendingPathComponent:@"edit.txt"]; 

NSString *saveString = [NSString stringWithContentsOfFile:saveFilePath encoding:NSUTF8StringEncoding error:nil]; 

NSLog(@"saveString is :: %@ savedName : %@ savefilepath : %@", saveString, savedName, saveFilePath); 
} 

的內容edit.txt是:

= WWWW = 
[[Category:ssss]] 

我想要做的是從文件edit.txt中讀取並將其內容存儲在一個字符串中。但是,在NSLogging上,我得到nullsaveString

我在gdb以下輸出::

splitView[838:f803] saveString is :: (null) savedName : xxxx savefilepath : /Users/xxxx/Library/Application Support/iPhone Simulator/5.1/Applications/D4B3A4CF-E7D0-4D25-B3D3A170A329/Documents/xxxx/edit.t‌​xt` 

有人可以幫助我理清錯誤?我無法弄清楚。感謝致敬。

+0

爲什麼你'savedName'參數空?你應該先檢查一下。 – borrrden

+0

它不爲空..它是'xxxx' – kamalbhai

+0

噢拍我混了up savedName和saveString nevermind – borrrden

回答

1

好是如下

NSString *path; 
path = [[NSBundle mainBundle] pathForResource: YOUR_TEXT_FILE_NAME ofType: @"txt"]; 

現在函數讀取返回NSString作爲返回對象的文件。 我在此函數調用是如下

NSString *data = [self readFile: path]]; 

現在的功能

-(NSString *)readFile:(NSString *)fileName 

{ 
    NSLog(@"readFile"); 

    NSString *appFile = fileName;  
    NSFileManager *fileManager=[NSFileManager defaultManager]; 
    if ([fileManager fileExistsAtPath:appFile])   
    { 
     NSError *error= NULL; 
     NSString *resultData = [NSString stringWithContentsOfFile: appFile encoding: NSUTF8StringEncoding error: &error]; 
     if (error == NULL)    
      return resultData; 
    } 
    return NULL; 
} 

希望這將幫助你

編碼快樂:)

+0

我應該如何在這裏定義我的路徑?在我的代碼中..我想我已經正確定義了我的路徑。 – kamalbhai

+0

只是用這個從文件中讀出不保存任何文件 –

+0

即只用於'NSString * saveString = [self readFile:path]];'其中'path'與上面給出的相同, 'YOUR_TEXT_FILE_NAME'' –

1

我認爲你的TXT文件路徑是錯誤的。檢查了這一點,你正在構建的saveFilePath的方式,也許:

NSString *savedDir = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"]; 
NSString *saveFilePath = [savedDir stringByAppendingPathComponent:savedName]; 
saveFilePath = [saveFilePath stringByAppendingPathComponent:@"edit.txt"]; 
+0

哦對不起..我的壞..但仍然'saveString'出來是空!我編輯了我的問題。 – kamalbhai

1

如果歐嘗試:

NSString *savedDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; 
NSString *saveFilePath = [savedDir stringByAppendingPathComponent:savedName]; 
//Why do you do that ? saveFilePath = [savedDir stringByAppendingPathComponent:@"edit.txt"]; 

NSString *saveString = [NSString stringWithContentsOfFile:saveFilePath encoding:NSUTF8StringEncoding error:nil]; 

NSLog(@"saveString is :: %@ savedName : %@ savefilepath : %@", saveString, savedName, saveFilePath); 
+0

我有超過1個。txt文件在'savedName'文件夾中。所以,我嘗試使用我的.txt文件的特定名稱。 – kamalbhai

+0

我也試過你的代碼..它仍然是空的。但是,你能向我解釋一下你的第一行嗎? – kamalbhai

+0

當我讀取您的代碼時,我懷疑您要查看應用程序的Document目錄。也許我錯了,但如果不是,我的代碼更常見於此目錄。請打印所有路徑(包含您的文件名)並向我們顯示。 – Pierre

1

檢查文檔是否含有edit.txt文件。

編輯:

- (void)sync:(NSString *)savedName{ 

    NSString *filePath = [self applicationDocumentsDirectory]; 
    filePath = [filePath stringByAppendingPathComponent:@"license.txt"]; 
    NSString *saveString = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil]; 

    NSLog(@"saveString is :: %@ savedName : %@ savefilepath : %@", saveString, savedName, filePath); 
} 

LICENSE.TXT的路徑是:/Users/nuzhat.zari/Library/Application支持/ iPhone模擬器/ 5.0 /應用/ 9BFCE2B5-C976-4EF6-91DF-DA4ABCD04788 /文檔。

確保您的文件路徑也與此類似。那麼我用來讀取.txt文件數據

+0

是的..它包含'.txt。文件 – kamalbhai

+0

你可以給你的edit.txt的路徑? –

+0

我在我編輯的問題中發佈了'path'作爲'savefilePath'在我的'NSLog'返回中。 – kamalbhai