Hello DigiLei,
Before you can read a file you first have to know what the file looks like, how it is laid out. Does it look something like your output or is it different?
When I first was learning to deal with input and output files I found this code useful:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
std::string iFileName{ "" }; // <--- Put file name here.
std::ifstream inFile;
inFile.open(iFileName);
if (inFile.is_open())
{
std::cout << "\n File " << iFileName << " is open" << std::endl;
std::this_thread::sleep_for(std::chrono::seconds(2)); // <--- Needs header files chrono" and "thread".
}
else
{
std::cout << "\n File " << iFileName << " did not open" << std::endl;
std::this_thread::sleep_for(std::chrono::seconds(3)); // <--- Needs header files chrono" and "thread".
exit(1); // <--- Because there is no need to continue.
}
|
In the then part of the if statement you can comment out these lines when everything is working and you no longer need them.
When you have gained a better understanding of how everything works this code can be shortened. When that can happen you have learned something.
As dhayden has said without seeing the input file it is hard to advise on how to read it.
Based on the output you have shown I can see the need for five variables, If the input is set up right you may only need one variable for first and last name otherwise two variables will be needed. ick1 has a good example of what you can do except for "full_name" will only pick up the first name and leave the last name for the next variable.
Without more to work with all I can say is that when you open an input file you need to check to make sure it is open. This checking for an open file and good file stream is not as necessary with output files. If the file does not exist it will be created thus it is hard to have an output file stream fail on open, but this is the code I use for output files:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
std::string oFileName{ "" }; // <--- Put file name here.
std::ofstream outFile;
outFile.open(oFileName, std::ios::trunc | std::ios::ate);
if (outFile.is_open())
{
std::cout << "\n File " << oFileName << " is open" << std::endl;
std::this_thread::sleep_for(std::chrono::seconds(2)); // <--- Needs header files chrono" and "thread".
}
else
{
std::cout << "\n File " << oFileName << " did not open" << std::endl;
std::this_thread::sleep_for(std::chrono::seconds(3)); // <--- Needs header files chrono" and "thread".
exit(1); // <--- No need to go any farther, but not likely to get here.
}
|
The biggest difference here is line 5. I use "trunc" to clear the file before writing new information to it. This is useful when first writing and debugging the code so yo only see the most recent output. The "ate" means to append the file and is what works with "trunc". When you are finished with the program you should remove the "std::ios::trunc |" part and change "ate" to "app" so it will always open the file for append.
Post an example of or the whole input file, if small, and some code of what you have tried and we can go from there.
Hope that helps,
Andy