Thread: Read blank delimeted textfile

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

    Read blank delimeted textfile

    Hello,

    I have a problem with reading a textfile. My actual code:

    Code:
    int main(int argc, char *argv[])
    {
      char *pstr[30];
      float *pflo[20];
      FILE *datin;
      int i,j,readint;
    
          
            datin=fopen("data.dat","r");
    
    
                    /* Data file holds always exactly 9 values */
    
                    for (j=0; j<=8; j++)
                    {
    
                    readint = fscanf(datin, "%s%f", pstr[i], &pflo[i]);
    
                    /* Output */
                    printf("%s\t%f\n", pstr[i], &pflo[i]);
                    }
    
            /* Close*/
            fclose(datin);
    
      return 0;
    }
    the textfile looks like

    Machine1 2.00012
    Machine2 2.04523
    ...
    ...
    and so on until Machine9

    I get always an memory access error and don't know why.

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    You haven't allocated memory to the array pstr. Did you mean to use a two-dimensional array of char as an array of strings (rather than the array of pointers to char that you have)?

    [edit]Pretty much the same goes for your array of pointers to float.

    [edit]And some other things.
    Code:
    #include <stdio.h>
    
    int main(void)
    {
       FILE *datin = fopen("data.dat","r");
       if ( datin != NULL )
       {
          char  pstr[9][30];
          float pflo[9];
          int   j;
          for ( j = 0; j < 9; ++j )
          {
             if ( fscanf(datin, "%s%f", pstr[j], &pflo[j]) != 2 )
             {
                break; /* OOPS */
             }
             printf("%s\t%f\n", pstr[j], pflo[j]);
          }
          fclose(datin);
       }
       return 0;
    }
    Last edited by Dave_Sinkula; 01-05-2006 at 02:00 PM.
    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.*

  3. #3
    Registered User
    Join Date
    Jan 2006
    Posts
    4
    Strange, normally it should work with the variable initialisation. No, I use string and float numbers seperately. No 2 dimensional array.

    Another version of this code works, but only the strings of the first column are written correctly. And this without memory allocation ...

  4. #4
    Registered User
    Join Date
    Jan 2006
    Posts
    4
    Nope, doesn't work

    String output is correct, value output is wrong

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    data.dat:
    Code:
    Machine1 2.00012
    Machine2 2.04523
    my output:
    Code:
    Machine1	2.000120
    Machine2	2.045230
    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.*

  6. #6
    Registered User
    Join Date
    Jan 2006
    Posts
    4
    thx, was a copy paste error

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Read() problems
    By yay4rei in forum C Programming
    Replies: 2
    Last Post: 07-21-2005, 10:47 AM
  2. How can I know the actual bytes read in a file read
    By pliang in forum C++ Programming
    Replies: 1
    Last Post: 06-08-2005, 04:23 PM
  3. Replies: 3
    Last Post: 04-27-2005, 11:50 AM
  4. What Would You Use To Read User Input?
    By djwicks in forum C Programming
    Replies: 11
    Last Post: 04-05-2005, 03:32 PM
  5. Help! Can't read decimal number
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 09-07-2001, 02:09 AM