当我将一个.txt文件加载到我的程序中时,如何删除这些随机整数(我根本没有输入) 我目前正在做C++中的一个任务,我必须把一组数据从 .txt < /Cux>文件中链接到:

当我将一个.txt文件加载到我的程序中时,如何删除这些随机整数(我根本没有输入) 我目前正在做C++中的一个任务,我必须把一组数据从 .txt < /Cux>文件中链接到:,c++,string,file,linked-list,C++,String,File,Linked List,这是我的加载代码: void record::load( string fileName ) throw( recordException ) { ifstream inFile( fileName.c_str() ); string c; string t; double p; int q; node *tail; while (!isEmpty()) { remove( 1 ); } size

这是我的加载代码:

void record::load( string fileName ) throw( recordException )
{
    ifstream inFile( fileName.c_str() );
    string c;
    string t;
    double p;
    int q;
    node *tail;

    while (!isEmpty())
    {
        remove( 1 );
    }
    size = 0;

    if ( getline(inFile, c) ||getline(inFile,t)||inFile >> p>> q) // Is file empty?
    {  // File not empty:
        try
        {
            head = new node;
            // Add the first integer to the list.
            head->cat = c;
            head->title = t;
            head->price = p;
            head->qty = q;
            head->next = NULL;
            tail = head;
            size = size + 1;
            inFile.skipws;

            // Add remaining items to linked list.
            while ( getline(inFile, c)||getline(inFile ,t)||inFile >> p>> q)
            // while(inFile.getline(c,50)||inFile.getline(t,50)||inFile>>p>>q)
            {
                tail->next = new node;
                tail = tail->next;
                tail->cat = c;
                tail->title = t;
                tail->price = p;
                tail->qty = q;
                tail->next = NULL;
                size = size + 1;
                inFile.skipws;
            }  // end while
        }  // end try
        catch (bad_alloc e)
        {
            throw recordException(
                "recordException: restore cannot allocate memory.");
        }  // end catch
    }  // end if

    inFile.close();
}
它工作得很好,除了一个问题:当我试图显示加载到链接列表中的数据时,我得到了非常奇怪的数字

这是我的程序运行时的样子:

有人能帮我吗?我能做些什么来删除这些号码?为什么会出现这些数字


我使用C++ CODBROCK 16.01,如果有任何帮助。

下次,不要使用图像,而是将实际文本复制/粘贴到你的帖子中。 您的读取代码与输入数据的格式不匹配。当您调用

getline()
时,它将一直读取,直到遇到换行符或EOF

因此,对
getline(infle,c)
的第一次调用一次读取整个第一行:

"     DVD     Snow White          20     20"
"     VCD     Cinderella          10     10"
然后
getline(infle,t)
一次读取下一行:

"     DVD     Snow White          20     20"
"     VCD     Cinderella          10     10"
然后
infle>>p>>q
尝试解析下一行,但失败:

"     MT      Wonderworld         20     30"
因为
MT
不是
double
Wonderworld
不是
int

您需要逐行读取文件,分别解析每一行,例如:

#include <string>
#include <sstream>

void record::load( std::string fileName ) throw( recordException )
{
    while (!isEmpty())
        remove( 1 );
    size = 0;

    std::ifstream inFile( fileName.c_str() );
    if (!inFile.is_open())
        return;

    std::string line;
    std::string c;
    char t[25+1] = {0}; // <-- use whatever your max title length actually is...
    double p;
    int q;
    node *newNode;

    while (std::getline(inFile, line)) // Is file empty?
    {
        std::istringstream iss(line);

        if ((iss >> c) && iss.get(t, sizeof(t)) && (iss >> p >> qty))
        {
            try
            {
                newNode = new node;
                newNode->cat = c;
                newNode->title = t;
                newNode->price = p;
                newNode->qty = q;
                newNode->next = NULL;

                if (!head)
                    head = newNode;

                if (tail)
                    tail->next = newNode;
                tail = newNode;
                ++size;
            }
            catch (const bad_alloc &e)
            {
                throw recordException(
                    "recordException: restore cannot allocate memory.");
            }
        }
    }
}
#包括
#包括
无效记录::加载(标准::字符串文件名)抛出(记录异常)
{
而(!isEmpty())
移除(1);
尺寸=0;
std::ifstream infle(fileName.c_str());
如果(!infle.is_open())
返回;
std::字符串行;
std::字符串c;
字符t[25+1]={0};//>c)和&iss.get(t,sizeof(t))&(iss>>p>>qty))
{
尝试
{
newNode=新节点;
newNode->cat=c;
newNode->title=t;
新建节点->价格=p;
新建节点->数量=q;
newNode->next=NULL;
如果(!头)
头=新节点;
如果(尾部)
tail->next=newNode;
tail=newNode;
++大小;
}
渔获物(康斯特巴德)
{
抛出记录异常(
“recordException:还原无法分配内存。”);
}
}
}
}

正则表达式更适合这样的任务。您可以使用以下内容:

std::string test = "      DVD      Snow White       20    20";
std::regex re("\\s*(\\S+)\\s+(\\S.*\\S)\\s+(\\d+)\\s+(\\d+)");
std::smatch match;
if (std::regex_match(test, match, re)) {
        for (size_t i = 0; i < match.size(); ++i) {
            std::cout << "submatch " << i << ": \"" << match[i] << "\"\n";
        }   
 } 

注意:我根据您的输入重新制作了图案-价格只包含数字,没有点。如果不是这样,您需要相应地修改RE。

如果(getline(infle,c)| | getline(infle,t)| | infle>>p>>q)
这不应该是
&
而不是
|
吗?使用类似于
gdb
的调试器(并使用所有警告和调试信息进行编译,例如
g++-Wall-Wextra-g
)文件实际上是什么样子的?不管怎样,
getline(infle,c)| getline(infle,t)| infle>>p>>q
肯定不是读取文件的正确方式。显示数据的代码是什么样子的?我建议为您的节点类重载
operator>
。节点应该能够从流中加载其成员,因为节点拥有知识和直接访问权。访问节点成员违反了封装规则。您不能以这种方式拆分这些行,您需要逐行读取它们,然后使用RE或手动编写的逻辑进行拆分。