自从我开始记录文件的详细信息后发生了什么? 我在C 7窗口中使用C++编写了一个程序:B.:这里是我用来检查文件中的行数的代码。第一部分是gotoline函数的声明,接下来是代码中我计算文件中行数的部分。在我开始添加日志记录过程之前,一切都很顺利。可能我记录的信息有误,或者在一个函数中打开和关闭了太多的文件?如果您能帮我找出错误,我将不胜感激,我很乐意提供更多关于我的问题的细节或澄清,请尽管问。我还在学习C++,所以我很困惑为什么我的程序停止计数。 std::ifstream& GotoLine(std::ifstream& file, unsigned int num) { file.seekg(std::ios::beg); for(int i=0; i < num - 1; ++i) { file.ignore(std::numeric_limits<std::streamsize>::max(),'\n'); } return file; } ifstream productguide; productguide.open ("MasterProductGuide.csv"); if (productguide.good()) { c = productguide.get(); if (c=='\n')x++; ofstream log; log.open ("log.txt", ofstream::app); if (log.good()) { time_t rawtime; struct tm * timeinfo; time ( &rawtime ); timeinfo = localtime ( &rawtime ); string loginfo; loginfo = asctime (timeinfo); log << "MarketManager Detected " << x << " # of lines in the Master Product Guide - " + loginfo; } else { cout << "There was an error creating the log file" << endl; cout << "The Program will now terminate" << endl; system("PAUSE"); return 0; } log.close(); } else { ofstream log; log.open ("log.txt", ofstream::app); if (log.good()) { time_t rawtime; struct tm * timeinfo; time ( &rawtime ); timeinfo = localtime ( &rawtime ); string loginfo; loginfo = asctime (timeinfo); log << "MarketManager Failed to oped the Master Product Guide - " + loginfo; } else { cout << "There was an error creating the log file" << endl; cout << "The Program will now terminate" << endl; system("PAUSE"); return 0; } log.close(); cout << "The Program will now terminate" << endl; system("PAUSE"); return 0; } productguide.close(); std::ifstream和GotoLine(std::ifstream和file,unsigned int num) { seekg(std::ios::beg); 对于(int i=0;i

自从我开始记录文件的详细信息后发生了什么? 我在C 7窗口中使用C++编写了一个程序:B.:这里是我用来检查文件中的行数的代码。第一部分是gotoline函数的声明,接下来是代码中我计算文件中行数的部分。在我开始添加日志记录过程之前,一切都很顺利。可能我记录的信息有误,或者在一个函数中打开和关闭了太多的文件?如果您能帮我找出错误,我将不胜感激,我很乐意提供更多关于我的问题的细节或澄清,请尽管问。我还在学习C++,所以我很困惑为什么我的程序停止计数。 std::ifstream& GotoLine(std::ifstream& file, unsigned int num) { file.seekg(std::ios::beg); for(int i=0; i < num - 1; ++i) { file.ignore(std::numeric_limits<std::streamsize>::max(),'\n'); } return file; } ifstream productguide; productguide.open ("MasterProductGuide.csv"); if (productguide.good()) { c = productguide.get(); if (c=='\n')x++; ofstream log; log.open ("log.txt", ofstream::app); if (log.good()) { time_t rawtime; struct tm * timeinfo; time ( &rawtime ); timeinfo = localtime ( &rawtime ); string loginfo; loginfo = asctime (timeinfo); log << "MarketManager Detected " << x << " # of lines in the Master Product Guide - " + loginfo; } else { cout << "There was an error creating the log file" << endl; cout << "The Program will now terminate" << endl; system("PAUSE"); return 0; } log.close(); } else { ofstream log; log.open ("log.txt", ofstream::app); if (log.good()) { time_t rawtime; struct tm * timeinfo; time ( &rawtime ); timeinfo = localtime ( &rawtime ); string loginfo; loginfo = asctime (timeinfo); log << "MarketManager Failed to oped the Master Product Guide - " + loginfo; } else { cout << "There was an error creating the log file" << endl; cout << "The Program will now terminate" << endl; system("PAUSE"); return 0; } log.close(); cout << "The Program will now terminate" << endl; system("PAUSE"); return 0; } productguide.close(); std::ifstream和GotoLine(std::ifstream和file,unsigned int num) { seekg(std::ios::beg); 对于(int i=0;i,c++,windows,codeblocks,C++,Windows,Codeblocks,看看这段代码,我看到你的日志一直被append打开,所以最终它会变得很大 我不能说这段代码会失败没有其他原因,但大于2gb的文件肯定可以做到这一点。我不记得append模式是否有问题,但在内部它可能使用seek。seek肯定有大文件问题。在linux上,你会使用fseek64函数系列。Windows有为了不兼容,它自己的版本带有下划线 当您处理流时,您还应该检查它的错误标志,当出现问题时,它实际上不会抛出。您在打开后进行检查,但其他操作也可能失败 更具体地说,调用std::istream::ig

看看这段代码,我看到你的日志一直被append打开,所以最终它会变得很大

我不能说这段代码会失败没有其他原因,但大于2gb的文件肯定可以做到这一点。我不记得append模式是否有问题,但在内部它可能使用seek。seek肯定有大文件问题。在linux上,你会使用
fseek64
函数系列。Windows有为了不兼容,它自己的版本带有下划线

当您处理流时,您还应该检查它的错误标志,当出现问题时,它实际上不会抛出。您在打开后进行检查,但其他操作也可能失败

更具体地说,调用
std::istream::ignore
。您的
GotoLine
不会检查eof。从设计角度来看,我对此相当怀疑。它可能会起作用,但肯定不会通过代码审查


作为挑剔,错误应该打印到
std::cerr
并返回1::)

我什么都不懂。你能发布这个源文件的完整版本吗?因为我为了可读性而将文件分解,所以它几乎有1000行。有没有更好的方法不发布就发送它。这两段代码对我来说看起来不错。所以我认为我遇到的问题是s在另一个地方,我们需要查看实际的代码。例如,无法告诉您如何计算您发布的示例中的行数。如果您的代码太长,请将其减少到可以复制问题的最小大小。这样做可能会发现问题。@Rob,对于非常大的代码示例,您可以使用s我喜欢。