Thread: reading and printing

  1. #1
    Registered User
    Join Date
    Oct 2006
    Location
    Germany
    Posts
    10

    reading and printing

    im trying to use fprintf() and fscanf() and it isnt working could someone tell me where im wrong in this? thanks in advanced
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    #define QUIT 5
        char filename[80];
    	char i[20], in[20], inf[20], info[20], infor[20], inform[20], informa[20], informat[20], informati[20], informatio[20], information[20];
    	int get_menu( void );
    	FILE *fp;
    
    main()
    {
    	
    	int choice = 0;
    	while (choice != QUIT)
    	{
    		
    		choice = get_menu();
    		if (choice == 1)
    		{
    			// creates file
    			puts("Enter a name for the file:");
    			scanf("%s", &filename);
    
    			if ((fp = fopen(filename, "w")) == NULL)
    			{
    				fprintf(stderr, "Error creating file %s", filename);
    				exit(1);
    			}
    			fclose(fp);
    		}
    		if (choice == 2)
    		{
    			//edits file
    			puts("Enter a file to edit:");
    			scanf("%s", &filename);
    			if ((fp = fopen(filename, "w")) == NULL)
    			{
    				fprintf(stderr, "Error opening file.");
    				exit(1);
    			}
    			
    			printf("Enter your information(no more than 11 words long): ");
    			
    			scanf("%d %d %d %d %d %d %d %d %d %d %d", &i, &in, &inf, &info, &infor, &inform, &informa, &informat, &informati, &informatio, &information);
    			
    			fprintf(fp, "%d %d %d %d %d %d %d %d %d %d %d", i, in, inf, info, infor, inform, informa, informat, informati, informatio, information);
    			
    			fclose(fp);
    		}
    		if (choice == 3)
    		{
    			
    			//reads a file
    			puts("Enter a file to read:");
    			scanf("%s", &filename);
    			if ((fp = fopen(filename, "r")) == NULL)
    			{
    				fprintf(stderr, "No exsiting file.");
    				exit(1);
    			}
    			fscanf(fp, "%d %d %d %d %d %d %d %d %d %d %d", &i, &in, &inf, &info, &infor, &inform, &informa, &informat, &informati, &informatio, &information);
    			printf("%d %d %d %d %d %d %d %d %d %d %d", i, in, inf, info, infor, inform, informa, informat, informati, informatio, information);
    			fclose(fp);
    		}
    		if (choice == 4)
    		{
    			// deletes file
    			puts("Enter a filename to remove: ");
    			scanf("%s", &filename);
    			if ( remove(filename) == 0)
    			{
    				printf("Succesful deletion of %s", filename);
    			}
    			else
    			{
    				fprintf(stderr, "Error deleting file %s", filename);
    				exit(1);
    			}
    			
    		}
    		
    	}
    	return (0);
    }
    
    int get_menu( void )
    {
    	int selection = 0;
    	/* display menu() to screen */
    	do
    	{
    		
    		printf("\n");
    		printf("1 - Create file\n");
    		printf("2 - Edit file\n");
    		printf("3 - Read file\n");
    		printf("4 - Delete file\n");
    		printf("5 - Quit\n");
    		printf("\n");
    		printf("\nEnter what you would like to do: ");
    	
    		scanf("%d", &selection);
    		
    	}
    	while (selection < 1 || selection > 5);
    	return selection;
    }

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    The problem is the variable type.

    the right code will be like that:

    Code:
    int i=0;
    scanf("%d",&i);
    printf("%d",i);
    By the way - your doing it right in the get_menu function. So change the main in the same way.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > scanf("%d %d %d %d %d %d %d %d %d %d %d", &i, &in, &inf, &info, &infor, &inform, &informa, &informat, &informati, &informatio, &information);

    > fprintf(fp, "%d %d %d %d %d %d %d %d %d %d %d", i, in, inf, info, infor, inform, informa, informat, informati, informatio, information);

    Well using %d to read and print strings is a bad idea. Maybe %s?

    Also, why all those variables?
    Why not for example
    char information[11][20];

    Then use a for loop.

    Which compiler are you using?
    If it's gcc / dev-c++ / mingw / djgpp, then use these additional compiler options
    gcc -W -Wall -ansi -pedantic -O2 prog.c
    For one thing, it will tell you when you mess up on using printf/scanf.
    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.

  4. #4
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    scanf("%s", &filename);
    Loose the ampersand.

    That make it any better ?
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  5. #5
    Registered User
    Join Date
    Oct 2006
    Location
    Germany
    Posts
    10
    im kinda dumb with for statements would it be like
    Code:
    for(information == 0; information <= 11; information >=1);
    im not to sure

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    for ( i = 0 ; i < 11 ; i++ )

    Where did >=1 come from?
    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.

  7. #7
    Registered User
    Join Date
    Oct 2006
    Location
    Germany
    Posts
    10
    Quote Originally Posted by Salem
    for ( i = 0 ; i < 11 ; i++ )

    Where did >=1 come from?
    im not to sure but im not to good with the for statements but thanks for that

  8. #8
    Registered User
    Join Date
    Oct 2006
    Location
    Germany
    Posts
    10
    Code:
    if (choice == 2)
    		{
    			//edits file
    			puts("Enter a file to edit:");
    			scanf("%s", &filename);
    			if ((fp = fopen(filename, "w")) == NULL)
    			{
    				fprintf(stderr, "Error opening file.");
    				exit(1);
    			}
    			
    			printf("Enter your information(no more than 11 words long): ");
    			for(information= 0; information < 11; information++);
    			scanf("%s %s %s %s %s %s %s %s %s %s %s", &information);
    			
    			fprintf(fp, "%s %s %s %s %s %s %s %s %s %s %s", information);
    			
    			fclose(fp);
    		}
    		if (choice == 3)
    		{
    			
    			//reads a file
    			puts("Enter a file to read:");
    			scanf("%s", &filename);
    			if ((fp = fopen(filename, "r")) == NULL)
    			{
    				fprintf(stderr, "No exsiting file.");
    				exit(1);
    			}
    			for(information = 0; information < 11; information++);
    			fscanf(fp, "%s %s %s %s %s %s %s %s %s %s %s", &information);
    			printf("%s %s %s %s %s %s %s %s %s %s %s", information);
    			fclose(fp);
    could you help by telling me whats wrong with the for statment in thismy errors that i keep getting are
    Code:
    error - Lvalue required in function main
    warning - Nonportable pointer conversion in function main
    error - Lvalue required in function main
    the compiler im using is borland c++5.5.1 for win32
    thanks for the help, again. im starting to get the hang of this

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > for(information= 0; information < 11; information++);
    > scanf("%s %s %s %s %s %s %s %s %s %s %s", &information);
    Like
    for ( i = 0 ; i < 11 ; i++ ) scanf("%s", information[i] );

    That is, if you begin with
    char information[11][20];
    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.

  10. #10
    Registered User
    Join Date
    Oct 2006
    Location
    Germany
    Posts
    10
    Quote Originally Posted by Salem
    > for(information= 0; information < 11; information++);
    > scanf("%s %s %s %s %s %s %s %s %s %s %s", &information);
    Like
    for ( i = 0 ; i < 11 ; i++ ) scanf("%s", information[i] );

    That is, if you begin with
    char information[11][20];
    i tried that and it still gives me the same errors. ill post the updated code
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    #define QUIT 5
        char filename[80];
    	char information[11][20], i[80];
    	int get_menu( void );
    	FILE *fp;
    
    main()
    {
    	
    	int choice = 0;
    	while (choice != QUIT)
    	{
    		
    		choice = get_menu();
    		if (choice == 1)
    		{
    			// creates file
    			puts("Enter a name for the file:");
    			scanf("%s", &filename);
    
    			if ((fp = fopen(filename, "w")) == NULL)
    			{
    				fprintf(stderr, "Error creating file %s", filename);
    				exit(1);
    			}
    			fclose(fp);
    		}
    		if (choice == 2)
    		{
    			//edits file
    			puts("Enter a file to edit:");
    			scanf("%s", &filename);
    			if ((fp = fopen(filename, "w")) == NULL)
    			{
    				fprintf(stderr, "Error opening file.");
    				exit(1);
    			}
    			
    			printf("Enter your information(no more than 11 words long): ");
    			for ( i = 0 ; i < 11 ; i++ );
    			scanf("%s", &information[i]);
    			
    			fprintf(fp, "%s", information[i]);
    			
    			fclose(fp);
    		}
    		if (choice == 3)
    		{
    			
    			//reads a file
    			puts("Enter a file to read:");
    			scanf("%s", &filename);
    			if ((fp = fopen(filename, "r")) == NULL)
    			{
    				fprintf(stderr, "No exsiting file.");
    				exit(1);
    			}
    			for(i = 0; i < 11; i++);
    			fscanf(fp, "%s", &information[i]);
    			printf("%s", information[i]);
    			fclose(fp);
    		}
    		if (choice == 4)
    		{
    			// deletes file
    			puts("Enter a filename to remove: ");
    			scanf("%s", &filename);
    			if ( remove(filename) == 0)
    			{
    				printf("Succesful deletion of %s", filename);
    			}
    			else
    			{
    				fprintf(stderr, "Error deleting file %s", filename);
    				exit(1);
    			}
    			
    		}
    		
    	}
    	return (0);
    }
    
    int get_menu( void )
    {
    	int selection = 0;
    	/* display menu() to screen */
    	do
    	{
    		
    		printf("\n");
    		printf("1 - Create file\n");
    		printf("2 - Edit file\n");
    		printf("3 - Read file\n");
    		printf("4 - Delete file\n");
    		printf("5 - Quit\n");
    		printf("\n");
    		printf("\nEnter what you would like to do: ");
    	
    		scanf("%d", &selection);
    		
    	}
    	while (selection < 1 || selection > 5);
    	return selection;
    }
    i really dont know what im doing wrong. i really do appreciate the help.

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > for ( i = 0 ; i < 11 ; i++ );
    Watch that ; at the end.
    This is no nothing, 11 times.

    Code:
    			for ( i = 0 ; i < 11 ; i++ ) {
    				scanf("%s", information[i]);		/* no & either */
    				fprintf(fp, "%s", information[i]);
    			}
    > i[80]
    Try
    Code:
    int i;
    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.

  12. #12
    Registered User
    Join Date
    Oct 2006
    Location
    Germany
    Posts
    10
    Quote Originally Posted by Salem
    > for ( i = 0 ; i < 11 ; i++ );
    Watch that ; at the end.
    This is no nothing, 11 times.

    Code:
    			for ( i = 0 ; i < 11 ; i++ ) {
    				scanf("%s", information[i]);		/* no & either */
    				fprintf(fp, "%s", information[i]);
    			}
    > i[80]
    Try
    Code:
    int i;
    Thanks a bunch i really feel stupid for not knowing that i guess i better start reveiwing for statements

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. NEED HELP READING FILE and PRINTING
    By geoffr0 in forum C Programming
    Replies: 4
    Last Post: 04-16-2009, 05:26 PM
  2. Reading and printing a file
    By T1m in forum C Programming
    Replies: 1
    Last Post: 01-08-2009, 01:29 PM
  3. List Reading - Printing
    By ch4 in forum C Programming
    Replies: 3
    Last Post: 06-13-2007, 10:50 AM
  4. reading a text file printing line number
    By bazzano in forum C Programming
    Replies: 4
    Last Post: 09-16-2005, 10:31 AM
  5. problems reading data into an array and printing output
    By serino78 in forum C Programming
    Replies: 4
    Last Post: 04-28-2003, 08:39 AM