reading file

This is a discussion on reading file within the C Programming forums, part of the General Programming Boards category; ok, im having a problem where im trying to read data in from a file and im getting extra data ...

  1. #1
    Registered User dharh's Avatar
    Join Date
    Jan 2002
    Posts
    51

    reading file

    ok, im having a problem where im trying to read data in from a file and im getting extra data I don't want. Can't quite even figure out where this data is from sometimes.

    example of file:
    Code:
    abcdefghijklmnopqrst 9999.99 breakfast 
    drink3 good wtf         1.29 beverage  
    food good wtfee         1.95 appetizer 
    steak                  19.65 dinner
    snipit of code im using:
    Code:
    #define ROWS 80           /* default rows */
    #define NAMEMAX 21        /* max size of name array */
    #define PRICEMAX 9       /* max size of price temp array */
    #define TYPEMAX 11        /* max size of type array */
    
    struct menu *getMenu(void) {
      char temp[ROWS];
      FILE *file;                /* file pointer */
      char name[NAMEMAX];        /* name of menu item */
      float price;               /* price of menu item */
      char type[TYPEMAX];        /* type of menu item */
      struct menu *theMenu;      /* head of menu item list */
      struct menu *newMenuItem;  /* new menu item */
    
      file = fopen("menu.db", "r");
    
      if (file == NULL) {
        printf("\nThe file could not be opened.");
        theMenu = NULL;
      }
      else {
        if (fscanf(file, "%20c %f %s\n", name, &price, type) != EOF) {
          printf("\n|%s| |%f| |%s|", name, price, type);
          theMenu = makeMenuItem(name, price, type);
          while (fscanf(file, "%20c %f %s\n", name, &price, type) != EOF) {
            printf("\n|%s| |%f| |%s|", name, price, type);
            newMenuItem = makeMenuItem(name, price, type);
            theMenu = insertMenuItem(theMenu, newMenuItem);
          }
        }
      }
    
      fclose(file);
    
      return (theMenu);
    }
    if I set fscanf(file, "%20c %f %s\n", name, &price, type) I get some extra character at the end of name I dont want. If I set to 21 I get an extra space. if I set to 19 I lose a char and things scew off where ill get odd data in the other variables.

    fscanf(file, "%19c %f %s\n", name, &price, type)
    Code:
    |abcdefghijklmnopqrs| |0.000000| |░|
    |t 9999.99 breakfast| |0.000000| |░|
    |drink3 good wtf    | |1.290000| |beverage|
    |food good wtfee    | |1.950000| |appetizer|
    |steak              | |19.650000| |dinner|
    fscanf(file, "%20c %f %s\n", name, &price, type)
    Code:
    |abcdefghijklmnopqrstn| |9999.990234| |breakfast|
    |drink3 good wtf     n| |1.290000| |beverage|
    |food good wtfee     n| |1.950000| |appetizer|
    |steak               n| |19.650000| |dinner|
    fscanf(file, "%21c %f %s\n", name, &price, type)
    Code:
    |abcdefghijklmnopqrst | |9999.990234| |breakfast|
    |drink3 good wtf      | |1.290000| |beverage|
    |food good wtfee      | |1.950000| |appetizer|
    |steak                | |19.650000| |dinner|
    While the extra space in this one doesn't seem so bad, I can't really have it. Am I doing something wrong, or will I have to force a '\0' in there?

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Zero out your arrays when you initialize them:

    char name[NAMEMAX] = {'\0'};

    Your program is not always filling up the array, so you print garbage values. Here's what works for me, I broke it down to just the input and output, but it should work when integrated with your code:
    Code:
    FILE *file;
    float price;
    char name[NAMEMAX] = {'\0'};
    char type[TYPEMAX] = {'\0'};
    
    file = fopen("datain.txt", "r");
    
    if (file != NULL) {
      if (fscanf(file, "%20c %f %s\n", name, &price, type) != EOF) {
        printf("\n|%s| |%.2f| |%s|", name, price, type);
        while (fscanf(file, "%20c %f %s\n", name, &price, type) != EOF) {
          printf("\n|%s| |%.2f| |%s|", name, price, type);
        }
      }
      printf ( "\n" );
      fclose(file);
    }
    -Prelude
    My best code is written with the delete key.

  3. #3
    Registered User dharh's Avatar
    Join Date
    Jan 2002
    Posts
    51
    thanks, that worked

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 PM
  2. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  3. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 09:46 AM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM

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