首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >WriteFile什么也不做

WriteFile什么也不做
EN

Stack Overflow用户
提问于 2012-08-11 23:00:54
回答 1查看 168关注 0票数 0

嘿,我的write和readFile方法有个问题。这些方法不做任何事情。该方法假定将动态数组的元素写入名为C++的文件中。然后,对于读取的文件,该方法也应该读取ArrayList的元素。

下面是我的代码:

代码语言:javascript
复制
//---------------------------------------------------------------------------------------
//  Name:         Array::WriteFile
//  Description:  Writes an array to disk
//  Arguments:    The Filename
//  Return Value: true on success, false on failure
//---------------------------------------------------------------------------------------
   void writeFile(string file)//saves the array elements into a text file
    {
        //sort();
        ofstream outfile(file);//allows you to write to the document passed in
        for(int i = 0; i < m_size; i++)//loops through the array
        {
            outfile << m_array[i] << endl;//saves each element into a single line in the text document
        }
        outfile.close();
    }


//---------------------------------------------------------------------------------------
//  Name:         ReadFile.
//  Description:  Reads an array from disk.
//  Arguments:    The Filename.
//  Return Value: True on success, false on failure.
//---------------------------------------------------------------------------------------
   void readFile(string file)
    {
        ifstream inFile(file);//reads the file
        string line;//creates a string to take in information from the text deocument
        int numLines=0;//number of lines read in text document so far

        while(getline(inFile, line))//loops through the text document counting how many lines are in it.
        {
            numLines++;//increments every time a line is read
        }
        Datatype object;//creates a variable of type DataType to hold whatever is in the text document be it int, float, string etc...
        inFile.clear() ;//these two lines reset the inFile and go back to the start allowing to read through it again
        inFile.seekg(0, ios::beg) ;
        for(int i = 0; i < numLines; i++)//loops for whatever the number of lines there is in the document.
        {
            inFile >> object;//pushes the line into the document into object
            push(object);//calls the push function that will push object into the array in the right order
        }
        inFile.close();

    }

如果还有什么需要,请直接问我,我会发帖的。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-12-29 05:53:07

这样做的问题在于目录权限。因为它只是读而不是写,所以文档从未被写入。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11915745

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档