Thread: Reading a file

  1. #1
    Registered User
    Join Date
    May 2004
    Posts
    2

    Reading a file

    I am using Visual C++ 6.0 and I have a code that we have written a long time ago for running some simulations. It used to work fine but now something is wrong. It cannot access a file with extension .f. This file (predat.f) includes data generated for the simulations using another code.

    What do think, does this have something to do with the file type? If yes, could anyone give me an update of what file type(s) could be used in this case? Thank you for your help!

    Code:
    #include	"param.h"
    
    extern long
    #include	"gintdat.h"
    
    extern double
    #include	"gfloatdat.h"
    
    void Addjobs(long);	
    void Iter(long);					
    void Savestat();					
    void Pstatus();							
    FILE *predat, *dataf;				
    #include	<process.h>
    
    void Update(long rtime)				
    {
    	long	fnojobs, fnomach, curjob, i, im, ind;	
    	
    	if (rtime < 10)									
    	{
    		if ( (predat = PREDAT) == NULL ) 
    		{
    		printf("\n\nCould not open file predat.f\n");		
    		printf("\nPress CTRL-C to cancel program execution.\n");		
                    cin >> apu;
    
    	               }
    
    	iter = iterind-1; 				
    		
    	fscanf(predat,"%ld %ld %ld %lf ", &seed, &fnojobs, &fnomach, &ddrge);

  2. #2
    Registered User
    Join Date
    Aug 2003
    Posts
    470
    I don't think you've given enough code to answer you problem. The line
    if ( (predat = PREDAT) == NULL )
    {
    printf("\n\nCould not open file predat.f\n");
    printf("\nPress CTRL-C to cancel program execution.\n");
    cin >> apu;

    }
    Looks wrong because you are assigning predat, a FILE*, to a PREDAT. Unless if predat is defined to be something like
    #define PREDAT stdin it's probably wrong. Secondly, you probably want to use fopen to open the file. If predat.f is a text file, you would then do something like
    Code:
    FILE* predat = fopen("predat.f", "r");
    if (predat == NULL)
    {
           fprintf(stderr, "File error\n");
           exit(-1);
    }

  3. #3
    Registered User
    Join Date
    May 2004
    Posts
    2
    You were quite right, I had written a part of the code in another file and discovering that helped me to figure out what was wrong. Thanks for your help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 PM
  2. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM