Error when running a simple program

This is a discussion on Error when running a simple program within the C Programming forums, part of the General Programming Boards category; hi all, this a simple program in FILES, the program read 3 data and saved in FILE and call the ...

  1. #1
    Registered User
    Join Date
    Dec 2003
    Posts
    24

    Error when running a simple program

    hi all,
    this a simple program in FILES, the program read 3 data and saved in FILE and call the function print to print the FILE

    Code:
    #include<stdio.h>
    /*********** Data ***********/
    typedef struct {
    	int id;
    	float gpa;
    }data;
    /*********** The functions ***********/
    void read(data*);
    void print(FILE*);
    /*********** Main ***********/
    int main(){
    	data R;
    	FILE *f1;
    	int i;
    	if((f1=fopen("f1.dat","w"))==NULL)
    		printf("sorry i can not open the file\n");
    	else{
    	for(i=0;i<3;i++){
    		read(&R);
    		fprintf(f1,"%d %.2f",R.id,R.gpa);
    	}
    	fclose(f1);
    	print(f1);
    	}
    	return 0;
    }
    /*********** Read ***********/
    void read(data *R){
    	printf("Enter the id :");
    	scanf("%d",&R->id);
    	printf("\n");
    	printf("Enter the gpa :");
    	scanf("%f",&R->gpa);
    	printf("\n");
    }
    /*********** Print ***********/
    void print(FILE *f){
    	data R;
    		if((f=fopen("f.dat","r"))==NULL)
    		printf("sorry i can not open the file\n");
    	else{
    	while(!feof(f)){
    		fscanf(f,"%d %.2f",&R.id,&R.gpa);
    		printf("The id is: %d the gpa is: %.2f",R.id,R.gpa);
    	}
    	fclose(f);
    }
    }
    /*********** End ***********/
    the out is
    Enter the id :1

    Enter the gpa :2.9

    Enter the id :2

    Enter the gpa :3.8

    Enter the id :3

    Enter the gpa :4.5

    sorry i can not open the fil //why can not open??????
    Press any key to continue

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Code:
    fopen("f1.dat","w")
    vs
    Code:
    fopen("f.dat","r")

  3. #3
    Registered User
    Join Date
    Dec 2003
    Posts
    24
    i dont understand???
    can u explain to me pleas and put the program after chang

  4. #4
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Sorry I don't speak nitwit so I can't simply it any more then this:
    You are opening the file "f1.dat" to write the information and then trying to read the said information from file "f.dat".

    edit: Just so you understand, the explaintion wasn't in nitwit, it was the level right above nitwit.

  5. #5
    Registered User
    Join Date
    Dec 2003
    Posts
    24
    Its a shame having people like you in such a great board
    Simply, if you are not interested in helping beginners its better to read only. And I'm sorry I wasted you time.
    -------------------------------------------
    Can anybody else help me??
    its very important!!

  6. #6
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Well it can not open "f.dat" in your print() function because that file does not exist.

    If you want to read the information that has been written you can open "f1.dat" as that is where you wrote it.

    If you want to read some other information you have to make sure the file exists.

    Code:
    void print(FILE *f){
    	data R;
    		if((f=fopen("f.dat","r"))==NULL)
    		printf("sorry i can not open the file\n");
    	else{
    	while(!feof(f)){
    		fscanf(f,"%d %.2f",&R.id,&R.gpa);
    		printf("The id is: %d the gpa is: %.2f",R.id,R.gpa);
    	}
    	fclose(f);
    }
    As you are not using the value of f you probably want to change this function so it takes no arguments.

    Code:
    void print(void){
    	data R;
    	FILE * f;
    
    	/* Do you really want to open "f1.dat" here? */
    	if((f=fopen("f.dat","r"))==NULL)
    		printf("sorry i can not open the file\n");
    	else{
    		while(!feof(f)){
    			fscanf(f,"%d %.2f",&R.id,&R.gpa);
    			printf("The id is: %d the gpa is: %.2f",R.id,R.gpa);
    		}
    		fclose(f);
    	}
    }
    Have Fun!

  7. #7
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Jesus $$$$ing Christ. I pointed out the problem with your code and then even explained it to you. Its YOUR job to take the feedback and try to absorb it. You had a very simple mistake which was pointed out.

    Do ask questions. But don't expect us to rewrite the program. Look at how I ask questions. I give an explantion of the problem I'm encountering, what I'm trying to do, and then the code. When someone responds, I take what they give me and figure out if that solves the problems, if so how, and if not I'll respond with that and usally ask another question.

    If you can't take two feakin seconds to do a search in your code for what I put up then its not my problem.

    And yes I'm in a bad mood today.

  8. #8
    Registered User
    Join Date
    Dec 2003
    Posts
    24
    anonytmouse
    Thank you very much for your help, but still there is a littel proplem when i run the program
    i write
    ------------------
    id 1 , gpa 2.3
    id 2, gpa 3.6
    id 3,gpa 4.7
    in out is
    Enter the id :1

    Enter the gpa :2.3

    Enter the id :2

    Enter the gpa :3.6

    Enter the id :3

    Enter the gpa :4.7

    The id is: 1 the gpa is: 2.30
    The id is: 3 the gpa is: 0.60
    The id is: 4 the gpa is: 0.70
    Press any key to continue
    ------
    and now I'm working on it

  9. #9
    Registered User
    Join Date
    Dec 2003
    Posts
    24
    Thantos
    I've gotten your point
    ­We are all here to share our opinions
    I'm sorry if I bothered you

  10. #10
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Sorry, was a little over the top.

  11. #11
    Registered User
    Join Date
    Dec 2003
    Posts
    24
    what is worng now????????????
    no error when i run the program but it is give me double the last one
    the out put is
    Enter the id :1

    Enter the gpa :1.2

    Enter the id :2

    Enter the gpa :2.6

    Enter the id :3

    Enter the gpa :3.8

    The id is: 1 the gpa is: 1.20
    The id is: 2 the gpa is: 2.60
    The id is: 3 the gpa is: 3.80
    The id is: 3 the gpa is: 3.80 ///// why print again?????
    Press any key to continue
    ----
    the code
    Code:
    #include<stdio.h>
    /*********** Data ***********/
    typedef struct {
    	int id;
    	float gpa;
    }data;
    /*********** The functions ***********/
    void read(data*);
    void print(FILE*);
    /*********** Main ***********/
    int main(){
    	data R;
    	FILE *f;
    	int i;
    	if((f=fopen("f.dat","w"))==NULL)
    		printf("sorry i can not open the file\n");
    	else{
    	for(i=0;i<3;i++){
    		read(&R);
    		fprintf(f,"%d %.2f ",R.id,R.gpa);
    	}
    	fclose(f);
    
    		if((f=fopen("f.dat","r"))==NULL)
    		printf("sorry i can not open the file\n");
    	else{
    	while(!feof(f)){
    		fscanf(f,"%d %f",&R.id,&R.gpa);
    		printf("The id is: %d the gpa is: %.2f\n",R.id,R.gpa);
    	}
    	fclose(f);
    }
    	}
    	return 0;
    }
    /*********** Read ***********/
    void read(data *R){
    	printf("Enter the id :");
    	scanf("%d",&R->id);
    	printf("\n");
    	printf("Enter the gpa :");
    	scanf("%f",&R->gpa);
    	printf("\n");
    }
    /*********** Print ***********/
    void print(FILE *f1){
    	data R;
     	if((f1=fopen("f1.dat","r"))==NULL)
    		printf("sorry i can not open the file\n");
    	else{
    		fscanf(f1,"%d %f",&R.id,&R.gpa);
    	while(!feof(f1)){
    		printf("The id is: %d the gpa is: %.2f\n",R.id,R.gpa);
    		fscanf(f1,"%d %f ",&R.id,&R.gpa);
    	}
    	fclose(f1);
    }
    }
    /*********** End ***********/

  12. #12
    Registered User
    Join Date
    Dec 2001
    Posts
    44
    feof() doesn't do what you think it does... or in fact, what anonytmous thinks it does.

    feof() only tells you EOF has occured only when some other stdio function (fscanf in your case) has already reported it. So any code of the form:

    Code:
    while (!feof()) {
      int foo;
      fscanf("%d",&foo);
      printf("%d\n",foo);
    }
    doesn't do what you think it does. It should be written:

    Code:
    int status;
    do {
      int foo;
      
    
      status=fscanf("%d",&foo);
      if (status==1) {
        printf("%d\n",foo);
      } else if (status!=EOF) {
        /* fscanf failed didn't EOF, but failed to match all the input.
            The inputted data is corrupted in some way. */
      }
    } while (status!=EOF);
    with not an feof() in sight. Generally, there's no need whatsoever to call feof().

    Your mistake is not checking the return values of all your fscanf calls. Take a look in your C reference material for what fscanf returns under what circumstances.

    Ian Woods

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 07-20-2007, 09:23 AM
  2. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  3. Simple window program
    By baniakjr in forum C++ Programming
    Replies: 5
    Last Post: 03-11-2006, 02:46 PM
  4. Running my program.....estimated to take 55 hrs...
    By AssistMe in forum C Programming
    Replies: 14
    Last Post: 03-20-2005, 11:46 PM
  5. Need help with simple DAQ program
    By canada-paul in forum C++ Programming
    Replies: 12
    Last Post: 03-15-2002, 07:52 AM

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