我正在输入文件并将文本解析为令牌。我试图获取每个由实数和虚数组成的标记,并尝试将它们打印到单独的列中。防爆输入文件格式:[[[0 + 0i 1 + 1i 9-345i -7654-1234i] [0 + 0i ......] [...]] [[......]]] 结果输出为:
0 0
1 1
9 -345
-7654 -1234 etc......
我能够成功拉入文件并且测试了tok并且它是正确的。唯一的问题是,一旦tok通过sscanf声明,所有输出都是: 0 0 对于数组中的所有项目。测试了简单的形式,它也给出了正确的输出:
#include <stdio.h>
#include <string.h>
int main ()
{
double r, i;
char array[]= "[[[1+1i -4+100i 45-234i -56-78i]]]";
char *tok;
tok = strtok(array, " []");
while(tok) {
sscanf(tok, "%lf%lfi", &r, &i);
printf("%.0f%.0fi\n", r, i);
tok = strtok(NULL, " []");
}
return 0;
}
这就像一个魅力但是当我将它应用于我的代码时,我只会得到零。我的代码如下:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
char data[999999];
int line = 0;
char *tok;
double real, imag;
void main()
{
FILE * filei = fopen("signal_subband_16.ascii","r");
FILE * fileo = fopen("Output_file_simple.txt","a");
if(filei==NULL){
printf("There was an error opening the input file");
exit(1);
}
else if(fileo==NULL){
printf("There was an error opening the output file");
exit(1);
}
else{
while(fgets(data,999999,filei)){
tok = strtok(data," []");
while(tok){
// printf("%s\n",tok);
sscanf(tok, "%1f%1fi", &real, &imag);
printf("%8.0f%8.0fi\n", real, imag);
tok = strtok(NULL," []");
}
}
memset(data,0,999999);
line ++;
}
fclose(filei);
fclose(fileo);
}
感觉这是一个非常简单的解决方案,我只是遗漏了一些东西。提前感谢您的帮助。