Read complex numbers (a+bi) from text file in C++

前端 未结 4 1658
难免孤独
难免孤独 2021-01-15 09:03

i want to read an array of complex numbers (in a+bi form). There are several suggestions I found on the internet, however those methods only result the re

4条回答
  •  谎友^
    谎友^ (楼主)
    2021-01-15 09:26

    int real[10] = { 0 };
    int imaginary[10] = { 0 };
    FILE *lpFile = fopen("filename" "rt"); // Open the file
    for (int i = 0; i < 10; i++) {
        fscanf(lpFile, "%d+%di", &real[i], &imaginary[i]); // Read data 10 times
    }
    fclose(lpFile); // Close the file
    

    This example code will read 10 complex numbers from a file.

提交回复
热议问题