Sequential File Writing Error:

This is a discussion on Sequential File Writing Error: within the C Programming forums, part of the General Programming Boards category; Well I'm just practicing andsomething is wrong, compiler says nothing so I think it is a logical error. Code: //Creates ...

  1. #1
    Registered User
    Join Date
    Jun 2004
    Posts
    277

    Sequential File Writing Error:

    Well I'm just practicing andsomething is wrong, compiler says nothing so I think it is a logical error.

    Code:
    //Creates a sequential write file
    
    #include<stdio.h>
    
    int
    main ()
    {
    
    	int account;
    	char name[30];
    	float money;
    	FILE *cfPtr;		//Client File Ptr
    
    	if ((cfPtr = fopen ("client.dat", "w")) == NULL)
    		printf ("Unable to open the file");
    	else
    	{
    		printf ("Give an account numer, a name and the ammount of money");
    		printf ("Give EOF to stop\n");
    		scanf ("%d%s%f", &account, name, &money);
    
    		while (!feof (stdin))
    		{
    			fprintf (cfPtr, "%d %s %.2f\n", account, name, money);
    			printf ("?");
    		scanf ("%d%s%f", &account, name, &money);}
    		fclose (cfPtr);
    	}
    	return 0;
    }
    The program is writing the same register like 200000000000000000000000 into the output file, looks like I got some kind of loop.

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,163
    Read this thread to find out what (part of) your problem is:
    What's wrong with my code (HELP)

    The linked-to thread has been active the last couple of days and I know you have to. Someone inexperienced can really learn a lot by reading other problems people are having. You should try it.
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    Jun 2004
    Posts
    277
    Quote Originally Posted by itsme86
    Read this thread to find out what (part of) your problem is:
    What's wrong with my code (HELP)

    The linked-to thread has been active the last couple of days and I know you have to. Someone inexperienced can really learn a lot by reading other problems people are having. You should try it.
    Well thanks by the direction, anyway I still don't understanding what is wrong, it seem I need to flush the \n right? But on the post you pointed me ppl said it was supposed to enter a blank line, well I'm getting allways the same line. They suggested to fix it by adding a double getchar(); call, I've tried it and nothing happened... So I still kinda lost...

  4. #4
    Registered User
    Join Date
    Jun 2004
    Posts
    277
    Quote Originally Posted by Maragato
    Well thanks by the direction, anyway I still don't understanding what is wrong, it seem I need to flush the \n right? But on the post you pointed me ppl said it was supposed to enter a blank line, well I'm getting allways the same line. They suggested to fix it by adding a double getchar(); call, I've tried it and nothing happened... So I still kinda lost...
    Oki I flushed the file so things seem to be working now:
    Code:
    //Creates a sequential write file
    
    #include<stdio.h>
    
    int main (){
    
    	int account;
    	char name[30];
    	float money;
    	FILE *cfPtr;		//Client File Ptr
    
    	if ((cfPtr = fopen ("client.dat", "w")) == NULL)
    		printf ("Unable to open the file");
    	else {
    		printf ("Give an account numer, a name and the ammount of money\n");
    		printf ("Give EOF to stop\n");
    		scanf ("%d%s%f", &account, name, &money);
    	int	fflush(FILE *cfPtr);
    		while (!feof(stdin)) 
    		{
    			fprintf (cfPtr, "%d %s %.2f\n", account, name, money);
    			printf ("?");
    		scanf ("%d%s%f", &account, name, &money);
    		}
    		fclose (cfPtr);
    	}
    	return 0;
    }
    it is getting the EOF, BUT not leaving the program at the end of the loop, suggestions?

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    The problem is with your use of scanf
    As soon as you input an erroneous character, scanf will fail to make use of it, and since you do nothing to trap errors, the bad character will be there next time around the loop as well. Enter infinite loop here.

    Try this and forget all about scanf
    Code:
    char buff[BUFSIZ];
    while ( fgets( buff, BUFSIZ, stdin ) != NULL ) {
      if ( sscanf ( buff, "%d%s%f", &account, name, &money) == 3 ) {
        fprintf (cfPtr, "%d %s %.2f\n", account, name, money);
      } else {
        // error message
      }
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Registered User
    Join Date
    Jun 2004
    Posts
    277
    Quote Originally Posted by Salem
    The problem is with your use of scanf
    As soon as you input an erroneous character, scanf will fail to make use of it, and since you do nothing to trap errors, the bad character will be there next time around the loop as well. Enter infinite loop here.

    Try this and forget all about scanf
    Code:
    char buff[BUFSIZ];
    while ( fgets( buff, BUFSIZ, stdin ) != NULL ) {
      if ( sscanf ( buff, "%d%s%f", &account, name, &money) == 3 ) {
        fprintf (cfPtr, "%d %s %.2f\n", account, name, money);
      } else {
        // error message
      }
    }
    Ive tried this:
    Code:
    //Creates a sequential write file
    
    #include<stdio.h>
    
    int main ()
    {
    	char buff[BUFSIZ];
    	int account;
    	char name[30];
    	float money;
    	FILE *cfPtr;
    
    	if ((cfPtr = fopen ("client.dat", "w")) == NULL)
    		printf ("Unable to open the file");
    	else
    	{
    		printf ("Give an account numer, a name and the ammount of money\n");
    		printf ("Give EOF to stop\n");
    		//scanf ("%d%s%f", &account, name, &money);
    		//int fflush(FILE *cfPtr);
    
    while (fgets (buff, BUFSIZ, stdin) != NULL)
    	{
    			if (sscanf (buff, "%d%s%f", &account, name, &money) ==
    			    3)
    			{
    				fprintf (cfPtr, "%d %s %.2f\n", account, name,
    					 money);
    			}
    			else
    				printf ("ERROR!");
    
    
    
    			while (!feof (stdin))
    			{
    				fprintf (cfPtr, "%d %s %.2f\n", account, name,
    					 money);
    				printf ("?");
    				scanf ("%d%s%f", &account, name, &money);
    			}
    			fclose (cfPtr);
    		}
    	}
    	return 0;
    }
    But I still with somekind of infinite loop, unable to leave the program...

    And thx a lot for the tips about scanf.

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    So why do you still have that while (!feof (stdin))
    I didn't need it - neither do you
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  8. #8
    Registered User
    Join Date
    Jun 2004
    Posts
    277
    Quote Originally Posted by Salem
    So why do you still have that while (!feof (stdin))
    I didn't need it - neither do you

    I have removed that crappy piece of code, but now I have seg fault when trying to insert the second record, but it is properly generating the file and inserting the first of the registers.

    Code:
    //Creates a sequential write file
    
    #include<stdio.h>
    
    int main ()
    {
    	char buff[BUFSIZ];
    	int account;
    	char name[30];
    	float money;
    	FILE *cfPtr;
    
    	if ((cfPtr = fopen ("client.dat", "w")) == NULL)
    		printf ("Unable to open the file");
    	else
    	{
    		printf ("Give an account numer, a name and the ammount of money\n");
    		printf ("Give EOF to stop\n");
    
    while (fgets (buff, BUFSIZ, stdin) != NULL)
    	{
    			if (sscanf (buff, "%d%s%f", &account, name, &money) ==  3)
    			{
    				fprintf (cfPtr, "%d %s %.2f\n", account, name,
    					 money);
    			}
    			else
    				printf ("ERROR!");
    		fclose (cfPtr);
    		}
    	}
    	return 0;
    }
    Why thy hell I'm having seg fault?

  9. #9
    Helper
    Join Date
    Jun 2004
    Posts
    255
    Quote Originally Posted by Maragato
    I have removed that crappy piece of code, but now I have seg fault when trying to insert the second record, but it is properly generating the file and inserting the first of the registers.
    <...>
    Why thy hell I'm having seg fault?
    Try to reindent your code properly, and you will see that fclose() is not at the right place.
    Emmanuel Delahaye

    "C is a sharp tool"

  10. #10
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    Decent indentation will save your life more times that you'll know.

    Code:
    #include<stdio.h>
    
    int main()
    {
        char buff[BUFSIZ];
        int account;
        char name[30];
        float money;
        FILE *cfPtr;
    
        if ((cfPtr = fopen("client.dat", "w")) == NULL)
            printf("Unable to open the file");
        else {
            printf("Give an account numer, a name and the ammount of money\n");
            printf("Give EOF to stop\n");
    
            while (fgets(buff, BUFSIZ, stdin) != NULL) {
                if (sscanf(buff, "%d%s%f", &account, name, &money) == 3) {
                    fprintf(cfPtr, "%d %s %.2f\n", account, name, money);
                } else
                    printf("ERROR!");
                fclose(cfPtr);
            }
        }
        return 0;
    }
    You close the file inside the while loop!!!!
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  11. #11
    Registered User
    Join Date
    Jun 2004
    Posts
    277
    Quote Originally Posted by Salem
    Decent indentation will save your life more times that you'll know.

    Code:
    #include<stdio.h>
    
    int main()
    {
        char buff[BUFSIZ];
        int account;
        char name[30];
        float money;
        FILE *cfPtr;
    
        if ((cfPtr = fopen("client.dat", "w")) == NULL)
            printf("Unable to open the file");
        else {
            printf("Give an account numer, a name and the ammount of money\n");
            printf("Give EOF to stop\n");
    
            while (fgets(buff, BUFSIZ, stdin) != NULL) {
                if (sscanf(buff, "%d%s%f", &account, name, &money) == 3) {
                    fprintf(cfPtr, "%d %s %.2f\n", account, name, money);
                } else
                    printf("ERROR!");
                fclose(cfPtr);
            }
        }
        return 0;
    }
    You close the file inside the while loop!!!!

    All problems solved I love you guys!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how do you resolve this error?
    By -EquinoX- in forum C Programming
    Replies: 32
    Last Post: 11-05-2008, 03:35 PM
  2. Quantum Random Bit Generator
    By shawnt in forum C++ Programming
    Replies: 62
    Last Post: 06-18-2008, 10:17 AM
  3. Using VC Toolkit 2003
    By Noobwaker in forum Windows Programming
    Replies: 8
    Last Post: 03-13-2006, 06:33 AM
  4. Learning OpenGL
    By HQSneaker in forum C++ Programming
    Replies: 7
    Last Post: 08-06-2004, 08:57 AM
  5. Couple C questions :)
    By Divx in forum C Programming
    Replies: 5
    Last Post: 01-28-2003, 12:10 AM

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