努比警报。啊。我在<stdio.h>
使用<fstream>
. 它们看起来都很笨重且使用起来不直观。我的意思是,为什么 C++ 不能提供一种方法来获取char*
指向文件中第一个字符的指针?这就是我想要的。
我正在做Project Euler Question 13,需要玩 50 位数字。我在文件中存储了 150 个数字13.txt
,我正在尝试创建一个 150x50 数组,以便我可以直接使用每个数字的数字。但我有很多麻烦。我尝试过使用 C++<fstream>
库,最近直接<stdio.h>
完成了它,但我不能点击某些东西。这就是我所拥有的;
#include <iostream>
#include <stdio.h>
int main() {
const unsigned N = 100;
const unsigned D = 50;
unsigned short nums[N][D];
FILE* f = fopen("13.txt", "r");
//error-checking for NULL return
unsigned short *d_ptr = &nums[0][0];
int c = 0;
while ((c = fgetc(f)) != EOF) {
if (c == '\n' || c == '\t' || c == ' ') {
continue;
}
*d_ptr = (short)(c-0x30);
++d_ptr;
}
fclose(f);
//do stuff
return 0;
}
有人可以提供一些建议吗?也许是 C++ 人,他们更喜欢哪个 I/O 库?