Thread: Reading From a text file

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    16

    Reading From a text file

    My program has runtime error and I do not know what is the problem, below are my text file followed by my coding:

    student.txt:

    10wad00800|kong meng|3.80
    10wad00900|jen sen|3.20

    Code:
    #include<stdio.h>
    
    void main()
    
    {
    	FILE *myfile;
    
    	char data1[20],data2[20],data3[5];
    	int i;
    
    	myfile=fopen("student.txt","r");
    
    	if(!myfile)
    
    		printf("file not found\n");
    
    	else
    	{
    		
    		printf("Student ID\t\tName\t\tCGPa\n"
    			"=============\t\t====\t\t====\n");
    
    		while(!feof(myfile))
    		{
    			fscanf(myfile,"%[^|]|%[^|]|%[^\n]",data1[i],data2[i],data3[i]);
    		
    			printf("%s\t\t%s\t\t%s\n",data1[i],data2[i],data3[i]);
    
    		i++;
    
    		}
    	}
    
    	fclose(myfile);
    }

  2. #2
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Don't use feof() to control loop. Question 12.2
    If you pay attention to warning, compiler will tell you that variable 'i' is used un-initialized.
    and it's int main(void).

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Also, your fclose will cause a segfault if you can't open the file (since myfile will be NULL). You should move it inside the else clause or exit immediately when you can't open the file. And, to go with Bayint Naung's comment on int main, make sure it returns an int at the end (typically zero for success, non-zero on failure).

  4. #4
    Registered User
    Join Date
    Oct 2010
    Posts
    16
    Thank you. I manage to solve my problem.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by anduril462 View Post
    Also, your fclose will cause a segfault if you can't open the file (since myfile will be NULL).
    No. Passing a valid pointer whose value is NULL is different than passing NULL. For cygwin, neither one actually crash:
    Code:
    #include<stdio.h>
    int main( void )
    {
        FILE *fp = NULL;
        int x = fclose( fp );
        
        printf( "x is %d\nready to crash? Hit enter.\n", x );
        getchar();
        
        fclose( NULL );
        printf( "If you see this, there was no segfault.\n" );
        return 0;
    }
    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    I find it intersting Cygwin doesn't segfault on either one (since Cygwin is supposed to provide Linux-like functionality) while it seg faulted on both the first and second call on my CentOS machine.

    I just spent a ton of time reading the standard and formulating well thought out response to your comment, only to realize I was the <expletive deleted> who used NULL instead of null (perhaps a little less wine before posting next time). I did learn the following about fopen and fclose along the way, however...

    According to section 7.19.5.3, paragraph 8 of the standard:
    The fopen function returns a pointer to the object controlling the stream. If the open operation fails, fopen returns a null pointer.
    It may or may not be NULL, but it's guaranteed to be a null pointer, and will be of type FILE *.

    As for fclose, section 7.19.5.1, paragraph 2 says:
    A successful call to the fclose function causes the stream pointed to by stream to be flushed and the associated file to be closed. Any unwritten buffered data for the stream are delivered to the host environment to be written to the file; any unread buffered data are discarded. Whether or not the call succeeds, the stream is disassociated from the file and any buffer set by the setbuf or setvbuf function is disassociated from the stream (and deallocated if it was automatically allocated).
    So, it seems that the handling of a null FILE * would be undefined. I guess the null pointer passed in to fclose wont necessarily cause a segfault, but it's certainly pointless to fclose a null file pointer (since it means the file was never opened in the first place), and since it may seg fault in some implementations, it's best to protect against it.
    Last edited by anduril462; 12-31-2010 at 03:30 AM.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I wasn't disagreeing with it being a bad idea to add the necessary checks - just the part where you said it would segfault, since the standard wouldn't define something like that.

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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with finding word frequency in a text file.
    By aeolusaether in forum C Programming
    Replies: 15
    Last Post: 04-04-2010, 09:59 PM
  2. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  3. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  4. Reading Character at a time from a text file
    By Giania in forum C Programming
    Replies: 8
    Last Post: 02-25-2006, 03:17 PM
  5. Outputting String arrays in windows
    By Xterria in forum Game Programming
    Replies: 11
    Last Post: 11-13-2001, 07:35 PM