matrix to vector

This is a discussion on matrix to vector within the C Programming forums, part of the General Programming Boards category; I have a text file with some numbers but in the form of a matrix and I want to store ...

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    5

    matrix to vector

    I have a text file with some numbers but in the form of a matrix and I want to store them as a vector. Here is what is in the file:

    3 (since its always square, I only need one number for the rows and columns)
    1 2 3
    0 1 2
    1 4 5

    and my code is:

    Code:
    main()
    {int i;
    float *XAtemp, row_col; 
    FILE *isa;
    
    isa=fopen("file.c","r");
    fscanf(isa,"%f",&row_col);
    
    XAtemp=(float*)malloc((row_col*row_col)*sizeof(float));
    for (i=0;i<(row_col*row_col);i++) 
        fscanf(isa," %f ",&XAtemp[i]);
    for (i=0;i<(row_col*row_col);i++)
       printf("\n %f \n", XAtemp[i]);
    
    system("pause");
    return 0;
    is there a way to skip the \n and tell it to continue reading?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Using fscanf, it will automatically discard \n while trying to read a number with %f.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can some one please tell me the cause of the error ?
    By broli86 in forum C Programming
    Replies: 8
    Last Post: 06-26-2008, 09:36 PM
  2. Matrix Help
    By HelpmeMark in forum C++ Programming
    Replies: 27
    Last Post: 03-06-2008, 05:57 PM
  3. Gauss-Jordan Matrix Inversion in C++
    By Max_Power82 in forum C++ Programming
    Replies: 3
    Last Post: 12-03-2006, 08:31 PM
  4. Matrix and vector operations on computers
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 05-11-2004, 07:36 AM
  5. Certain functions
    By Lurker in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2003, 01:26 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21