简体   繁体   中英

How do i remove these random integers(which is I did not input at all) when I load a .txt file into my program

I am currently doing an assignment in C++ in which I have to load a bunch of data from a .txt file into a linked list:

图片

Here is my load code:

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();
}

It works great, except for one issue: I get really weird numbers when I try to display the data that is loaded in my linked list.

This is what it looks like when my program runs:

图片

Can anyone help me? What can I do to remove those numbers? Why do these numbers occur anyway?

I am using C++ Codeblocks 16.01, if that is of any help.

Next time, don't use images, always copy/paste the actual text into your post instead.

Your reading code does not match the format of your input data. When you call getline() , it reads until it encounters a line break or EOF.

So, the first call to getline(inFile, c) reads the ENTIRE first line in one go:

"     DVD     Snow White          20     20"

Then getline(inFile,t) reads the NEXT line in one go:

"     VCD     Cinderella          10     10"

Then inFile >> p >> q tries to parse the NEXT line and fails:

"     MT      Wonderworld         20     30"

Since MT is not a double and Wonderworld is not an int .

You need to read the file line-by-line, parsing each line individually, for example:

#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.");
            }
        }
    }
}

Regular Expression is much more suitable for task like this. You can use something like:

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";
        }   
 } 

live example

So you could read file line by line and split it using that RE:

std::regex re("\\s*(\\S+)\\s+(\\S.*\\S)\\s+(\\d+)\\s+(\\d+)");
std::string line;
while( std::getline( inFile, line ) ) {
    std::smatch match;
    if (!std::regex_match(test, match, re)) continue;
    std::string cat = match[1].str();
    std::string title = match[2].str();
    double price = std::stod( match[3].str() );
    int qty = std::stoi( match[4].str() );
    // you insert your node into list here 
}

Note: I made RE pattern based on your input - price contains only digits, no dot. If it is not the case you would need to modify RE accordingly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM