Noob fopen Array help!!!

This is a discussion on Noob fopen Array help!!! within the C Programming forums, part of the General Programming Boards category; Hi guys this is my first post on the BB. I'm a noob in C programming and need some help ...

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    4

    Noob fopen Array help!!!

    Hi guys this is my first post on the BB. I'm a noob in C programming and need some help with fopen. I'm trying to read in 4 lines of textfile data seperated by tabs

    Code:
      -5.736  -4.936  -4.136  -3.336  -2.536  -1.736  -0.936  -0.268
         2.6     2.2     1.8     1.4       1     0.6     0.2       0
           0     2.6     2.2     1.8     1.4       1     0.6     0.2
      -439.1   -4.08   -4.08   -4.08   -4.08   -4.08   -4.08   -2.04
    and then for each row create a 1D array
    Code:
    Matrix1={-5.736,  -4.936,  -4.136,  -3.336, -2.536,  -1.736,  -0.936,  -0.268};
    Matrix2={2.6,    2.2,     1.8,     1.4,       1,     0.6,     0.2 ,      0};
    Matrix3={0 ,    2.6,     2.2,     1.8,     1.4,       1,     0.6 ,    0.2};
    B={0, -439.1,   -4.08 ,  -4.08,   -4.08,   -4.08,   -4.08,   -4.08,   -2.04};
    notice on the B vector a zero in front. How would I write this code using fopen? If anyone could give me some tips on how to code fopen stuff it would be greatly appreciated.
    Last edited by elementalwinds; 10-19-2006 at 09:31 PM.

  2. #2
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    check this out for getting started. post back any attempts and errors if any! good luck

    ps no one will give you complete solutions so please try and put effort into it. after reading the link above give it a good effort! hope to see the results

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    4
    This is what I've come up.
    Code:
          FILE *fin_data;
          fin_data =fopen ("fin_data.txt","r");
          float matrix1[8] = fgets(1,8,fin_data);
          float matrix2[8] = fgets(2,8,fin_data);
          float matrix3[8] = fgets(3,8,fin_data);
          float B[8] = fgets(2,8,fin_data);
    but I keep getting
    Code:
    initializing argument 1 of `char* fgets(char*, int, FILE*)'
    I'm to sure how to use fgets. Is the first line after fgets the line number in the textfile?

    * oh and yeah. I basically programmed a Successive over Relaxation Numerical Method for non-linear equations and just need a little help on the fopen stuff cause instead of using a textfile I hard coded the arrays.
    Last edited by elementalwinds; 10-19-2006 at 10:11 PM.

  4. #4
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    http://www.cplusplus.com/ref/cstdio/fgets.html
    and
    http://www.cplusplus.com/ref/cstring/strtok.html

    the descriptions on reference websites such as that are very helpful

    post back with questions or errors (with code) if need be!
    Last edited by nadroj; 10-19-2006 at 10:18 PM.

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    4
    This is what I came up with
    Code:
    main()
    {
          FILE *fin_data;
          char dataline[N];
          char databroken;
          
          fin_data =fopen ("fin_data.txt","r");
               fgets(dataline,100,fin_data);
               puts(dataline);
               printf("%c\n",dataline);
    databroken = strtok(dataline," ");
    while(databroken !=0)
    {
                     printf("%s\n",databroken);;
          }
    return(0);
    }
    I keep getting
    Code:
    invalid conversion from `char*' to `char'
    I'm not sure what that exactly means

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Code:
       printf("%c\n",dataline);
       databroken = strtok(dataline," ");
       while ( databroken !=0 )
       {
          printf("%s\n",databroken);;
    The data types don't match with what they should be: dataline is not a single character and databroken is not a pointer to char
    Code:
    main()
    That form went out of vogue in 1989. Perhaps a trip through the FAQ would be beneficial.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  7. #7
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    -- char dataline[N];
    where is N defined and what is its value?

    -- printf("%c\n",dataline);
    your telling printf that you want to print a char(acter) (c) but your passing it a pointer. you probably want dataline[#]

  8. #8
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    If the file is formatted as such, and you are attempting to scan values from the file, then fscanf may be a good selection.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  9. #9
    Registered User
    Join Date
    Oct 2006
    Posts
    4
    Thanks for the Help guys. I figured it out. Sweet Slumber.....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 11-19-2008, 02:36 PM
  2. array of pointers/pointer arithmetic
    By tlpog in forum C Programming
    Replies: 18
    Last Post: 11-09-2008, 07:14 PM
  3. [question]Analyzing data in a two-dimensional array
    By burbose in forum C Programming
    Replies: 2
    Last Post: 06-13-2005, 08:31 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 10:54 AM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 08:48 PM

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