【发布时间】:2016-10-12 09:38:53
【问题描述】:
所以我有一个这样的txt文件:
3/1995 13,25,16,14
4/1995 36,1,24,48
5/1996 39,46,35,2
233/1996 14,16,25,12
我想在另一个 txt 文件中将其修改为如下所示:
13,25,16,14
36,1,24,48
39,46,35,2
14,16,25,12
我还想将它们从 char 转换为 int 并将它们放入 2d 向量中。 这是我到目前为止所尝试的:
#include<iostream>
#include<fstream>
#include<cstring>
using namespace std;
static const int WIDTH = 10;
static const int HEIGHT = 50;
int main()
{
char level[HEIGHT][WIDTH];
ifstream file;
file.open("new.txt");
for(int i = 0; i < HEIGHT; i++)
{
for(int j = 0; j < WIDTH; j++)
{
file>>level[i][j];
cout<<level[i][j];
}cout<<endl;
}
return 0;
}
而且它不会读取空白空间,所以它只会把所有东西都搞砸。
【问题讨论】:
-
所以你想删除前面的数字,这很容易:使用
getline()获取整个行(参考:link)。然后你必须简单地擦除你从开始到第一个空白处得到的字符串。看到你的行中总是有 4 个数字,因此你也可以从剩下的字符串中删除所有“,”符号并创建一个 stringstream 对象。