Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/161.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ mac上的ifstream_C++_Macos_Ofstream - Fatal编程技术网

C++ mac上的ifstream

C++ mac上的ifstream,c++,macos,ofstream,C++,Macos,Ofstream,我试图读入一个随机文件(在macxcode上),并确定文档中字母k的实例。然后将数字打印为输出文件。我的问题是outfile没有被写入,nums_k返回为0。我不确定ifstream是否工作不正确,或者ofstream是否需要建立不同的文件名。这是我的源代码 #include <iostream> #include <fstream> #include <string> using namespace std; int main() {

我试图读入一个随机文件(在macxcode上),并确定文档中字母k的实例。然后将数字打印为输出文件。我的问题是outfile没有被写入,nums_k返回为0。我不确定ifstream是否工作不正确,或者ofstream是否需要建立不同的文件名。这是我的源代码

#include <iostream> 
#include <fstream> 
#include <string>  
using namespace std;  

int main() { 

    ifstream infile("Users/bryanmichaelnorris/documents/extra credit    assignment.docx"); 

    string line; 
    int numks = 0;  

    while (getline(infile,line)) {     
         int x = 0;         
         for (std::string::iterator it=line.begin(); it!=line.end(); ++it) {            
             if (line[x] == 'k') {                 
                  numks++;            
             }            
             x++;         
         }     
    }         

    infile.close();     
    ofstream outfile("number of k's.docx");   
    outfile << "There are " << numks << " K's in the file." << endl;
    outfile.close();        
    return 0;
}
#包括
#包括
#包括
使用名称空间std;
int main(){
ifstream infle(“用户/bryanmichaelnorris/documents/extra credit assignment.docx”);
弦线;
int numks=0;
while(getline(infle,line)){
int x=0;
对于(std::string::iterator it=line.begin();it!=line.end();++it){
如果(第[x]行='k'){
numks++;
}            
x++;
}     
}         
infle.close();
出流口的数量(“k’s.docx的数量”);

outfile为打开的文件添加了验证

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main()
{
    const char * csInputFileNane="Users/bryanmichaelnorris/documents/extra credit    assignment.docx";
    ifstream infile(csInputFileNane);
        if (!infile.is_open()) {
        cerr << "Cannot open file \""<<csInputFileNane<<'"'<<endl;
        return -1;
    }
    string line;

    int numks = 0;

    while (getline(infile,line))
    {   int x = 0;
        for (std::string::iterator it=line.begin(); it!=line.end(); ++it)              {
            if (line[x] == 'k')
            {
                numks++;
            }
            x++;
        }
    }
    infile.close();
    const char *csOutFileName="number of k's.docx";
    ofstream outfile(csOutFileName);
    if (!outfile.is_open()) {
        cerr << "Cannot open file \""<<csOutFileName<<'"'<<endl;
        return -1;
    }
    outfile << "There are " << numks << " K's in the file." << endl;
    outfile.close();
    return 0;

}
#包括
#包括
#包括
使用名称空间std;
int main()
{
const char*csInputFileNane=“用户/bryanmichaelnorris/documents/extra credit assignment.docx”;
ifstream infile(csInputFileNane);
如果(!infle.is_open()){

cerr
“用户/…”
,首先我会在名称的开头加上一个
/
。如果您想验证输入文件是否正确打开,那么测试它肯定不会有什么坏处,而不是假设世界上一切正常。您可能还想研究输出文件的写入位置,作为在Xco下运行时的当前工作目录de通常与人们认为的完全不同(但您可以在模式编辑器中更改它)@WhozCraig几乎肯定是一针见血了不要对不是MS Word文档的文件使用
.docx
扩展名…如果你真的需要扩展名,也许
.txt
是一个更好的扩展名。如果输入文件也是MS Word文件,那么我不会这样计算字母“k”,因为你可能有“k”用于某些东西ng而不是文本本身。顺便说一句:
k
为零表示输入失败,因此输出完全不相关。下次尝试将您的问题降至最低限度。这也是网站规则要求的原因。这是我在这里提出的第一个问题,对格式设置带来的不便表示歉意。