簡體   English   中英

XCode,標准C ++和ifstream文件處理

[英]XCode, standard C++ and ifstream file handling

最近,我發現Xcode可用於編寫普通的C ++程序。

我的問題是與以下代碼:

#include <iostream>
#include <fstream>
using namespace std;
int main (int argc, char *argv[]) {
    char operation='0';
    int operand=0;
    //open file for reading  
    ifstream ifile;
    ifile.open (argv[1]);
    if(!ifile.is_open()){
            cout<<"Invalid file\n";
            exit(1);
    }
    while(ifile.good())
    {
            ifile>>operation;

            if(operation=='I') {  
                    ifile>>operand;
                    cout<<"Inserting :"<<operand<<endl;
                                                    }
            else
            if(operation=='D') { cout<<"Deleting  "<<endl;
                                                    }
    }
    return 0;
}

當輸入以下內容時,

I 4
I 8
I 10
I 9
*

當我在Xcode中編譯並運行時,得到以下信息

Loading program into debugger…
Program loaded.
run
[Switching to process 58116]
Inserting :0
Running…

Debugger stopped.
Program exited with status value:0.

我已將文件名設置為項目首選項中的參數。

但是,在終端中,我得到了

Inserting :4
Inserting :8
Inserting :10
Inserting :9

請幫助我理解為什么會這樣。

我不知道這是什么,但是重新安裝XCode解決了問題。

您可能需要在項目首選項參數中指定一個絕對路徑。

首先作為調試時的標准規則。 消除所有不必要的復雜性

我懷疑您的問題是您沒有在調試參數中正確指定argv[1] ,但是我可能錯了。

首先,嘗試手動指定文件名,而不是從argv [1]接受文件名。 (相對路徑應來自“ project / build / Debug”目錄)

你需要檢查ifile.good()嘗試讀取字節,只有這樣,你知道,如果EOF閱讀時被擊中。

最后,在讀取argv []時始終檢查argc

使用“編輯活動可執行文件”來訪問工作目錄和命令行參數的設置。

不幸的是,XCode具有一些冗余的項目設置,您需要打開正確的窗口。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM