Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何修复循环,使其不会';t在文件c+中保留条目时停止+;_C++_Loops_Linker - Fatal编程技术网

C++ 如何修复循环,使其不会';t在文件c+中保留条目时停止+;

C++ 如何修复循环,使其不会';t在文件c+中保留条目时停止+;,c++,loops,linker,C++,Loops,Linker,我意识到这是我在同一个主题上的第二篇帖子,我感谢你们所有人对我的耐心,感谢你们为使这篇文章成功所做的努力。自从我上次发布以来已经有几天了,我仍在试图弄清楚为什么循环在读取了输入文件中的15个条目后仍坚持终止 我的教授为我们提供了一个链接器,其中包含main()函数和参数中存在的两个文件,一个顺序访问输入文件和一个随机访问输出文件,因此标题中的首字母缩写为ed。我已经得到了所有其他实例的工作,但我和我的导师还没有弄清楚发生了什么,我真的需要更多的帮助,任何建议都将不胜感激 #include <

我意识到这是我在同一个主题上的第二篇帖子,我感谢你们所有人对我的耐心,感谢你们为使这篇文章成功所做的努力。自从我上次发布以来已经有几天了,我仍在试图弄清楚为什么循环在读取了输入文件中的15个条目后仍坚持终止

我的教授为我们提供了一个链接器,其中包含main()函数和参数中存在的两个文件,一个顺序访问输入文件和一个随机访问输出文件,因此标题中的首字母缩写为ed。我已经得到了所有其他实例的工作,但我和我的导师还没有弄清楚发生了什么,我真的需要更多的帮助,任何建议都将不胜感激

#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;

const int DESC_SIZE = 37;

struct Item
{
int itemId;
char description[DESC_SIZE];
double cost, price;
};

int processFile(const char* saifFile, const char* raofFile)
{
fstream outFile, inFile;
Item Inventory;
int counter = 0;
int errorCode = 0;

inFile.open(saifFile, ios::in);
outFile.open(raofFile, ios::out | ios:: binary | ios::trunc);
if (!inFile.fail())
{
    cout << " Part ID  Part Cost  Part Price   Part Description" << endl;
    cout << " =======  =========  ==========   ================" << endl;
    inFile >> Inventory.itemId;
    if (!inFile.eof())
    {
    while (!inFile.eof() && counter <= 100 && errorCode == 0)
        {
            inFile >> Inventory.cost >> Inventory.price;
            inFile.getline(Inventory.description, DESC_SIZE);
            if (Inventory.itemId != counter)
                errorCode = -4;
            if (Inventory.cost < 0)
                errorCode = -5;
            if (Inventory.price < 0)
                errorCode = -6;
            cout << "      " << Inventory.itemId << "     " << setw(5) << Inventory.cost << "       " << setw(5) << Inventory.price <<" " << Inventory.description << endl;
            counter++;
            inFile >> Inventory.itemId;
        }
        if (!inFile.eof())
            errorCode = -3;
    }
    else
        errorCode = -2;
}
else
    errorCode = -1;
inFile.close();
switch (errorCode)
{
case -1:
    cout << "ERROR: Cannot open input and/or output file.\n";
    break;
case -2:
    cout << "ERROR: Empty input file.\n";
    break;
case -3:
    cout << "ERROR: More than 100 records in the input file.\n";
    break;
case -4:
    cout << "ERROR: Item id numbers out of sequence in the input file.\n";
    break;
case -5:
    cout << "ERROR: Found record with negative cost in input file.\n";
    break;
case -6:
    cout << "ERROR: Found record with negative price in input file.\n";
    break;
}
if (errorCode != 0)
    return errorCode;
return counter;
#包括
#包括
#包括
使用名称空间std;
const int DESC_SIZE=37;
结构项
{
int itemId;
字符描述[DESC_SIZE];
双重成本、双重价格;
};
int进程文件(常量字符*saifFile,常量字符*raofFile)
{
fstream输出文件,填充;
项目清单;
int计数器=0;
int errorCode=0;
infle.open(saifFile,ios::in);
open(raofFile,ios::out | ios::binary | ios::trunc);
如果(!infle.fail())
{
cout>Inventory.price;
填充getline(Inventory.description,DESC_SIZE);
if(Inventory.itemId!=计数器)
错误代码=-4;
如果(库存成本<0)
错误代码=-5;
如果(存货价格<0)
错误代码=-6;

cout我最好的猜测是以下代码导致了问题:

inFile >> Inventory.cost >> Inventory.price;
inFile.getline(Inventory.description, DESC_SIZE);
如果在输入到
Inventory.price
之后还有一个换行符,它将中断以下
getline()
语句,因为它在到达换行符时停止。您必须使用
ignore()
忽略它:

infle>>Inventory.cost>>Inventory.price;

infle.ignore();//对于每个可能的停止条件,您都有一个错误代码,您会问它为什么停止。为什么您不检查自己的
errorCode
,或者告诉它?您也可以使用一个分步调试程序,自己查找。由于您不提供输入、预期输出和实际输出,没有人能帮助您。您需要具体说明。如何确定当它读取15个条目时是否终止?@0x499602D2可能通过
返回计数器
,但这是我的疯狂水晶球告诉我的。你真是个天才!太感谢你了!我真不敢相信我没有早点看到这一点。@0x499602D2Wow。我嫉妒你的干运行调试技能=)。我在考虑外部代码中的一个bug或输入文件。但文本IO不是我的事。顺便说一句。为什么这个社区会有答案?@luk32谢谢。我只是想做一个。
:)
inFile >> Inventory.cost >> Inventory.price;
inFile.ignore(); // <==
inFile.getline(Inventory.description, DESC_SIZE);