reading data format into program

This is a discussion on reading data format into program within the C Programming forums, part of the General Programming Boards category; I'm working on that education quizzer program that I got some hints on awhile ago. Now I'm adding the ability ...

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    380

    reading data format into program

    I'm working on that education quizzer program that I got some hints on awhile ago. Now I'm adding the ability to read and write the program data on disk.

    The problem that I am having is reading the data. When the data is displayed newlines are shown where there shouldn't be any. I think newlines are being added during the loading process.

    Here's my code to load the data into the program
    Code:
    int loadDatabank(char filename[25])
    { //loads databank off disk
       int ctr;
       int i;
       FILE *fPtr;
       char buffer[80];
       printf("Enter filename to be loaded: ");
       scanf("%s", filename);
       strcat(filename,".txt");
       fPtr = fopen(filename, "r+");
       if(!fPtr)
       { printf("A problem occurred whie trying to open the file.\n");
          pressKey();
          return 0;
      }
      fseek(fPtr,0L,SEEK_SET);
      fgets(buffer,sizeof(buffer),fPtr);
      sscanf(buffer,"%d",&numList);
      for(ctr = 0;ctr <= numList; ctr++)
     {
        fgets(buffer,sizeof(buffer),fPtr);
        sscanf(buffer,"%d",&list[ctr].type);
        fgets(list[ctr].question,sizeof(list[ctr].question),fPtr);
    
        if(list[ctr].type == QUEST_MULTIPLE)
       {
           fgets(buffer,sizeof(buffer),fPtr);
           sscanf(buffer,"%d",&list[ctr].nChoices);
           i = 0;
           while(i < list[ctr].nChoices)
           {
               if(fgets(list[ctr].choices[i],sizeof(list[ctr].choices[i]),fPtr) == NULL)
               break;
               i++;
               }
            }
            fgets(list[ctr].answer,sizeof(list[ctr].answer),fPtr);
                 strtok(list[ctr].answer,"\n");
            }
    
           fclose(fPtr);
           printf("File successfully loaded.");
           pressKey();
       return 1;
    }
    The comments were added for this post.
    File format:
    Code:
    4  /* total entries in file */
    0 /* question type: 0 direct, 1 multiple choice, ect. */
    What is 1 + 1? /* question*/
    2 /*answer*/
    
    2
    Red is red.
    true
    
    3
    -------- becomes before January.
    February
    
    1
    The perimeter of a square is 24. What is the area?
    3 /* number of choices */
    36 sq. in.
    6 sq. in.
    60 sq. in.
    A
    Don't you dare hit me on the head, you know I'm not normal.
    A Stooge Site
    Green Frog Software

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,589
    Well this fgets() leaves the newline
    fgets(list[ctr].question,sizeof(list[ctr].question),fPtr);

    Adopt a consistent approach, like
    - always fgets() into the same buffer, then strcpy() the result to where you want it / sscanf() it to extract info
    - always remove the \n before doing anything else
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  2. Replies: 26
    Last Post: 06-15-2005, 02:38 PM
  3. How To Embed Data In Program While Compile?
    By ooosawaddee3 in forum C++ Programming
    Replies: 1
    Last Post: 09-09-2002, 10:14 AM
  4. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM

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