Reading a file

This is a discussion on Reading a file within the C++ Programming forums, part of the General Programming Boards category; I am using Visual C++ 6.0 and I have a code that we have written a long time ago for ...

  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, 01:46 PM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 09:46 AM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM

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