Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/160.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/delphi/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ c+中的字符数组到int+;_C++_Arrays_Char_Int - Fatal编程技术网

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()
元素。