C ++逐行读取文件,有些行的列与其他行不同
C++ read file line by line and some lines have different columns than the others
我的 text.txt 看起来像这样:
1 52 Hayden Smith 18:16 15 M Berlin
2 54 Mark Puleo 18:25 15 M Berlin
3 97 Peter Warrington 18:26 29 M New haven
4 305 Matt Kasprzak 18:53 33 M Falls Church
5 272 Kevin Solar 19:17 16 M Sterling
6 394 Daniel Sullivan 19:35 26 M Sterling
7 42 Kevan DuPont 19:58 18 M Boylston
8 306 Chris Goethert 20:00 43 M Falls Church
问题是,对于第三行,镇名是纽黑文,纽黑文和黑文之间有一个space。并且对于第四行,在 Falls 和 Church 之间有一个 space。当我逐行读取文件时,这会产生问题。
因此,我的代码如下所示:
ifstream infile("text.txt", ios::in);
if(!infile)
{
cerr<<"File could not be opend"<<endl;
}
SortedLinked mylist;
int a;
int b;
string c;
string d; // I have eight different variables, representing
string e; // each column, but some lines have nine columns.
int f;
char g;
string h;
string mystr;
int mymin;
int mysec;
while(infile>>a>>b>>c>>d>>e>>f>>g>>h)
{
// This does not work, because as you get to line three, h is New, there
// is no variable to store Haven. Therefore my code can only get to line
// 3.
mystr = c+d;
mymin = convertString<int>(e.substr(0,2));
mysec = convertString<int>(e.substr(3, 4));
Runner M(mystr, f, mymin, mysec);
//M.print();
mylist.additem(M);
}
此外,如果您尝试这样做:
replace(h.begin(), h.end(), ' ', '_')
尝试在New和haven之间加一个下划线,不行。
因为这试图用另一个变量替换一个变量,这里的 h 是 New,它不是 New haven,你无法到达 haven,因为 haven 是另一个变量。所以你无法用下划线替换 space。
此外,如果您尝试这样做:
while(infile>>a>>b>>c>>d>>e>>f>>g>>h>>i)
这也不行,因为当你到达第 1 行时,只有右边的列,没有任何东西可以存储到变量 i 中。像这样的代码只会让你到第 1 行。
如果您有其他方法可以做到这一点,例如跳过城镇名称列、第 8 列和第 9 列(对于某些行),因为我什至不需要城镇名称。
或者,您可以去掉 New haven 之间的 space 和 Fall Church 之间的 space。任何事情都会受到赞赏。
您可以使用 getline
阅读该行的其余部分。
while( (infile>>a>>b>>c>>d>>e>>f>>g) && getline(infile, h))
您应该考虑使用 getline。这样你就可以逐行读取然后解析输入。
一旦你将这一行读入一个字符串,你就可以像往常一样解析它,用 space 拆分每个子字符串,直到你得到城镇的名称,你将在其中拆分从那里到行尾的子字符串到您的城镇名称变量中。
您可以使用一个小技巧:由于您希望忽略的空格仅出现在最后一个条目中,因此您可以使用 std::getline
而不是 >>
来读取城镇名称。
string str;
while (getline(infile, str)) { // Read the entire line
stringstream ss(str);
ss>>a>>b>>c>>d>>e>>f>>g;
getline(ss, h);
... // The rest of your code
}
我已经有了一个现有的实用程序 class 来处理字符串,并且在处理您的文件结构一段时间后,这就是我能够想出的。但是,我确实必须修改您现有的文本文件才能使其正常工作。您的所有列都必须有一个 space(这可以更改为一个选项卡),对于最后一个条目,其中一些城镇名称有两个单词,我用双引号将它们括起来。所以现在你的 text.txt
文件看起来像这样:
text.txt
1 52 Hayden Smith 18:16 15 M Berlin
2 54 Mark Puleo 18:25 15 M Berlin
3 97 Peter Warrington 18:26 29 M "New Haven"
4 305 Matt Kasprzak 18:53 33 M "Falls Church"
5 272 Kevin Solar 19:17 16 M Sterling
6 394 Daniel Sullivan 19:35 26 M Sterling
7 42 Kevan DuPont 19:58 18 M Boylston
8 306 Chris Goethert 20:00 43 M "Falls Church"
这是工作程序。
stdafx.h
#ifndef STDAFX_H
#define STDAFX_H
#include <stdio.h>
#include <tchar.h>
#include <conio.h>
#include <string>
#include <sstream>
#include <fstream>
#include <iostream>
#include <iomanip>
#include <vector>
#include <algorithm>
enum ReturnCode {
RETURN_OK = 0,
RETURN_ERROR = 1,
}; // ReturnCode
#endif // STDAFX_H
stdafx.cpp
#include "stdafx.h"
Utility.h
#ifndef UTILITY_H
#define UTILITY_H
class Utility {
public:
static void pressAnyKeyToQuit();
static std::string toUpper(const std::string& str);
static std::string toLower(const std::string& str);
static std::string trim(const std::string& str, const std::string elementsToTrim = " \t\n\r");
static unsigned convertToUnsigned(const std::string& str);
static int convertToInt(const std::string& str);
static float convertToFloat(const std::string& str);
static std::vector<std::string> splitString(const std::string& strStringToSplit, const std::string& strDelimiter, const bool keepEmpty = true);
private:
Utility(); // Private - Not A Class Object
Utility(const Utility& c); // Not Implemented
Utility& operator=(const Utility& c); // Not Implemented
template<typename T>
static bool stringToValue(const std::string& str, T* pValue, unsigned uNumValues);
template<typename T>
static T getValue(const std::string& str, std::size_t& remainder);
}; // Utility
#include "Utility.inl"
#endif // UTILITY_H
Utility.inl
// ----------------------------------------------------------------------------
// stringToValue()
template<typename T>
static bool Utility::stringToValue(const std::string& str, T* pValue, unsigned uNumValues) {
int numCommas = std::count(str.begin(), str.end(), ',');
if (numCommas != uNumValues - 1) {
return false;
}
std::size_t remainder;
pValue[0] = getValue<T>(str, remainder);
if (uNumValues == 1) {
if (str.size() != remainder) {
return false;
}
}
else {
std::size_t offset = remainder;
if (str.at(offset) != ',') {
return false;
}
unsigned uLastIdx = uNumValues - 1;
for (unsigned u = 1; u < uNumValues; ++u) {
pValue[u] = getValue<T>(str.substr(++offset), remainder);
offset += remainder;
if ((u < uLastIdx && str.at(offset) != ',') ||
(u == uLastIdx && offset != str.size()))
{
return false;
}
}
}
return true;
} // stringToValue
Utility.cpp
#include "stdafx.h"
#include "Utility.h"
// ----------------------------------------------------------------------------
// pressAnyKeyToQuit()
void Utility::pressAnyKeyToQuit() {
std::cout << "Press any key to quit" << std::endl;
_getch();
} // pressAnyKeyToQuit
// ----------------------------------------------------------------------------
// toUpper()
std::string Utility::toUpper(const std::string& str) {
std::string result = str;
std::transform(str.begin(), str.end(), result.begin(), ::toupper);
return result;
} // toUpper
// ----------------------------------------------------------------------------
// toLower()
std::string Utility::toLower(const std::string& str) {
std::string result = str;
std::transform(str.begin(), str.end(), result.begin(), ::tolower);
return result;
} // toLower
// ----------------------------------------------------------------------------
// trim()
// Removes Elements To Trim From Left And Right Side Of The str
std::string Utility::trim(const std::string& str, const std::string elementsToTrim) {
std::basic_string<char>::size_type firstIndex = str.find_first_not_of(elementsToTrim);
if (firstIndex == std::string::npos) {
return std::string(); // Nothing Left
}
std::basic_string<char>::size_type lastIndex = str.find_last_not_of(elementsToTrim);
return str.substr(firstIndex, lastIndex - firstIndex + 1);
} // trim
// ----------------------------------------------------------------------------
// getValue()
template<>
float Utility::getValue(const std::string& str, std::size_t& remainder) {
return std::stof(str, &remainder);
} // getValue <float>
// ----------------------------------------------------------------------------
// getValue()
template<>
int Utility::getValue(const std::string& str, std::size_t& remainder) {
return std::stoi(str, &remainder);
} // getValue <int>
// ----------------------------------------------------------------------------
// getValue()
template<>
unsigned Utility::getValue(const std::string& str, std::size_t& remainder) {
return std::stoul(str, &remainder);
} // getValue <unsigned>
// ----------------------------------------------------------------------------
// convertToUnsigned()
unsigned Utility::convertToUnsigned(const std::string& str) {
unsigned u = 0;
if (!stringToValue(str, &u, 1)) {
std::ostringstream strStream;
strStream << __FUNCTION__ << " Bad conversion of [" << str << "] to unsigned";
throw strStream.str();
}
return u;
} // convertToUnsigned
// ----------------------------------------------------------------------------
// convertToInt()
int Utility::convertToInt(const std::string& str) {
int i = 0;
if (!stringToValue(str, &i, 1)) {
std::ostringstream strStream;
strStream << __FUNCTION__ << " Bad conversion of [" << str << "] to int";
throw strStream.str();
}
return i;
} // convertToInt
// ----------------------------------------------------------------------------
// convertToFloat()
float Utility::convertToFloat(const std::string& str) {
float f = 0;
if (!stringToValue(str, &f, 1)) {
std::ostringstream strStream;
strStream << __FUNCTION__ << " Bad conversion of [" << str << "] to float";
throw strStream.str();
}
return f;
} // convertToFloat
// ----------------------------------------------------------------------------
// splitString()
std::vector<std::string> Utility::splitString(const std::string& strStringToSplit, const std::string& strDelimiter, const bool keepEmpty) {
std::vector<std::string> vResult;
if (strDelimiter.empty()) {
vResult.push_back(strStringToSplit);
return vResult;
}
std::string::const_iterator itSubStrStart = strStringToSplit.begin(), itSubStrEnd;
while (true) {
itSubStrEnd = search(itSubStrStart, strStringToSplit.end(), strDelimiter.begin(), strDelimiter.end());
std::string strTemp(itSubStrStart, itSubStrEnd);
if (keepEmpty || !strTemp.empty()) {
vResult.push_back(strTemp);
}
if (itSubStrEnd == strStringToSplit.end()) {
break;
}
itSubStrStart = itSubStrEnd + strDelimiter.size();
}
return vResult;
} // splitString
main.cpp
#include "stdafx.h"
#include "Utility.h"
struct Data {
int a;
int b;
std::string c;
std::string d;
std::string e;
int f;
char g;
std::string h;
};
int main() {
std::string strFilename( "text.txt" );
std::ifstream file;
std::string strLine;
std::vector<std::string> vTemp;
std::vector<std::string> vResult;
std::vector<Data> vData;
// Open File For Reading
file.open( strFilename.c_str() );
// Check For Error Of Opening File
if ( !file.is_open() ) {
std::cout << "Error opening file (" << strFilename << ")" << std::endl;
return RETURN_ERROR;
}
// Continue Until End Of File
while( !file.eof() ) {
// Get Single Full Line Save To String
std::getline( file, strLine );
// Split String Using A Double Quote Delimiter
vTemp = Utility::splitString( strLine, "\"" );
// Check To See If vTemp Has More Than One String
if ( vTemp.size() > 1 ) {
// Store The Last Value Of vResult -
// We Need To Use Pop Back To Account For Last Double Quote
vTemp.pop_back(); // Remove Last Double Quote
std::string temp = vTemp.back();
vTemp.pop_back(); // Remove Wanted String From vTemp.
// At This Point We Need To Parse vTemp Again Using Space Delimiter
vResult = Utility::splitString( vTemp[0], " " );
// Need To Account For Last Space In Vector
vResult.pop_back();
// Now We Can Push Our Last String Back Into vResult
vResult.push_back( temp );
} else if ( vTemp.size() == 1 ) {
// Just Parse vTemp Using Space Delimiter
vResult = Utility::splitString( vTemp[0], " " );
}
// Print Out Results For Validity
for ( unsigned u = 0; u < vResult.size(); u++) {
std::cout << vResult.at(u) << " ";
}
std::cout << std::endl;
// Here Is Where You Would Populate Your Variables, Structures Or Classes On Each Pass Of The While Loop.
// With This Structure There Should Only Be 8 Entries Into Our vResult
Data temp;
temp.a = Utility::convertToInt( vResult[0] );
temp.b = Utility::convertToInt( vResult[1] );
temp.c = vResult[2];
temp.d = vResult[3];
temp.e = vResult[4];
temp.f = Utility::convertToInt( vResult[5] );
temp.g = vResult[6].at( 0 ); // Should Only Be One Character In This String
temp.h = vResult[7];
vData.push_back( temp );
}
std::cout << std::endl << std::endl;
// Print Using Structure For Validity
std::cout << "---------------------------------------\n";
for ( unsigned u = 0; u < vData.size(); u++ ) {
std::cout << vData[u].a << " "
<< vData[u].b << " "
<< vData[u].c << " "
<< vData[u].d << " "
<< vData[u].e << " "
<< vData[u].f << " "
<< vData[u].g << " "
<< vData[u].h << std::endl;
}
// Close File
file.close();
Utility::pressAnyKeyToQuit();
return RETURN_OK;
} // main
据我所知,这是一个完整的工作程序,已使用 MSVS 2015 Community Edition 在英特尔四核 3.0Ghz 运行 Windows 7 64 位和 8GB Ram 上成功编译和构建.没有编译或构建错误,预期数据正确打印到控制台 window。
我的 text.txt 看起来像这样:
1 52 Hayden Smith 18:16 15 M Berlin
2 54 Mark Puleo 18:25 15 M Berlin
3 97 Peter Warrington 18:26 29 M New haven
4 305 Matt Kasprzak 18:53 33 M Falls Church
5 272 Kevin Solar 19:17 16 M Sterling
6 394 Daniel Sullivan 19:35 26 M Sterling
7 42 Kevan DuPont 19:58 18 M Boylston
8 306 Chris Goethert 20:00 43 M Falls Church
问题是,对于第三行,镇名是纽黑文,纽黑文和黑文之间有一个space。并且对于第四行,在 Falls 和 Church 之间有一个 space。当我逐行读取文件时,这会产生问题。
因此,我的代码如下所示:
ifstream infile("text.txt", ios::in);
if(!infile)
{
cerr<<"File could not be opend"<<endl;
}
SortedLinked mylist;
int a;
int b;
string c;
string d; // I have eight different variables, representing
string e; // each column, but some lines have nine columns.
int f;
char g;
string h;
string mystr;
int mymin;
int mysec;
while(infile>>a>>b>>c>>d>>e>>f>>g>>h)
{
// This does not work, because as you get to line three, h is New, there
// is no variable to store Haven. Therefore my code can only get to line
// 3.
mystr = c+d;
mymin = convertString<int>(e.substr(0,2));
mysec = convertString<int>(e.substr(3, 4));
Runner M(mystr, f, mymin, mysec);
//M.print();
mylist.additem(M);
}
此外,如果您尝试这样做:
replace(h.begin(), h.end(), ' ', '_')
尝试在New和haven之间加一个下划线,不行。 因为这试图用另一个变量替换一个变量,这里的 h 是 New,它不是 New haven,你无法到达 haven,因为 haven 是另一个变量。所以你无法用下划线替换 space。
此外,如果您尝试这样做:
while(infile>>a>>b>>c>>d>>e>>f>>g>>h>>i)
这也不行,因为当你到达第 1 行时,只有右边的列,没有任何东西可以存储到变量 i 中。像这样的代码只会让你到第 1 行。
如果您有其他方法可以做到这一点,例如跳过城镇名称列、第 8 列和第 9 列(对于某些行),因为我什至不需要城镇名称。
或者,您可以去掉 New haven 之间的 space 和 Fall Church 之间的 space。任何事情都会受到赞赏。
您可以使用 getline
阅读该行的其余部分。
while( (infile>>a>>b>>c>>d>>e>>f>>g) && getline(infile, h))
您应该考虑使用 getline。这样你就可以逐行读取然后解析输入。
一旦你将这一行读入一个字符串,你就可以像往常一样解析它,用 space 拆分每个子字符串,直到你得到城镇的名称,你将在其中拆分从那里到行尾的子字符串到您的城镇名称变量中。
您可以使用一个小技巧:由于您希望忽略的空格仅出现在最后一个条目中,因此您可以使用 std::getline
而不是 >>
来读取城镇名称。
string str;
while (getline(infile, str)) { // Read the entire line
stringstream ss(str);
ss>>a>>b>>c>>d>>e>>f>>g;
getline(ss, h);
... // The rest of your code
}
我已经有了一个现有的实用程序 class 来处理字符串,并且在处理您的文件结构一段时间后,这就是我能够想出的。但是,我确实必须修改您现有的文本文件才能使其正常工作。您的所有列都必须有一个 space(这可以更改为一个选项卡),对于最后一个条目,其中一些城镇名称有两个单词,我用双引号将它们括起来。所以现在你的 text.txt
文件看起来像这样:
text.txt
1 52 Hayden Smith 18:16 15 M Berlin
2 54 Mark Puleo 18:25 15 M Berlin
3 97 Peter Warrington 18:26 29 M "New Haven"
4 305 Matt Kasprzak 18:53 33 M "Falls Church"
5 272 Kevin Solar 19:17 16 M Sterling
6 394 Daniel Sullivan 19:35 26 M Sterling
7 42 Kevan DuPont 19:58 18 M Boylston
8 306 Chris Goethert 20:00 43 M "Falls Church"
这是工作程序。
stdafx.h
#ifndef STDAFX_H
#define STDAFX_H
#include <stdio.h>
#include <tchar.h>
#include <conio.h>
#include <string>
#include <sstream>
#include <fstream>
#include <iostream>
#include <iomanip>
#include <vector>
#include <algorithm>
enum ReturnCode {
RETURN_OK = 0,
RETURN_ERROR = 1,
}; // ReturnCode
#endif // STDAFX_H
stdafx.cpp
#include "stdafx.h"
Utility.h
#ifndef UTILITY_H
#define UTILITY_H
class Utility {
public:
static void pressAnyKeyToQuit();
static std::string toUpper(const std::string& str);
static std::string toLower(const std::string& str);
static std::string trim(const std::string& str, const std::string elementsToTrim = " \t\n\r");
static unsigned convertToUnsigned(const std::string& str);
static int convertToInt(const std::string& str);
static float convertToFloat(const std::string& str);
static std::vector<std::string> splitString(const std::string& strStringToSplit, const std::string& strDelimiter, const bool keepEmpty = true);
private:
Utility(); // Private - Not A Class Object
Utility(const Utility& c); // Not Implemented
Utility& operator=(const Utility& c); // Not Implemented
template<typename T>
static bool stringToValue(const std::string& str, T* pValue, unsigned uNumValues);
template<typename T>
static T getValue(const std::string& str, std::size_t& remainder);
}; // Utility
#include "Utility.inl"
#endif // UTILITY_H
Utility.inl
// ----------------------------------------------------------------------------
// stringToValue()
template<typename T>
static bool Utility::stringToValue(const std::string& str, T* pValue, unsigned uNumValues) {
int numCommas = std::count(str.begin(), str.end(), ',');
if (numCommas != uNumValues - 1) {
return false;
}
std::size_t remainder;
pValue[0] = getValue<T>(str, remainder);
if (uNumValues == 1) {
if (str.size() != remainder) {
return false;
}
}
else {
std::size_t offset = remainder;
if (str.at(offset) != ',') {
return false;
}
unsigned uLastIdx = uNumValues - 1;
for (unsigned u = 1; u < uNumValues; ++u) {
pValue[u] = getValue<T>(str.substr(++offset), remainder);
offset += remainder;
if ((u < uLastIdx && str.at(offset) != ',') ||
(u == uLastIdx && offset != str.size()))
{
return false;
}
}
}
return true;
} // stringToValue
Utility.cpp
#include "stdafx.h"
#include "Utility.h"
// ----------------------------------------------------------------------------
// pressAnyKeyToQuit()
void Utility::pressAnyKeyToQuit() {
std::cout << "Press any key to quit" << std::endl;
_getch();
} // pressAnyKeyToQuit
// ----------------------------------------------------------------------------
// toUpper()
std::string Utility::toUpper(const std::string& str) {
std::string result = str;
std::transform(str.begin(), str.end(), result.begin(), ::toupper);
return result;
} // toUpper
// ----------------------------------------------------------------------------
// toLower()
std::string Utility::toLower(const std::string& str) {
std::string result = str;
std::transform(str.begin(), str.end(), result.begin(), ::tolower);
return result;
} // toLower
// ----------------------------------------------------------------------------
// trim()
// Removes Elements To Trim From Left And Right Side Of The str
std::string Utility::trim(const std::string& str, const std::string elementsToTrim) {
std::basic_string<char>::size_type firstIndex = str.find_first_not_of(elementsToTrim);
if (firstIndex == std::string::npos) {
return std::string(); // Nothing Left
}
std::basic_string<char>::size_type lastIndex = str.find_last_not_of(elementsToTrim);
return str.substr(firstIndex, lastIndex - firstIndex + 1);
} // trim
// ----------------------------------------------------------------------------
// getValue()
template<>
float Utility::getValue(const std::string& str, std::size_t& remainder) {
return std::stof(str, &remainder);
} // getValue <float>
// ----------------------------------------------------------------------------
// getValue()
template<>
int Utility::getValue(const std::string& str, std::size_t& remainder) {
return std::stoi(str, &remainder);
} // getValue <int>
// ----------------------------------------------------------------------------
// getValue()
template<>
unsigned Utility::getValue(const std::string& str, std::size_t& remainder) {
return std::stoul(str, &remainder);
} // getValue <unsigned>
// ----------------------------------------------------------------------------
// convertToUnsigned()
unsigned Utility::convertToUnsigned(const std::string& str) {
unsigned u = 0;
if (!stringToValue(str, &u, 1)) {
std::ostringstream strStream;
strStream << __FUNCTION__ << " Bad conversion of [" << str << "] to unsigned";
throw strStream.str();
}
return u;
} // convertToUnsigned
// ----------------------------------------------------------------------------
// convertToInt()
int Utility::convertToInt(const std::string& str) {
int i = 0;
if (!stringToValue(str, &i, 1)) {
std::ostringstream strStream;
strStream << __FUNCTION__ << " Bad conversion of [" << str << "] to int";
throw strStream.str();
}
return i;
} // convertToInt
// ----------------------------------------------------------------------------
// convertToFloat()
float Utility::convertToFloat(const std::string& str) {
float f = 0;
if (!stringToValue(str, &f, 1)) {
std::ostringstream strStream;
strStream << __FUNCTION__ << " Bad conversion of [" << str << "] to float";
throw strStream.str();
}
return f;
} // convertToFloat
// ----------------------------------------------------------------------------
// splitString()
std::vector<std::string> Utility::splitString(const std::string& strStringToSplit, const std::string& strDelimiter, const bool keepEmpty) {
std::vector<std::string> vResult;
if (strDelimiter.empty()) {
vResult.push_back(strStringToSplit);
return vResult;
}
std::string::const_iterator itSubStrStart = strStringToSplit.begin(), itSubStrEnd;
while (true) {
itSubStrEnd = search(itSubStrStart, strStringToSplit.end(), strDelimiter.begin(), strDelimiter.end());
std::string strTemp(itSubStrStart, itSubStrEnd);
if (keepEmpty || !strTemp.empty()) {
vResult.push_back(strTemp);
}
if (itSubStrEnd == strStringToSplit.end()) {
break;
}
itSubStrStart = itSubStrEnd + strDelimiter.size();
}
return vResult;
} // splitString
main.cpp
#include "stdafx.h"
#include "Utility.h"
struct Data {
int a;
int b;
std::string c;
std::string d;
std::string e;
int f;
char g;
std::string h;
};
int main() {
std::string strFilename( "text.txt" );
std::ifstream file;
std::string strLine;
std::vector<std::string> vTemp;
std::vector<std::string> vResult;
std::vector<Data> vData;
// Open File For Reading
file.open( strFilename.c_str() );
// Check For Error Of Opening File
if ( !file.is_open() ) {
std::cout << "Error opening file (" << strFilename << ")" << std::endl;
return RETURN_ERROR;
}
// Continue Until End Of File
while( !file.eof() ) {
// Get Single Full Line Save To String
std::getline( file, strLine );
// Split String Using A Double Quote Delimiter
vTemp = Utility::splitString( strLine, "\"" );
// Check To See If vTemp Has More Than One String
if ( vTemp.size() > 1 ) {
// Store The Last Value Of vResult -
// We Need To Use Pop Back To Account For Last Double Quote
vTemp.pop_back(); // Remove Last Double Quote
std::string temp = vTemp.back();
vTemp.pop_back(); // Remove Wanted String From vTemp.
// At This Point We Need To Parse vTemp Again Using Space Delimiter
vResult = Utility::splitString( vTemp[0], " " );
// Need To Account For Last Space In Vector
vResult.pop_back();
// Now We Can Push Our Last String Back Into vResult
vResult.push_back( temp );
} else if ( vTemp.size() == 1 ) {
// Just Parse vTemp Using Space Delimiter
vResult = Utility::splitString( vTemp[0], " " );
}
// Print Out Results For Validity
for ( unsigned u = 0; u < vResult.size(); u++) {
std::cout << vResult.at(u) << " ";
}
std::cout << std::endl;
// Here Is Where You Would Populate Your Variables, Structures Or Classes On Each Pass Of The While Loop.
// With This Structure There Should Only Be 8 Entries Into Our vResult
Data temp;
temp.a = Utility::convertToInt( vResult[0] );
temp.b = Utility::convertToInt( vResult[1] );
temp.c = vResult[2];
temp.d = vResult[3];
temp.e = vResult[4];
temp.f = Utility::convertToInt( vResult[5] );
temp.g = vResult[6].at( 0 ); // Should Only Be One Character In This String
temp.h = vResult[7];
vData.push_back( temp );
}
std::cout << std::endl << std::endl;
// Print Using Structure For Validity
std::cout << "---------------------------------------\n";
for ( unsigned u = 0; u < vData.size(); u++ ) {
std::cout << vData[u].a << " "
<< vData[u].b << " "
<< vData[u].c << " "
<< vData[u].d << " "
<< vData[u].e << " "
<< vData[u].f << " "
<< vData[u].g << " "
<< vData[u].h << std::endl;
}
// Close File
file.close();
Utility::pressAnyKeyToQuit();
return RETURN_OK;
} // main
据我所知,这是一个完整的工作程序,已使用 MSVS 2015 Community Edition 在英特尔四核 3.0Ghz 运行 Windows 7 64 位和 8GB Ram 上成功编译和构建.没有编译或构建错误,预期数据正确打印到控制台 window。