Thread: Problem with read-from-file process

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    25

    Problem with read-from-file process

    I’ve used the fscanf() function in the past with no problems, but for some reason, my current use of it in my C-mex s-function (Matlab-compatible C code) is causing problems. Below is a simplified version of my code (note that the mdlStart() function in a c-mex s-function is analogous to the main() function in C):

    Code:
    #define NUM_TARGS 20
     
    double targets[NUM_TARGS]; 
     
     
    static void mdlStart(SimStruct *S){
     
       int i = 0; 
       FILE *in1; 
     
       in1 = fopen(“target_vals.txt”, “r”); 
     
           /* initialize the array to 0.0 */ 
           for(i = 0; i < NUM_TARGS; i++)
                   targets[i] = 0.0; 
     
           /* read in values from input file to targets[ ] array */ 
           for(i = 0; i < NUM_TARGS; i++){
               fscanf(in1, “%f”, &targets[i]); 
               mexPrintf("targets[%d] = %f.\n", i, targets[i]);   /* each targets[] value prints as 0.0 !! */ 
           }
      
    } /* end mdlStart() */
    The format of the target_vals.txt file is a list of 20 double values, each on its own line (i.e. a newline character after each value).

    The problem: when I print the values being read into the targets[ ] array, they all display as 0.0, when the actual values contained in this file are all positive-valued doubles. I’ve also tried reading in the “%lf” type in, but that hasn’t resulted in any improvements.

    Any suggestions about what’s wrong with my code above, that’s causing it to assign all 0 values to the targets[ ] array (or maybe, never read in values from the input file at all??), instead of reading in the correct double values from the input file? I’ve read through various tutorials and examples, but I haven’t been able to recognize the problem yet.

    Thanks for your help.
    Last edited by CodeRed; 03-06-2012 at 01:17 PM.

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    You should check if fopen fails to open the file or if fscaf fails to read any values. And "%lf" should/must be used if you're saving in doubles. Also, don't forget to call fclose at the end.
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Feb 2012
    Posts
    25
    Thanks for the input, G. Sorry I forgot to include the fclose() in my pseudocode above; it *is* included in my real code. You're correct that the "%lf" was necessary to read in the double values -- I swear I tried that previously with no success, but apparently there were other differences in my code that prevented it from working. Anyway, %lf was the solution to this problem, thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Open a file, read it, display it and process char.
    By Devenc in forum C Programming
    Replies: 6
    Last Post: 04-19-2010, 11:29 PM
  2. How to read and process+output to file
    By jochen in forum C Programming
    Replies: 4
    Last Post: 11-03-2007, 02:22 PM
  3. Problem in File Process
    By ercumania in forum C Programming
    Replies: 5
    Last Post: 07-24-2006, 06:03 AM
  4. Read file and process/sort the string?
    By kevinh in forum C Programming
    Replies: 8
    Last Post: 12-20-2005, 01:04 AM
  5. Process sending file descriptors to another process
    By Yasir_Malik in forum C Programming
    Replies: 4
    Last Post: 04-07-2005, 07:36 PM