例如说我有一个txt文件,看起来像这样的数据的这些行:如何在只读一个int,而忽略C++中的其余
43-.u87t 24days r random sentence
54pj6fcb5 22things L random sentence
我需要得到43忽略剩下的那一天,然后得到24个不理会的日子,得到r然后得到字符串,然后再用下一行做。
我最终会得到这个信息到一个输出文件,其中43将是一个ascII字符用作填充字符,24将是输出字符串的宽度,r将等于右对齐,并将字符串放入。所以输出线1是:
+++++++++random sentence
线2将是:
random sentence6666666
这不是我的确切分配,但非常相似。现在我有这个:
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
using namespace std;
int main()
{
string filename;
int a, b, c; // ASCII values for fill character
int aw, bw, cw; // width values for setwidth
char x, y, z; // character for right or left justification
string s1, s2, s3; // strings
ifstream inData;
cout << "please enter the location of the file you wish to input: " << endl;
getline(cin, filename);
inData.open(filename.c_str());
inData >>
任何帮助将不胜感激。
你已经写了一个没有包装的东西! –
你是否熟悉stringstream? – Beta
这是来自[Ivan Neeson](http://www.kumobius.com/2013/08/c-string-to-int/)的一篇很棒的文章,它展示了将'string'转换为'int'的不同方式。其中一些人有你想要的行为。 – wendelbsilva