Arrays 从文件中计算和读取整数
好的,现在我很难从文件中读取整数。(我正在使用C++)Arrays 从文件中计算和读取整数,arrays,visual-c++,counting,Arrays,Visual C++,Counting,好的,现在我很难从文件中读取整数。(我正在使用C++) 8714 我使用VisualStudio2010并设置了调试器,以便从一个文件中获取输入并输出到另一个文件 输出仅为2,因为它仅计算行数 在任何情况下,我试图做的是计算这个文件中的所有数字,然后创建一个“大小”数组,然后将每个数字输入到数组中。现在我想这样做的原因是因为我想能够接受任意数量的整数。有人能帮我吗 我希望这个工作不用向量。 < p>编辑:这里是C++你寻找 我的C++技能有点生疏,请原谅,但这应该是 #include <i
8714
我使用VisualStudio2010并设置了调试器,以便从一个文件中获取输入并输出到另一个文件
输出仅为2,因为它仅计算行数
在任何情况下,我试图做的是计算这个文件中的所有数字,然后创建一个“大小”数组,然后将每个数字输入到数组中。现在我想这样做的原因是因为我想能够接受任意数量的整数。有人能帮我吗
<>我希望这个工作不用向量。 < p>编辑:这里是C++你寻找 <>我的C++技能有点生疏,请原谅,但这应该是
#include <iostream>
#include <fstream>
#include <string>
#include <stdlib.h>
using namespace std;
#define MAX_LINES 25
#define MAX_IN_LINE 50
int main() {
//open our file
ifstream myfile ("test.txt");
//initialize our array's
int myarray[MAX_LINES][MAX_IN_LINE];
//used for knowing how many numbers are stored in each array
int totalargs[MAX_LINES];
//rest of variables
string line,tempnum;
int end=0;
int firstarg=0,secondarg=0;
int num;
//set all of our arrays to be zero'd out
memset(myarray,0,MAX_LINES*MAX_IN_LINE);
memset(totalargs,0,MAX_LINES);
//make sure file is opened
if (myfile.is_open()) {
//get a line
getline(myfile,line);
//the first line is the number, so set it to num
num=atoi(line.c_str());
while(!myfile.eof()) {
getline(myfile,line);
//if there is a , in the line we have gotten
while((end=line.find(' ',0))!=string::npos) {
//get the number before the ,
tempnum=line.substr(0,end);
myarray[firstarg][secondarg]=atoi(tempnum.c_str());
secondarg++;
//erase the part of the line we have gotten
line.erase(0,end+1);
}
//we will have an extra number at the end after that loop
//this gets that last number
tempnum=line.substr(0,line.length());
myarray[firstarg][secondarg]=atoi(tempnum.c_str());
//set the number of args to our array
totalargs[firstarg]=secondarg;
firstarg++;
//reset arg.
secondarg=0;
}
} else {
cout << "cannot open";
}
//this is extra, but it just shows you your variables and that
//they really do have numbers in them.
cout << "num: " << num << endl;
for (int x=0;x<firstarg;x++) {
cout << "Array " << x+1 << ": " << myarray[x][0];
for (int y=1;y<=totalargs[x];y++) {
cout << "," << myarray[x][y];
}
cout << endl;
}
}
#包括
#包括
#包括
#包括
使用名称空间std;
#定义最大行数25
#在第50行中定义最大值
int main(){
//打开我们的文件
ifstream myfile(“test.txt”);
//初始化数组的
int myarray[MAX_LINE][MAX_IN_LINE];
//用于了解每个数组中存储了多少个数字
整数总参数[最大行数];
//其余变量
字符串行,tempnum;
int end=0;
int firstarg=0,secondarg=0;
int-num;
//将所有数组设置为零
memset(myarray,0,最大行数*最大行数);
memset(总参数,0,最大行);
//确保文件已打开
如果(myfile.is_open()){
//接电话
getline(myfile,line);
//第一行是数字,因此将其设置为num
num=atoi(line.c_str());
而(!myfile.eof()){
getline(myfile,line);
//如果有,在我们的队伍中
while((end=line.find(“”,0))!=string::npos){
//把电话号码放在,
tempnum=line.substr(0,结束);
myarray[firstarg][secondarg]=atoi(tempnum.c_str());
secondarg++;
//擦掉我们得到的那部分线
行。擦除(0,结束+1);
}
//在循环结束后,我们将在末尾有一个额外的数字
//这是最后一个数字
tempnum=line.substr(0,line.length());
myarray[firstarg][secondarg]=atoi(tempnum.c_str());
//设置数组的参数数
totalargs[firstarg]=secondarg;
firstarg++;
//重置参数。
secondarg=0;
}
}否则{
coutstd::ifstream in(“myFile.txt”);
int i,大小=0;
while(in>>i)//在输入端计算int
{
大小++;
}
in.seekg(0,in.beg);
int arr=新的int[大小];
对于(尺寸i=0;i>arr[i];
...
删除[]arr;
为什么不使用矢量
std::ifstream in("myFile.txt");
int i;
std::vector<int> v;
while(in>>i) // counting int at the input
{
v.push_back(i);
}
std::ifstream-in(“myFile.txt”);
int i;
std::向量v;
while(in>>i)//在输入端计算int
{
v、 推回(i);
}
“现在的输出是2,因为现在所做的是计算行数而不是单个整数。这看起来不像C++。我是C++的吗?”Dkkele:我用C++,虽然它看起来不象它。“我知道你的问题是,但是这个答案是?”EL是一种,但是我想做它没有向量或V.@ PraskHasnUpTaTi我使用C++,所以这将不完全工作…我编辑了问题,说C++,我很抱歉任何混淆这是好的,但是,它不允许我吱吱ARR使用新方法,因为它不能改变“int到int *”你是什么意思“将int更改为int*”?
#include <iostream>
#include <fstream>
#include <string>
#include <stdlib.h>
using namespace std;
#define MAX_LINES 25
#define MAX_IN_LINE 50
int main() {
//open our file
ifstream myfile ("test.txt");
//initialize our array's
int myarray[MAX_LINES][MAX_IN_LINE];
//used for knowing how many numbers are stored in each array
int totalargs[MAX_LINES];
//rest of variables
string line,tempnum;
int end=0;
int firstarg=0,secondarg=0;
int num;
//set all of our arrays to be zero'd out
memset(myarray,0,MAX_LINES*MAX_IN_LINE);
memset(totalargs,0,MAX_LINES);
//make sure file is opened
if (myfile.is_open()) {
//get a line
getline(myfile,line);
//the first line is the number, so set it to num
num=atoi(line.c_str());
while(!myfile.eof()) {
getline(myfile,line);
//if there is a , in the line we have gotten
while((end=line.find(' ',0))!=string::npos) {
//get the number before the ,
tempnum=line.substr(0,end);
myarray[firstarg][secondarg]=atoi(tempnum.c_str());
secondarg++;
//erase the part of the line we have gotten
line.erase(0,end+1);
}
//we will have an extra number at the end after that loop
//this gets that last number
tempnum=line.substr(0,line.length());
myarray[firstarg][secondarg]=atoi(tempnum.c_str());
//set the number of args to our array
totalargs[firstarg]=secondarg;
firstarg++;
//reset arg.
secondarg=0;
}
} else {
cout << "cannot open";
}
//this is extra, but it just shows you your variables and that
//they really do have numbers in them.
cout << "num: " << num << endl;
for (int x=0;x<firstarg;x++) {
cout << "Array " << x+1 << ": " << myarray[x][0];
for (int y=1;y<=totalargs[x];y++) {
cout << "," << myarray[x][y];
}
cout << endl;
}
}
vector<int> numbers;
ifstream fin("infile.txt");
int x;
while( fin >> x ) {
numbers.push_back(x);
}
std::ifstream in("myFile.txt");
int i, size=0;
while(in>>i) // counting int at the input
{
size++;
}
in.seekg (0, in.beg);
int arr=new int[size];
for(size_t i=0;i<size;++i)
in>>arr[i];
...
delete[]arr;
std::ifstream in("myFile.txt");
int i;
std::vector<int> v;
while(in>>i) // counting int at the input
{
v.push_back(i);
}