CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Feb 2009
    Posts
    2

    Reading huge matrices from text files

    I have been trying to read real numbers of the following format:

    -324.5 21.8
    23.45 -223.8
    ...

    two columns and quite a lot of rows. The code I have been using to extract it, is pasted here, only generates absurd values in the variable "Matrix". Please let me know what the problem could be. I am sure its some blunder I cant see. Thanks a lot!!!

    Heres the code:



    #include <iostream>
    #include <fstream>
    #include <math.h>
    #include <stdio.h>



    using namespace std;


    void main()
    {
    Long float Matrix[39][2];
    int col, row, i, j;
    FILE *ftp;
    col = 1; row=39;
    ftp = fopen("data.txt","r");

    for(i = 0; i < row; i++)
    for(j = 0; j < col+1; j++){
    fscanf(ftp, "%Lf", &Matrix[i][j]);
    printf("matrix[%d][%d] = %f\n", i,j,Matrix[i][j]);
    }
    system("pause");
    }

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Reading huge matrices from text files

    The capitalized "Long" is throwing me a bit. I didn't think that was a type. Second, "long float"? Don't you just mean double?

    &#37;lf (lowercase L) is the correct format specifier to read a double. If you want to just read floats then drop the l entirely.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured