简体   繁体   中英

C++ file i/o stream issue

First of all I DO NOT WANT CODES I want to write them myself but I am current out of idea for this part so any help would be appreciated BUT I DO NOT WANT written ANY CODE

Here is the problem that I can't seem to solve.

okay I'm re-edit this to give you the questions.

Assuming we read the input from the ifstream (text file) and the system end of file condition signals end of input

  • the first line is last name up to 20 char
  • the second line is the person firs name up to 10 char
  • the third line is the two character postal service abbreviation up to 2 char
  • the forth line is the company up to 40 char

the part I don't get is part of the error handing ...

  • fewer or greater four input lines per attendee would be rejected

how do we know when to stop for the greater or less than 4 input per- person?

The post is not completely clear, but with the given information this is what I came up with on the fly:

First make a container (array, vector, list, map, whatever) to hold the names of the States. You will need this to check if a series of entries is valid.

Now, you said entries (each individual record [ie name, name, state, company]) is separated by newlines. Using this information I would read in lines until you hit a blank line and store them into a container for temporary holding.

When you reach the new line (signaling a new record), check the temporary container. If it contains 4 strings (lines) and are in the order of: non-state, non-state, state, non-state; then consider it a valid record and store it in a permanent container and clear the temp container.

Continue this until you reach the end of the file.

Hope this helps and makes sense as you asked explicitly for no code.

Keep a bitmap of states:

bool state_bitmap[NUM_STATES];

Initialize it with all the states:

state_bitmap[hash_code ("state1")] = true;
state_bitmap[hash_code ("state2")] = true;
// ...

For each string read at a state position, make sure it is in the bitmap:

std::string state = read_state_from_file ();
if (!state_bitmap[hash_code (state)]
{
    // Not a state!
}

If the problem is about duplicate names, you can follow a similar approach. Read each string and map it to a bool , if it is not already there in the map:

std::map<std::string, bool> names;

std::string name = read_next_name_from_file ();
if (!names[name])
{
    names[name] = true;
}
else
{
    // name already in map, do something about that.
} 

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