Thread: How to store sentences with whitespace into a txt file?

  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    13

    Question How to store sentences with whitespace into a txt file?

    Hi all,

    I've encountered a problem with my program. I'm trying to make a multiple choice quiz, you're able to add questions & answers to a text file which will be asked when you play the game.

    When i try to add a question with my code it can't contain whitespaces, for example if i type in: What is round and is used for soccer?

    It will absorp the following in the txt file which is supposed to contain the questions:

    [Q]What
    [A1]is
    [A2]round
    [A3]and

    How can i add sentences? I'm trying to get this:

    [Q]What is round and is used for soccer?
    [A1]A football
    [A2]A tennisball
    [A3]A table

    Here's my code:

    Code:
    void vragenadministratie()
    {
    	FILE* vragen;
    	char vraag[20];
    	char antw[20][20], antw1[20][20], antw2[20][20];
    	int keuze;
    
    	if ((vragen=fopen("vragen.txt", "a+"))==NULL)    /* bestand in /BIN/ openen */
    	{
    		printf("Het vragen-bestand kan niet gevonden worden!");
    		exit(1);
    	}                
    		printf("Typ uw nieuwe vraag:\n");
    	scanf("%s", vraag);
    	fprintf(vragen, "\n[Q]%s", vraag);   /* schrijf vraag weg naar vragen.txt */
    		printf("Geef antwoord 1:\n");
    	scanf("%s", antw);
    	fprintf(vragen, "\n[A1]%s\n", antw);  /* schrijf antw1 weg naar bestand */
    		printf("Geef antwoord 2:\n");
    	scanf("%s", antw1);
    	fprintf(vragen, "[A2]%s\n", antw1);  /* schrijf antw2 weg naar bestand */
    		printf("Geef antwoord 3:\n");
    	scanf("%s", antw2);
    	fprintf(vragen, "[A3]%s\n", antw2);  /* schrijf antw3 weg naar bestand */
    
    	fclose(vragen); 					/* sluit fopen om weg te schrijven & opslaan */
    
    	printf("\n++++++++++++++++++++++++++++++++++");
    	printf("\nUw vraag is: %s", vraag);
    	printf("\nUw antwoorden zijn: \n A1: %s \n A2: %s \n A3: %s", antw, antw1, antw2);
    	printf("\n++++++++++++++++++++++++++++++++++");
    	printf("\n\n+1 : Speel een spel");
    	printf("\n+2 : Voeg nog een vraag toe\n");
    
    	scanf("%d", &keuze);
    	/* Keuze na aanmaken gebruiker */
    	switch ( keuze )
    		{
    		case 1:
    			login();
    			break;
    		case 2:
    			vragenadministratie();
    			break;
    		default:
    			printf("Error: Foutieve invoer!");
    			break;
    			}
    Thanks in advance.

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    to scanf string including spaces you can use format like %[^\n]
    It will read any character encountered till it finds \n character
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Jan 2007
    Posts
    13

    Question

    Quote Originally Posted by vart
    to scanf string including spaces you can use format like %[^\n]
    It will read any character encountered till it finds \n character
    thanks for your very quick reply, i tried to implement this but it still doesn't seem to work

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Could you post the smallest possible part of the code that can be compiled and still reproduces your problem?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    use fgets() function to read a string. Probably that should solve the problem. Make a note that u got to remove \n char from the string before u write that string on to the file. Or remove the \n char from the fprintf.

    ssharish2005

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  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. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  5. archive format
    By Nor in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 08-05-2003, 07:01 PM