C++ c+中的字符数组到int+;
我无法获取以字符数组形式存储的字符串部分C++ c+中的字符数组到int+;,c++,arrays,char,int,C++,Arrays,Char,Int,我无法获取以字符数组形式存储的字符串部分 char code1 [12]={0}; char c; string compressed_file; 我从文本文件中获取输入,直到其中出现“,”为止 cout<<"Input compressed_file name"<<endl; cin>>compressed_file; string extracted_file; cout<<"Input extracted_file name"<<
char code1 [12]={0};
char c;
string compressed_file;
我从文本文件中获取输入,直到其中出现“,”为止
cout<<"Input compressed_file name"<<endl;
cin>>compressed_file;
string extracted_file;
cout<<"Input extracted_file name"<<endl;
cin>>extracted_file;
ifstream input;
input.open(compressed_file.c_str());
ofstream decompresscode;
decompresscode.open(extracted_file.c_str());
input>>c;
while(c != ',')
{
int i=0;
code1[i]=c;
cout<<code1[i];
i++;
input>>c;
}
int old=atoi(code1);
cout<<old;
cout您总是保存在0
位置:
int i=0; // this need to be out of while loop
code1[i]=c;
cout<<code1[i];
您总是保存在位置0
:
int i=0; // this need to be out of while loop
code1[i]=c;
cout<<code1[i];
将int i=0
移动到循环外部。实际上,每次都将其重置为0
input>>c;
int i=0; //move to here
while(c != ',')
{
code1[i]=c;
cout<<code1[i];
i++;
input>>c;
}
input>>c;
int i=0//搬到这里来
而(c!=',')
{
代码1[i]=c;
coutc;
}
将int i=0移动到循环外部。实际上,每次都将其重置为0
input>>c;
int i=0; //move to here
while(c != ',')
{
code1[i]=c;
cout<<code1[i];
i++;
input>>c;
}
input>>c;
int i=0//搬到这里来
而(c!=',')
{
代码1[i]=c;
coutc;
}
考虑使用std::vector
而不是数组。这将允许更长的输入,而您不需要变量<代码> i>代码>,选择“<代码>后退”(< /代码>元素)。考虑使用<代码> STD::vector < /代码>,而不是您的数组。这将启用更长的输入,并且您不需要变量i
,而是选择back()
元素。