I am attempting to extract two columns of numbers from a text file. first column is the real part of the number and the second in the imaginary part. I managed to extract the list of numbers from the file as strings but I don't know how to separate the strings into two parts. I have attempted to use the sscanf function but just hasnt worked. The difficult part is the numbers can be both positive and negative therefore I cant use + and - in the delimiter of the strtok function since it will remove the negatives. Iv been stuck for a few days so any suggestions would be appreciated. Thanks in advance. Here is the code I worte which errors at the sscanf line.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <complex.h>
char array[35]= "[[[1+1i -4+100i 45-234i -56-78i]]]";
char *arrayp[35];
int count,n,i,j;
double complex z1;
double real = 0;
double imaginary = 0;
int main()
{
arrayp[0] = strtok(array," []");
n=1;
while (arrayp[n-1]!=NULL)
{
arrayp[n] = strtok(NULL, " []");
n++;
}
// up to this point it has been tested to work. the remaining code is very
// sloppy since I have tried 8 different things and have quickly written one of
// the options tried.
for(j=0;j<n;j++)
{
if (strchr(*arrayp, '+'))
{
sscanf(arrayp[j],"%f+%fi", real, imaginary);
}
else if(arrayp string has equal to more than 1 '-')
{
sscanf(arrayp[j],"%f%fi", real, imaginary);
}
}
}
The output should be something like this:
0 0
-4 100
45 -234
-56 -78
I noticed there are mistakes such are trying to search *arrayp in strchr but its a pointer I dont know how to convert a pointer into a string so i can put it into this file. Thank you for the help and effort in advance.