I am inputting a file and parsing the text into tokens. I am attempting to take each token which consists of a real and imaginary number and trying to print them out into separate columns. Ex input file format: [[[0+0i 1+1i 9-345i -7654-1234i][0+0i......][...]][[......]]] The resulting output is suposed to be:
0 0
1 1
9 -345
-7654 -1234 etc......
I am able to successfully pull in the file and the tok was tested and it was correct. The only prblem is once the tok is put through the sscanf statement all the output is: 0 0 for all the items in the array. The simplistic form was tested and it also gave the correct output:
#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;
}
This worked like a charm However when I apply this to my code, i am only getting zeros. My code is the following:
#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);
}
Feel like this is a very simple solution I'm just missing something. Thank you in advance for the help.