gpt4 book ai didi

c++ - WriteFile 什么都不做

转载 作者:行者123 更新时间:2023-11-28 07:59:19 26 4
gpt4 key购买 nike

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

这是我的代码:方法

//---------------------------------------------------------------------------------------
// 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();

}

如果还需要什么,尽管问,我会发帖。

最佳答案

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

关于c++ - WriteFile 什么都不做,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11915745/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com