reading a file

This is a discussion on reading a file within the C Programming forums, part of the General Programming Boards category; I'm trying to do Write a C program that displays a three option menu of: 1. Enter A Record, 2. ...

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    104

    Question reading a file

    I'm trying to do Write a C program that displays a three option menu of: 1. Enter A Record, 2. Display List, 3. Terminate. Option 1 should ask for Last Name (character string up to 25 long), First Name (character string up to 25 long), Sex (enumerated data type of M/F), Grade (enumerated data type of A/B/C/D/F). The data should be placed in a structure and inserted into a linked list ordered by Last Name. Option 2 should list all records on the screen. Option 3 should write the linked list to a disk file and exit. When the program is invoked it should read the existing data in the disk file if it exists before displaying the menu. After options 1 and 2 are complete the menu should be displayed again.

    I'm having an issues etting up the file to be read. ANd ideas this is what i have gotten so far.

    Code:
     #include <stdio.h>
    #include <string.h>
    
    #define FLASE 0
    #define TRUE 1
    #define Nlen 25
    
    typedef struct person{
         char FName [Nlen +1];
         char LName [Nlen +1];
         char Sex;
         char Grade;
         struct person *Next;
    } PERSON;
    
    void PrintList (PERSON*);
    int DeletePerson;
    PERSON* InsertNode (PERSON*, PERSON*);     
    
    main ()
    {
        PERSON *STU; //TEmp pointer used to creat list
        char ANS;    //User responce
        int Len,      //Name Length
            Deleted; //True if requested item deleted
        char FName [Nlen +1];  // first name
        char LName [Nlen +1];  // lase name
        char Grade, Sex; 
        FILE *fin;
         fin = fopen( "data.dat", "rw");
         
    PERSON *TOP = NULL; //Initialize top list point to Null     
        
         
     while (fscan(fin, %-*s %-*s %c %c\n, Nlen, STU->LName, STU->FName, STU->Sex,STU->Grade) != EOF){
          
     
     getchar ();
     getchar ();
     return 0;
    }

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Code:
    while (fscan(fin, %-*s %-*s %c %c\n, Nlen, STU->LName, STU->FName, STU->Sex,STU->Grade) != EOF)
    Bad guess.

    Is your data supposed to be stored in the file as text? Or the binary of the struct? If text, use fgets/sscanf (and read up on *scanf -- again if necessary). If binary, use fread.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    104
    I need to store it as text.
    Code:
    while (fscan(fin, '%-*s %-*s %c %c\n", Nlen, STU->LName, STU->FName,
    STU->Sex,STU->Grade) != EOF)
    dose this look right for know
    Code:
      #include <stdio.h>
    #include <string.h>
    
    #define FLASE 0
    #define TRUE 1
    #define Nlen 25
    
    typedef struct person{
         char FName [Nlen +1];
         char LName [Nlen +1];
         char Sex;
         char Grade;
         struct person *Next;
    } PERSON;
    
    void PrintList (PERSON*);
    int DeletePerson;
    PERSON* InsertNode (PERSON*, PERSON*);     
    
    main ()
    {
        PERSON *STU; //TEmp pointer used to creat list
        int Ans;    //User responce
        int Len,      //Name Length
            Deleted; //True if requested item deleted
        char FName [Nlen +1];  // first name
        char LName [Nlen +1];  // lase name
        char Grade, Sex; 
        FILE *fin;
         fin = fopen( "data.dat", "rw");
         
    PERSON *TOP = NULL; //Initialize top list point to Null     
        
         
     while (fscanf(fin, "%-*s %-*s %c %c\n", Nlen, STU->LName, STU->FName, STU->Sex,STU->Grade) != EOF){
          printf ("chose option you want to do\n");
          printf ("Option 1 : Enter New Recode");
          printf ("Option 2 : Display List");
          scanf ("%d", Ans);
          
           switch (Ans){
              case '1' :       
                 do {
                  STU = (PERSON*) malloc (sizeof(PERSON));
                  if (STU == NULL)
                    printf ("Error -- could not allocate memory\n\n");
                  else {
                     printf ("\nEnter students first name ( uo to %d letters):", Nlen);
                     fgets (STU->FName, Nlen+1, stdin);
                     Len = strlen (STU->FName);
                       if (STU->FName[Len-1]== '\n')
                           STU->FName [Len-1] == '\0';
                      
                      printf ("\nEnter students first name ( uo to %d letters):", Nlen);
                     fgets (STU->LName, Nlen+1, stdin);
                     Len = strlen (STU->LName);
                       if (STU->LName[Len-1]== '\n')
                           STU->LName [Len-1] = '\0';  
                       
                     printf ("Please enter Sex");
                     scanf ("%c", &STU->Sex);
                     printf ("Please enter Grade");
                     scanf ("%c", &STU->Grade);  
                     STU->Next= NULL;          
                   
     
     getchar ();
     getchar ();
     return 0;
    }

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Code:
    while (fscan(fin, '%-*s %-*s %c %c\n", Nlen, STU->LName, STU->FName,
    STU->Sex,STU->Grade) != EOF)
    The scanf family does not have these modifiers in the manner you think.

    Are you having trouble reading fgets/sscanf? It only makes life easier for you.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    104
    so what your saying is that i do not need -* in there just teh s to say that it is a string

    but know im getting this error message 44 H:\My Documents\Untitled1.cpp `malloc' undeclared (first use this function) on thsi line what dose it mean.

    Code:
    STU = (PERSON*) malloc (sizeof(PERSON));

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    35
    Quote Originally Posted by redmondtab
    but know im getting this error message 44 H:\My Documents\Untitled1.cpp `malloc' undeclared (first use this function) on thsi line what dose it mean.
    If I'm not mistaken, the compiler cannot locate the prototype for malloc, so you must add this somewhere in your code:
    Code:
    #include <stdlib.h>

  7. #7
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by redmondtab
    so what your saying is that i do not need -* in there just teh s to say that it is a string
    Yes and no. You really want to limit the width of the string input, but you can't do it that way. For now a short version is to hard-code it, like this: %25[^ ].
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    104
    ok i need help with my case statment the erors that I.m getting are
    72 H:\My Documents\Untitled1.cpp expected `while' before '2'
    72 H:\My Documents\Untitled1.cpp expected `(' before '2'
    72 H:\My Documents\Untitled1.cpp expected `)' before ':' token
    72 H:\My Documents\Untitled1.cpp expected `;' before ':' token
    72 H:\My Documents\Untitled1.cpp expected primary-expression before ':' token
    72 H:\My Documents\Untitled1.cpp expected `;' before ':' token

    Code:
      switch (Ans){
              case '1' :       
                 do {
                  STU = (PERSON*) malloc (sizeof (PERSON));
                  if (STU == NULL)
                    printf ("Error -- could not allocate memory\n\n");
                  else {
                     printf ("\nEnter students first name ( uo to %d letters):", Nlen);
                     fgets (STU->FName, Nlen+1, stdin);
                     Len = strlen (STU->FName);
                       if (STU->FName[Len-1]== '\n')
                           STU->FName [Len-1] == '\0';
                      
                      printf ("\nEnter students first name ( uo to %d letters):", Nlen);
                     fgets (STU->LName, Nlen+1, stdin);
                     Len = strlen (STU->LName);
                       if (STU->LName[Len-1]== '\n')
                           STU->LName [Len-1] = '\0';  
                       
                     printf ("Please enter Sex");
                     scanf ("%c", &STU->Sex);
                     printf ("Please enter Grade");
                     scanf ("%c", &STU->Grade);  
                     STU->Next= NULL; 
                      break;         
                     } //else statment end
                  } //do statment end  
                
                 
                case '2' :
                    printf ((fscanf(fin, "%s %s %c %c\n", Nlen, STU->LName, STU->FName, STU->Sex,STU->Grade);
                      break;

  9. #9
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    A do...while loop needs a do and a while.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    104
    HOw do I do that

  11. #11
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    If you don't know that, then why are you using it? Why not for or while?
    [edit]http://www.daniweb.com/techtalkforums/post94143-2.html
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  12. #12
    Registered User
    Join Date
    Sep 2006
    Posts
    104
    sorry blond moment. Just trying to write this a section at a time.

    how to i print whats in a file
    72 H:\My Documents\Untitled1.cpp cannot convert `FILE*' to `const char*' for argument `1' to `int printf(const char*, ...)'

    Code:
    printf (fin, "%s %s %c %c\n", Nlen, STU->LName, STU->FName, STU->Sex,STU->Grade);
    Last edited by redmondtab; 09-30-2006 at 11:58 AM.

  13. #13
    Registered User
    Join Date
    Sep 2006
    Posts
    104
    need help with this error not to sure what this error means..

    72 C:\Dev-Cpp\tmitchellwk8.cpp cannot convert `FILE*' to `const char*' for argument `1' to `int printf(const char*, ...)'
    77 C:\Dev-Cpp\tmitchellwk8.cpp expected `}' at end of input
    77 C:\Dev-Cpp\tmitchellwk8.cpp expected `}' at end of input

    Code:
      #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    
    #define FLASE 0
    #define TRUE 1
    #define Nlen 25
    
    typedef struct person{
         char FName [Nlen +1];
         char LName [Nlen +1];
         char Sex;
         char Grade;
         struct person *Next;
    } PERSON;
    
    void PrintList (PERSON*);
    int DeletePerson;
    PERSON* InsertNode (PERSON*, PERSON*);     
    
    main ()
    {
        PERSON *STU; //TEmp pointer used to creat list
        int Ans;    //User responce
        int Len,      //Name Length
            Deleted; //True if requested item deleted
        char FName [Nlen +1];  // first name
        char LName [Nlen +1];  // lase name
        char Grade, Sex, Ans1; 
        FILE *fin;
         fin = fopen( "data.dat", "rw");
         
    PERSON *TOP = NULL; //Initialize top list point to Null     
        
         
     while (fscanf(fin, "%25[^ ]. %25[^ ].%c %c\n", Nlen, STU->LName, STU->FName, STU->Sex,STU->Grade) != EOF){
         
          printf ("chose option you want to do\n");
          printf ("Option 1 : Enter New Recode");
          printf ("Option 2 : Display List");
          scanf ("%d", Ans);
          
           switch (Ans){
              case '1' :       
                 STU = (PERSON*) malloc (sizeof (PERSON));
                  if (STU == NULL)
                    printf ("Error -- could not allocate memory\n\n");
                  else {
                     printf ("\nEnter students first name ( uo to %d letters):", Nlen);
                     fgets (STU->FName, Nlen+1, stdin);
                     Len = strlen (STU->FName);
                       if (STU->FName[Len-1]== '\n')
                           STU->FName [Len-1] == '\0';
                      
                      printf ("\nEnter students first name ( uo to %d letters):", Nlen);
                     fgets (STU->LName, Nlen+1, stdin);
                     Len = strlen (STU->LName);
                       if (STU->LName[Len-1]== '\n')
                           STU->LName [Len-1] = '\0';  
                       
                     printf ("Please enter Sex");
                     scanf ("%c", &STU->Sex);
                     printf ("Please enter Grade");
                     scanf ("%c", &STU->Grade);  
                     STU->Next= NULL; 
                      break;         
                     } //else statment end
                             
                 
                case '2' :
                    printf (fin, "%s %s %c %c\n", Nlen, STU->LName, STU->FName, STU->Sex,STU->Grade);
                      break;        
     getchar ();
     getchar ();
     return 0;
    }

  14. #14
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,673
    The fprintf() function prints to a file.

  15. #15
    Registered User
    Join Date
    Sep 2006
    Posts
    104
    i want to print whats in the file to the screen. Did i not read it in.

Page 1 of 3 123 LastLast
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