gpt4 book ai didi

c++ - 在不同的 C++ 编译器中读取文件

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:24:05 24 4
gpt4 key购买 nike

编辑:最初我认为这是由于我测试程序的不同 IDE 造成的。问题似乎缩小到每个 IDE 使用的单独编译器。

在我介绍 C++ 类(class)时,我需要编写一个程序,从一个文件中读取 double 值,并对它们求值,将内容输出到另一个 .txt 文件

我使用 Visual Studio 2012 编写了程序,它按照我的预期读取文件并按预期执行

我相信我的老师使用 Dev C++,所以我在那个 IDE 和 Code::Blocks 中编译了相同的代码。

我注意到 get 指针的行为不同,我相信这是编译器造成的。在 Code::Blocks 和 Dev C++ 中,在读取第一个 double (6.0) 后,inFile.tellg() 返回 15。在 VS2012 中,它返回 3。

我可以对这个程序做些什么,以便它可以在所有 IDE 中运行?

要读取的文件:
每行的前 3 个数字是盒子的尺寸,接下来的 2 个是盒子的直径和 jar 的高度。

6.0 6.0 10.3 5.0 10.0
6.0 5.0 10.3 5.0 10.0
12.0 3.3 4.0 3.0 11.0
12.0 3.2 4.0 3.0 11.0
9.5 6.5 7.5 6.0 9.5
9.5 6.5 7.5 6.0 9.0
4.5 8.0 4.5 4.0 7.5
4.0 8.0 4.5 4.0 7.5
7.3 7.3 17.0 7.0 16.0
6.8 7.3 17.0 7.0 16.0
7.3 7.3 16.2 7.0 16.0
7.2 7.3 16.3 7.0 16.0

预期输出(在 VS2012 中执行):

           BOX                                 JAR
L W H D H FITS?
===========================================================
6.0 6.0 10.3 5.0 10.0 YES
6.0 5.0 10.3 5.0 10.0 NO
12.0 3.3 4.0 3.0 11.0 YES
12.0 3.2 4.0 3.0 11.0 NO
9.5 6.5 7.5 6.0 9.5 NO
9.5 6.5 7.5 6.0 9.0 YES
4.5 8.0 4.5 4.0 7.5 YES
4.0 8.0 4.5 4.0 7.5 NO
7.3 7.3 17.0 7.0 16.0 YES
6.8 7.3 17.0 7.0 16.0 NO
7.3 7.3 16.2 7.0 16.0 NO
7.2 7.3 16.3 7.0 16.0 NO
===========================================================

Code::Blocks 和 Dev C++ 的输出:

               BOX                                 JAR
L W H D H FITS?
===========================================================
6.0 6.0 10.3 5.0 10.0 YES
0.3 5.0 10.0 12.0 3.3 NO
1.0 12.0 3.2 4.0 3.0 NO
5.0 6.5 7.5 6.0 9.5 NO
5.0 7.5 6.0 9.0 4.5 NO
0.5 4.0 7.5 4.0 8.0 NO
4.0 7.5 7.3 7.3 17.0 NO
16.0 6.8 7.3 17.0 7.0 NO
0.0 7.3 7.3 16.2 7.0 NO
6.0 7.2 7.3 16.3 7.0 NO
16.0 16.0 16.0 16.6 7.2 NO
===========================================================

最后是程序:

#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
bool hasDouble(ifstream&); // prototype

int main()
{
// initialize constants
// MAX_BOX_LENGTH must be modified to evaluate boxes with one or more sides >= 100.
const double ACCEPTABLE_CLEARANCE = 0.25, MAX_BOX_LENGTH = 100;
const int WID = 9;

// initialize variables
ifstream inFile;
ofstream outFile;
bool data_In_File = true;
bool jar_Fits = false;
double large_Box_Dim = 0, small_Box_Dim = 0, jar_D = 0, jar_H = 0, focus = 0;

// Welcome User to Program
cout << "Welcome to the \"Jar Fit?\" utility.\n\n\nThis program will open the \"P4Boxes.txt\" file"
<< " located in the same\ndirectory, and read the data from within it. The program will "
<< "then\ndetermine if the jar provided will fit in the box provided, with\nthe allowed "
<< "clearance. This data will be entered into the file\n\"NCBoxesOutput.txt\" in the same "
<< "directory.\n" << endl;
system("PAUSE");

inFile.open("P4Boxes.txt"); // Open input file

// Check for errors opening file by looking for a double
if(!hasDouble(inFile))
{
cout << endl << "There was an error opening the file.\n\nThe program will now terminate."
<< endl;
system("PAUSE");
return 1;
}
outFile.open("NCBoxesOutput.txt"); // Open output file

// Make output file header
outFile << setprecision(1) << fixed << showpoint << boolalpha << setw(WID * 2) << "BOX"
<< setw((WID * 4) + 1) << "JAR\n" << setw(WID) << "L " << setw(WID) << "W " << setw(WID)
<< "H " << setw(WID * 2) << "D " << setw(WID) << "H " << setw(WID) << "FITS?" << "\n"
<< setfill('=') << setw(WID * 7) << left << " " << right << setfill(' ') << endl;

// Main program loop
while(data_In_File)
{
jar_Fits = false; // Reset variables for each iteration
large_Box_Dim = 0;
small_Box_Dim = MAX_BOX_LENGTH;
for(int i = 0; i < 3; i++) // Get box dimensions.
{
inFile >> focus;
cout << "The read number is " << focus << " and the pointer is at " << inFile.tellg() << endl;
system("PAUSE");
if(focus > large_Box_Dim)
large_Box_Dim = focus; // For jar height comparison
if(focus < small_Box_Dim)
small_Box_Dim = focus; // For jar width comparison
outFile << setw(WID) << focus;
}
inFile >> jar_D >> jar_H; // Get jar Dimensions
outFile << setw(WID * 2) << jar_D << setw(WID) << jar_H;
jar_D += ACCEPTABLE_CLEARANCE; // Account for needed clearance
jar_H += ACCEPTABLE_CLEARANCE;
if((jar_D <= small_Box_Dim) && (jar_H <= large_Box_Dim)) // Does jar fit?
outFile << setw(WID) << "YES\n" << endl;
else
outFile << setw(WID) << "NO\n" << endl;
data_In_File = hasDouble(inFile); // is there another double in file?
}
outFile << setfill('=') << setw(WID * 7) << left << " " << right << setfill(' ') << endl;
cout << "\nThe program has executed successfully.\n" << endl; // Inform user
system("PAUSE");
return 0;
}

bool hasDouble(ifstream& inFile) // This function checks the file for for a double
{
double test = 0;
int place = 0;
place = inFile.tellg(); //This records the location of the get pointer
if(inFile >> test)
{
inFile.seekg(place); // If a double is found, the point is returned to previous location.
return true;
}
else
return false;
}

对于代码转储,我深表歉意,但我一直在寻找,但找不到对此或如何修复它的可靠解释。

最佳答案

我看不出这样的代码有什么问题。

查看输入数据并将其与您获得的数字进行比较,很明显在读入一组五个数字后,读取会跳过九个字符。这显然是因为在 while 循环结束时调用了 hasDouble() - 您已经表明不能依赖 tellg 并且 hasDouble 需要它来工作。

因此,为了让代码在所有 IDE 上运行,您需要用其他检测文件末尾的方法替换循环中 hasDouble 的使用。

我真的不能说为什么 tellg 的行为不同,但在我看来它在 Code::Blocks 和 Dev C++ 中被破坏了。可能是我们期望它的表现比实际情况更好。

关于c++ - 在不同的 C++ 编译器中读取文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21818048/

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