Thread: 'static FILE *' in a function ...

  1. #1
    Registered User Sargnagel's Avatar
    Join Date
    Aug 2002
    Posts
    166

    Question 'static FILE *' in a function ...

    I want to write a function that fetches one line from a text file and writes it to a buffer. I just want to pass the path and/or name of the file and a reference to a buffer. With each function call the next line in the file is written to the buffer.
    Please tell me, if the code below is okay or needs some modification.
    Code:
    #include <stdio.h>
    
    #define SIZE 500
    
    int getline(char filename[], char buf[])
    {
    	static FILE *in = NULL;
    	
    	if(in == NULL)
    		in = fopen(filename, "r");
    	
    	if(!fgets(buf, SIZE, in)) 
    	{
    		/* EOF reached ... hopefully */
    		fclose(in); 
    		return 0;
    	}
    	return 1;
    }
    
    int main()
    {
    	char buf[SIZE] = {'\0'};
    		
    	while(getline("blabla.txt", buf))
    	{
    		puts(buf);
    		memset(buf, '\0', SIZE);
    	}
    	return 0;
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    What is the point of using a static file pointer? Static provides no benifit here. If you plan on holding the file pointer open, why are you closing it? Otherwise, your file pointer is invalid after you close it. Your file pointer will not null when you close the file:
    Code:
    #include <stdio.h>
    
    int main( void )
    {
    	FILE*fp=fopen("blah","w");
    	fclose(fp);
    	if( fp == NULL )
    		printf("Null.");
    	else
    		printf("nope.");
    
    	return 0;
    }
    Yes, it closes it. No, it doesn't set the pointer to null. As such, your null check will fail, and you'll try reading from a closed file.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User Sargnagel's Avatar
    Join Date
    Aug 2002
    Posts
    166
    Thank you for your example, quzah!

    I just want to read in a whole file line by line in a while loop. After the last line has been read, I don't need the file handle anymore and close the file. That's all ... nothing special.

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    The call to fgets leaves (may leave) a newline in the buffer. The call to puts appends a newline to the buffer. You may want to use fputs(buf, stdout) instead.
    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.*

  5. #5
    Registered User Sargnagel's Avatar
    Join Date
    Aug 2002
    Posts
    166
    Originally posted by Dave_Sinkula
    The call to fgets leaves (may leave) a newline in the buffer. The call to puts appends a newline to the buffer. You may want to use fputs(buf, stdout) instead.
    Aaah! This may be the reason why the debug output of another program of mine didn't work as expected! Thank you for your help, Dave.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner Needs help in Dev-C++
    By Korrupt Lawz in forum C++ Programming
    Replies: 20
    Last Post: 09-28-2010, 01:17 AM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  3. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 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. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM