EDIT: изначально я думал, что это связано с тем, что я тестировал программу IDE. Проблема, судя по всему, сводится к отдельным компиляторам, которые использует каждая среда IDE.Чтение файлов в разных компиляторах C++
Для присвоения в моем введении к классу C++, мне нужно написать программу, которая считывает двойники из файла, и оценивает их, выводит содержимое в другой текстовый файл
Я написал программу с помощью Visual Studio 2012, и он читает файл, как и следовало ожидать, и исполняется, как ожидалось.
Я считаю, что мой учитель использует Dev C++, поэтому я скомпилировал тот же код в этой среде IDE, а также Code :: Blocks.
Я заметил, что указатель get ведет себя по-другому, и я считаю, что это связано с компилятором. В Code :: Blocks и Dev C++ после чтения первого двойника (6.0) inFile.tellg() возвращает 15. В VS2012 он возвращает 3.
Что я могу сделать с этой программой, чтобы она работала во всех IDE?
Файл для чтения:
Первые 3 номера каждой строки являются размерами коробки, а следующие 2 - диаметром и высотой банки, чтобы они вставлялись в коробку.
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;
}
Приносим извинения за свалку кода, но я h ave выглядел и не может найти убедительного объяснения этому или для того, как его исправить.
IDE не должен иметь никакого значения. Я не читаю весь этот код, но возможно ли это проблема с окончанием строки между платформами/IDE? – John3136
Честно говоря, не уверен. По какой-либо причине в Code :: Blocks и Dev C++ inFile.tellg() возвращается 15 после чтения только одного двойника. в VS возвращается 3. : -/ – Carson2006
Конкретный компилятор, который они используют, может иметь значение, но не сам IDE. – OJFord