Reading from file

This is a discussion on Reading from file within the C Programming forums, part of the General Programming Boards category; Hello to all. I'm a newbie at C programming and this forum as well. A friend told me that I ...

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    18

    Reading from file

    Hello to all.

    I'm a newbie at C programming and this forum as well. A friend told me that I could get some good advice regarding C programming here.

    I've written a simple program which ask user to put student name and result. ( the input will be saved into a file called "test.dat")

    I'm having a problem with the program I wrote. I was able to compile and run it and it did work. The only problem was it won't display the student name and mark.

    here is the code

    Code:
    	
    	#include <stdio.h>
    	#include <stdlib.h>
    
    	struct student
    	{
    		char name[20];
    		int result;
    	}	s1[100];
    
    
    	main()
    	{
    		int menu,stuNum,counter;
    	
    	
    		printf("1.Add\n\n");
    		printf("2.Display\n\n");
    		printf("3.Exit\n\n");
    		printf("Select Menu: ");
    
    		scanf("%d", &menu);
    		
    		if(menu == 1)
    		{
    		 FILE *fp;
    		 fp = fopen("test.dat", "w");
    
    			system ("cls"); 
    			printf("How many student result do you wish to enter? ");
    			scanf("%d", &stuNum);
    
    			for(counter = 0; counter < stuNum; counter++)
    			{
    				printf("\nEnter student name: ");
    				scanf("%s", s1[counter].name);
    
    				printf("Enter student result: ");
    				scanf("%d", &s1[counter].result);
    
    			}
    
    		 fclose(fp);
    		}
    
    
    		if (menu == 2)
    		{	
    
    			FILE *fp;
    			char ch;
    			fp = fopen("test.dat","r");
    			if (fp == NULL)
    			{
    				printf("can't open file");
    				exit(1);
    			}
    
    			ch = getc(fp);
    			while(ch!=EOF)
    			{
    				putchar(ch);
    				ch=getc(fp);
    			}
    			fclose(fp);
    
    			system ("cls"); 
    		}
    
    		if (menu == 3)
    		{
    			system ("cls"); 
    			exit(1);
    		}
    	}



    I was hoping you guys can help me to solve this problem. I'll post in the code I've written. I hope someone can give me some insights on how I can solve this...


    Thanks in advance.

  2. #2
    Registered User
    Join Date
    Mar 2005
    Posts
    37
    Code:
    			for(counter = 0; counter < stuNum; counter++)
    			{
    				printf("\nEnter student name: ");
    				scanf("%s", s1[counter].name);
    
    				printf("Enter student result: ");
    				scanf("%d", &s1[counter].result);
    			}
    
    		 fclose(fp);
    		}


    In the above code you seems to be doing all fine..

    which statement of urs puts the data that u enter on the screen , into the file.. ??

    :-) happy debugging..

  3. #3
    Registered User
    Join Date
    Apr 2005
    Posts
    18
    I tried to change this part

    Code:
    for(counter = 0; counter < stuNum; counter++)
         {
               print("\nEnter student name: ");
               scan("%s", s1[counter].name);
    
               printf("Enter student result: ");
               scanf("%d", &s1[counter].result);
         } 
    
          fclose(fp);
        }
    Now the code look like this

    Code:
    for(counter = 0; counter < stuNum; counter++)
         {
               fprintf(fp, "\nEnter student name: ");
               fscanf(fp, "%s", s1[counter].name);
    
               fprintf(fp, "Enter student result: ");
               fscanf(fp, "%d", &s1[counter].result);
         } 
    
          fclose(fp);
        }
    it gave me error during runtime...
    i really don't know where my mistake are.
    (i started C 2 weeks ago that is why i don't know how to solve an error)

    can u explain where is my mistake ??

  4. #4
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    712
    You need to read from standard input and write to file.
    Here's how you can do that:
    Code:
      for(counter = 0; counter < stuNum; counter++)
      {
                   fprintf(stdout, "\nEnter student name: ");
                   fscanf(stdin,"%s", s1[counter].name);
                   clear_buf();
                               
                    fprintf(stdout, "Enter student result: ");
                    fscanf(stdin,"%d", &s1[counter].result);
                    clear_buf();
                    /*write to file after input*/
                    fprintf(fp, "%s %d\n", s1[counter].name, s1[counter].result);
     }
    I always prefer to clear buffer after input, somewhere this is necessary, and somewhere is not.
    Anyway, here's clear_buf function:
    Code:
     void clear_buf()
     {
          while(getchar() != '\n');
     }
    Hope this helps

    - Micko
    Gotta love the "please fix this for me, but I'm not going to tell you which functions we're allowed to use" posts.
    It's like teaching people to walk by first breaking their legs - muppet teachers! - Salem

  5. #5
    Registered User
    Join Date
    Mar 2005
    Posts
    37
    hi,


    Even this part needs to be taken care of.. U r not reading frm the file..

    Code:
    while(ch!=EOF)
    			{
    				putchar(ch);
    				ch=getc(fp);
    			}
    			fclose(fp);
    and regarding the err.. what was the err u were getting..

    I will just tell to how to go abt but won't take ur pleasure of getting the code rite.. !!! ;-)

    happy codeing..

  6. #6
    Registered User
    Join Date
    Jun 2004
    Posts
    201
    Quote Originally Posted by Micko
    I always prefer to clear buffer after input, somewhere this is necessary, and somewhere is not.
    Anyway, here's clear_buf function:
    Code:
     void clear_buf()
     {
          while(getchar() != '\n');
     }
    Won't this cause an infinite loop when getchar() starts returning EOF?

  7. #7
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    712
    Well, you need to clear buffer only when you're certain that there is an unwanted data, and correct way would be to do this:
    Code:
    int ch;
    ...
     while ((ch = getchar()) != '\n' && ch != EOF);
    ...
    Thanks Laserve

    - Micko
    Gotta love the "please fix this for me, but I'm not going to tell you which functions we're allowed to use" posts.
    It's like teaching people to walk by first breaking their legs - muppet teachers! - Salem

  8. #8
    Registered User
    Join Date
    Apr 2005
    Posts
    18
    Code:
    I always prefer to clear buffer after input, somewhere this is necessary, and somewhere is not.
    Anyway, here's clear_buf function:
    
    
     void clear_buf()
     {
          while(getchar() != '\n');
     }
    thx for the code Miko.

    Actually i was hoping that u will explain to me not giving the solution
    Thanks again for u'r help

  9. #9
    Registered User
    Join Date
    Apr 2005
    Posts
    18
    previously the err.. occur in this part
    Code:
    for(counter = 0; counter < stuNum; counter++)
    {
    printf("\nEnter student name: ");
    scanf("%s", s1[counter].name);
    
    printf("Enter student result: ");
    scanf("%d", &s1[counter].result);
    }
    
     fclose(fp);
    }
    Now everything is fine.
    Thanks a lot for your help guys.

    Uh but i still don't understand why we use stdout and stdin for file reading

  10. #10
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    712
    Maybe this link will help.
    Gotta love the "please fix this for me, but I'm not going to tell you which functions we're allowed to use" posts.
    It's like teaching people to walk by first breaking their legs - muppet teachers! - Salem

  11. #11
    Registered User
    Join Date
    Apr 2005
    Posts
    18
    Thank you micko.

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