Thread: reading a file

  1. #16
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Quote Originally Posted by redmondtab
    i want to print whats in the file to the screen. Did i not read it in.
    Actually no you did not, if answer is ever equal to two before it equals one. The switch-case would jump to that case and try to print a bunch of presumably empty values. Please check your logic.

    And it won't even appear on the screen since you called printf incorrectly, winning you a compile-time error.

  2. #17
    Registered User
    Join Date
    Sep 2006
    Posts
    104
    so if i write it like this will it pull it from the file and print the data to the screen, becuase that is what i want to do. IF there is data in the file priint the list. IF there is no data. Then as the user to input the data. or is there a bette way to write that statment. BEcasue i know i still need to put it into a loop with an option to teremat. But i wanted to do it in small steps so that i could walk though what i was doing and get the code correct as i went.

    Code:
    printf (fin, "%s %s %c %c\n",  LName, FName, Sex,Grade);

  3. #18
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    > BEcasue i know i still need to put it into a loop with an option to teremat
    As you read parts of the file I would believe that is a loop with a requirement to terminate eventually.

    >IF there is data in the file priint the list. IF there is no data. Then as the user to input the data.
    -Read data from the file first.
    -If that fails, ask the user if there is more data
    -If yes, get input from the user and store it somewhere. If no, exit.
    -Print that to the file using something like fprintf()
    -loop

    Code:
    scanf("%d", &Ans);
    The first argument to printf is and always was a format string, not a FILE *
    Code:
    printf ("%s %s %c %c\n",  LName, FName, Sex,Grade);

  4. #19
    Registered User
    Join Date
    Sep 2006
    Posts
    104
    I want it to print if the user says they want to see they list. I need to give them the option to see the list or just add data. and i want the list to print to the screen if i use fprintf that print to the file which is not wnat i want to do.

    i did something wrong. i think that my while statement should be changed. because there is no data to read. any help. is htere any way to put a third option in to termanit the second time around an start like a do loop till number 3 but only after it gives the user option 1 and option 2 but i need to know how to get inside of my loop i think i need a fscanf to read the data before it goes into the while statment.

    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( "e: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' :
                     fscanf(fin, "%25[^ ]. %25[^ ].%c %c\n", Nlen, STU->LName, STU->FName, STU->Sex,STU->Grade);
                     printf ("%s %s %c %c\n", LName, FName, Sex, Grade);
                      break;        
                      
                      }
                      }
     getchar ();
     getchar ();
     return 0;
    }
    Last edited by redmondtab; 10-05-2006 at 09:00 AM.

  5. #20
    Registered User
    Join Date
    Mar 2005
    Posts
    140
    Code:
    while (fscanf(fin, "%25[^ ]. %25[^ ].%c %c\n", Nlen, STU->LName, STU->FName, &STU->Sex,&STU->Grade) != EOF){
    Nlen, should not be there at all. You are trying to store a value in '25'

    You have more problems in your code with not passing an address to scanf, pay attention to your warnings

    Ans is an int, your switch conditions should be ints
    case 1: NOT case '1':

    The flow of you program does not make any sense.
    For each time you read a line from the file you want run all of your program logic?

    You'll probably want to break things into functions, some quick pseudo code for thought...
    Code:
    while(menu_choice != quit) {
      print menu();
      get_choice();
      switch(menu_choice) {
        1: new_student;
        2: print_students;
        3: quit
        default: error try again
      }
    }
    
    new_student {
      open_file in append mode
      get info from user
      write to file
      close file
    }
    
    print_students {
      open file in read mode
      print file to screen
      close file
    }
    You could read your file into a data structure instead of opening and closing the file for each read/write, but this would be a good firstcut.
    If you read all the data into an array or linked list you can work with that and only have to open the file once for reading (when your program starts) and once to write the data back out when you quit.
    Last edited by spydoor; 10-05-2006 at 09:57 AM.

  6. #21
    Registered User
    Join Date
    Sep 2006
    Posts
    104
    ok i will try it that way and show you what i have. just let me know if in going in the correct direction with it please. is this what you are talking about. I"m still working on it im getting this error the lines are highlieted in blue. SO what am i doing wrong.

    56 E:\tmitchellwk8a.cpp expected `;' before '{' token
    80 E:\tmitchellwk8a.cpp expected `;' before '{' token


    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;
    int new_student;
    int print_students;
    
    PERSON* InsertNode (PERSON*, PERSON*);     
    
    main ()
    {
        PERSON *STU; //TEmp pointer used to creat list
        int Ans, menu_choice, quit;    //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, *fout;
         
       
         
     while(menu_choice != quit) 
     {
      printf ("chose option you want to do\n");
          printf ("Option 1 : Enter New Recode");
          printf ("Option 2 : Display List");
      scanf ("%d", menu_choice);
      switch(menu_choice) {
        case '1': 
             new_student;
        case '2':
              print_students;
        case '3':
              quit;
        case '': 
         printf ("error try again");
         }
    }
    
     new_student {
      fin = fopen( "e:data.dat", "r");//open_file in append mode
       // get info from user
      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;
       fprintf(fin, "%25[^ ]. %25[^ ].%c %c\n", Nlen, STU->LName, STU->FName, STU->Sex,STU->Grade);
      fout = fclose( "e:data.dat");//close file
    }
    
     print_students {
     fin = fopen( "e:data.dat", "r"); //open file in read mode
       fscanf(fin, "%25[^ ]. %25[^ ].%c %c\n", Nlen, STU->LName, STU->FName, &STU->Sex,&STU->Grade);
                     printf ("%s %s %c %c\n", LName, FName, Sex, Grade); //print file to screen
        fout = fclose( "e:data.dat"); // close file
    }
     getchar ();
     getchar ();
     return 0;
    }
    
    //sort array
    Last edited by redmondtab; 10-05-2006 at 12:03 PM.

  7. #22
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Code:
    while (fscanf(fin, "%25[^ ]. %25[^ ].%c %c\n", Nlen, STU->LName, STU->FName, STU->Sex,STU->Grade) != EOF){
    Are there really periods [nod to those elsewhere: full stops] in the text?
    And don't compare for one possible failure before assuming you got what you wanted, check for success -- that is, did you successfully scan the 5 items?
    And why not use fgets/sscanf?
    And since it's whitespace, I guess I could have used %25s instead of %25[^ ]. (The period at is at the end of the sentence to mark the end of the sentence, not part of the format specifier.)
    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. #23
    Registered User
    Join Date
    Sep 2006
    Posts
    104

    Question

    I'm able to get it into the while statment and it shows the options. But i keep gettign a referance window in dos when i hit one of the options what did i do wrong.


    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;
        
         
    PERSON *TOP = NULL; //Initialize top list point to Null     
     fin = fopen( "e:data.txt", "rw");    
         
     while (fscanf(fin, "%25s %25s %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\n");
          printf ("Option 2 : Display List\n");
          printf ("Option 3 : Quit\n");
          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' :
                     fscanf(fin, "%25s %25s %c %c\n", Nlen, STU->LName, STU->FName, &STU->Sex, &STU->Grade);
                     printf ("%s %s %c %c\n", LName, FName, Sex, Grade);
                     break; 
                            
                  case '3' :  
                      printf ("you chose to exit.  Hit enter to close");  
                      break; 
                }//end of switch
          } // end of while
          }
     getchar ();
     getchar ();
     return 0;
    }
    Last edited by redmondtab; 10-05-2006 at 05:53 PM.

  9. #24
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    I hope no one minds this, but I've been making some attempts. Maybe something like this is closer to what the OP wants:
    Code:
    #include <assert.h>
    #include <stdio.h>
    #include <stdlib.h>
    
    #define Nlen    26
    
    typedef struct person_t {
       char FName[Nlen];
       char LName[Nlen];
       char Sex;
       char Grade;
       struct person_t *next;
    } PERSON;
    
    void amend_file( char file[], PERSON *STU );
    void show_file( char file[], PERSON *STU );
    
    int main ( void )
    {
       PERSON *STU;
       int Ans;
       char file[] = "data.txt";
    
       do
       {
          printf("1. Enter new record\n"
                 "2. Display List\n"
                 "3. Quit\n\n");
          printf("Prompt: ");
          fflush(stdout);
          if ( scanf("%d", &Ans) == 1 ) /* get answer */
          {
             STU = malloc(sizeof *STU);
             if (STU == NULL)
             {
                perror("PERSON");
                exit(EXIT_FAILURE);
             }
             switch(Ans)
             {
                case 1:
                   amend_file(file, STU);
                   break;
                case 2:
                   show_file(file, STU);
                   break;
                case 3: default:
                   Ans = 3;
                   break;
             }
          } /* /get answer */
       }
       while ( Ans != 3 );
       free(STU);
       return EXIT_SUCCESS;
    }
    /*
     * amend_file: Enter stuff into the working file.
     * Very much a bare bones function so far; work on making this better
     * Perhaps make it work with a linked list, as your struct suggests?
     */
    void amend_file( char file[], PERSON *HEAD )
    {
       assert(HEAD != NULL);
       FILE *fin = fopen(file, "a");
       if ( fin == NULL )
       {
          perror(file);
          exit(EXIT_FAILURE);
       }
       printf("Enter new record (EOF to quit): ");
       while (
        scanf("%s %s %c %c", HEAD->FName, HEAD->LName, &HEAD->Sex,
          &HEAD->Grade) == 4
       )
       {
         fprintf(fin, "%s %s %c %c\n", HEAD->FName, HEAD->LName, HEAD->Sex,
            HEAD->Grade);
         fflush(fin);
       }
       fclose(fin);
    }
    /*
     * show_file: Reads the working file.
     * Bare bones again...
     */
    void show_file ( char file[], PERSON *HEAD )
    {
      assert(HEAD != NULL);
      int i = 1;
      FILE *fout = fopen(file, "r");
      if (file == NULL)
      {
        perror(file);
        exit(EXIT_FAILURE);
      }
      while (
       fscanf(fout, "%s %s %c %c", HEAD->FName, HEAD->LName, &HEAD->Sex,
         &HEAD->Grade) == 4
      )
      {
        printf("Record %d: %s %s %c %c\n", i, HEAD->FName, HEAD->LName,
           HEAD->Sex, HEAD->Grade);
        i++;
      }
      fclose(fout);
    }
    I was able to make this text file.
    $ ./a
    1. Enter new record
    2. Display List
    3. Quit

    Prompt: 1
    Enter new record (EOF to quit): Tom Capobres M B
    George Carlin M A
    Frank Sinatra M C
    Matt Outlaw M A
    Betsy Storrs F B
    1. Enter new record
    2. Display List
    3. Quit

    Prompt: 2
    Record 1: Tom Capobres M B
    Record 2: George Carlin M A
    Record 3: Frank Sinatra M C
    Record 4: Matt Outlaw M A
    Record 5: Betsy Storrs F B
    1. Enter new record
    2. Display List
    3. Quit

    Prompt: 3

    Sorry it's just so ugly though...

  10. #25
    Registered User
    Join Date
    Sep 2006
    Posts
    104
    i was trying to do it. but i think that im taking the long way around. i put in a linked list. But i have not sorted it or even tried to see if it works. here is what i have.

    but i think you are showing me a better way to do that i can understand.

    Code:
    #include <assert.h>
    #include <stdio.h>
    #include <stdlib.h>
    
    #define Nlen    26
    
    typedef struct person_t {
       char FName[Nlen];
       char LName[Nlen];
       char Sex;
       char Grade;
       struct person_t *next;
    } PERSON;
    
    void amend_file( char file[], PERSON *STU );
    void show_file( char file[], PERSON *STU );
    void insert( struct listNode   *newnode,  /* new node */
    				struct listNode   **start, /* first node */
    				struct listNode   **last /* last node */
    				)
    {
    	struct listNode  *oldptr, *ptr;
    	if(*last == NULL) /* first node in linklist */
    	{
                 for (LName = )
                 {
    		newnode->nextPtr = NULL;
    		newnode->priorPtr = NULL;
    		*last = newnode;
    		*start = newnode;
    		return;
        }
    	}
    	ptr = *start; /* start at top of linklist */
    	oldptr = NULL;
    
    	while(ptr)
    	{
    		if(ptr->data < newnode->data )
    		{
    			oldptr = ptr;
    			ptr = ptr->nextPtr;
    		}
    		else
    		{
    			if(ptr->priorPtr)
    			{
    				ptr->priorPtr->nextPtr = newnode;
    				newnode->nextPtr = ptr;
    				newnode->priorPtr = ptr->priorPtr;
    				ptr->priorPtr = newnode;
    				return;
    			}
    			newnode->nextPtr = ptr; /* newnode first node */
    			newnode->priorPtr = NULL;
    			ptr->priorPtr = newnode;
    			*start = newnode;
    			return;
    		}
    	}
    	oldptr->nextPtr = newnode;
    	newnode->nextPtr = NULL;
    	newnode->priorPtr = oldptr;
    	*last = newnode;
    }
    int main (void)
    {
       PERSON *STU;
       int Ans;
       	struct listNode  *start = NULL;         
    	struct listNode  *last = NULL;    
    	struct listNode  *data = NULL;
    	struct listNode  *tempnode  = NULL;  
       char file[] = "e:data.txt";
    
       do
       {
          printf("1. Enter new record\n"
                 "2. Display List\n"
                 "3. Quit\n\n");
          printf("Prompt: ");
          fflush(stdout);
          if ( scanf("%d", &Ans) == 1 ) /* get answer */
          {
             STU = (PERSON*) malloc (sizeof (PERSON));
             if (STU == NULL)
             {
                perror("PERSON");
                exit(EXIT_FAILURE);
             }
             switch(Ans)
             {
                case 1:
                   amend_file(file, STU);
                   break;
                case 2:
                   show_file(file, STU);
                   break;
                case 3: default:
                   Ans = 3;
                   break;
             }
          } 
        for (LName = ){  data = (struct listNode  *) malloc(sizeof(listNode));
    		if(!data)
    			return -1;
    		data->data=  Stu->LName ;
    		insert(data, &start, &last);/}
    * /get answer */
       }
       while ( Ans != 3 );
       free(STU);
       return EXIT_SUCCESS;
    }
    /*
     * amend_file: Enter stuff into the working file.
     * Very much a bare bones function so far; work on making this better
     * Perhaps make it work with a linked list, as your struct suggests?
     */
    void amend_file( char file[], PERSON *HEAD )
    {
       assert(HEAD != NULL);
       FILE *fin = fopen(file, "a");
       if ( fin == NULL )
       {
          perror(file);
          exit(EXIT_FAILURE);
       }
       printf("Enter new record (EOF to quit): ");
       while (
        scanf("%s %s %c %c", HEAD->FName, HEAD->LName, &HEAD->Sex,
          &HEAD->Grade) == 4
       )
       {
         fprintf(fin, "%s %s %c %c\n", HEAD->FName, HEAD->LName, HEAD->Sex,
            HEAD->Grade);
         fflush(fin);
       }
       fclose(fin);
    }
    /*
     * show_file: Reads the working file.
     * Bare bones again...
     */
    void show_file ( char file[], PERSON *HEAD )
    {
      assert(HEAD != NULL);
      int i = 1;
      FILE *fout = fopen(file, "r");
      if (file == NULL)
      {
        perror(file);
        exit(EXIT_FAILURE);
      }
      while (
       fscanf(fout, "%s %s %c %c", HEAD->FName, HEAD->LName, &HEAD->Sex,
         &HEAD->Grade) == 4
      )
      {
        printf("Record %d: %s %s %c %c\n", i, HEAD->FName, HEAD->LName,
           HEAD->Sex, HEAD->Grade);
        i++;
      }
      fclose(fout);
    }
    Last edited by redmondtab; 10-05-2006 at 06:48 PM.

  11. #26
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Code:
    fflush(fin);
    Reason?

    There are perhaps still a few issues:
    main.c:20: warning: "struct listNode" declared inside parameter list
    main.c:20: warning: its scope is only this definition or declaration, which is probably not what you want
    main.c: In function `insert':
    main.c:25: error: `LName' undeclared (first use in this function)
    main.c:25: error: (Each undeclared identifier is reported only once
    main.c:25: error: for each function it appears in.)
    main.c:25: error: syntax error before ')' token
    main.c:28: error: dereferencing pointer to incomplete type
    main.c: At top level:
    main.c:34: error: `start' undeclared here (not in a function)
    main.c:34: warning: data definition has no type or storage class
    main.c:35: warning: initialization makes integer from pointer without a cast
    main.c:35: warning: data definition has no type or storage class
    main.c:37: error: syntax error before "while"
    main.c:42: error: redefinition of 'ptr'
    main.c:34: error: previous definition of 'ptr' was here
    main.c:42: error: invalid type argument of `->'
    main.c:42: warning: data definition has no type or storage class
    main.c:43: error: syntax error before '}' token
    main.c:57: error: `newnode' undeclared here (not in a function)
    main.c:57: warning: data definition has no type or storage class
    main.c:58: error: syntax error before "return"
    main.c:64: warning: data definition has no type or storage class
    main.c:65: error: syntax error before '}' token
    main.c: In function `main':
    main.c:104: error: `LName' undeclared (first use in this function)
    main.c:104: error: syntax error before ')' token
    main.c:107: error: dereferencing pointer to incomplete type
    main.c:107: error: `Stu' undeclared (first use in this function)
    main.c:108: warning: passing arg 1 of `insert' from incompatible pointer type
    main.c:108: warning: passing arg 2 of `insert' from incompatible pointer type
    main.c:108: warning: passing arg 3 of `insert' from incompatible pointer type
    main.c:108: error: syntax error before '/' token
    main.c: At top level:
    main.c:112: warning: parameter names (without types) in function declaration
    main.c:112: error: conflicting types for 'free'
    C:/Progra~1/CodeBlocks/bin/../lib/gcc/mingw32/3.4.4/../../../../include/stdlib.h:348: error: previous declaration of 'free' was here
    main.c:112: error: conflicting types for 'free'
    C:/Progra~1/CodeBlocks/bin/../lib/gcc/mingw32/3.4.4/../../../../include/stdlib.h:348: error: previous declaration of 'free' was here
    main.c:112: warning: data definition has no type or storage class
    main.c:113: error: syntax error before "return"
    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. #27
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    > Reason?
    Code:
    fflush(fin);
    None apparently. And much of the lint stuff is probably the op's doing. When I run splint on my program I get some different problems, but not nearly as many or as important perhaps. In my own defense I did throw this together the most ass way possible. Sorry about that though.

  13. #28
    Registered User
    Join Date
    Sep 2006
    Posts
    104

    Unhappy

    i can get the program top run. But need to have a loop in there so that you just enter in one persons data at a time. then send it to a linked list then to be sorted. i think that i what i want to do any help

  14. #29
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Well before you mess with the smaller program I gave you, did you develop a linked list implementation that does what you need? Did you develop a sorting algorithm that performs reasonably? Develop your program in smaller parts and then maybe what I posted here can be made as a part of the bigger picture. Don't just plug stuff in randomly like you've been doing; it creates problems and wastes time.

  15. #30
    Registered User
    Join Date
    Sep 2006
    Posts
    104
    would you like me to post my linked list and sort alagrithem them and the work on puting the two together.

    the other problem that i have is that 1 i can get out of the loop. I should be able to enter in one data and the be prompted at the list again. How do I do that. here is what i came up for my linked list. just courise how do i pass the rest of the strct so that it all prints together. So I should do the sort first then pass it to the linked list or the other way aroound

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    struct listNode
    {
    	int data;
    	struct listNode *priorPtr;
    	struct listNode *nextPtr;
    
    }ListNode;
    
    struct listNode  *start = NULL;         
    struct listNode  *last = NULL;           
    
    void insert( struct listNode   *newnode,  /* new node */
    				struct listNode   **start, /* first node */
    				struct listNode   **last /* last node */
    				)
    {
    	struct listNode  *oldptr, *ptr;
    	if(*last == NULL) /* first node in linklist */
    	{
    		newnode->nextPtr = NULL;
    		newnode->priorPtr = NULL;
    		*last = newnode;
    		*start = newnode;
    		return;
    	}
    	ptr = *start; /* start at top of linklist */
    	oldptr = NULL;
    	while(ptr)
    	{
    		if(ptr->data < newnode->data )
    		{
    			oldptr = ptr;
    			ptr = ptr->nextPtr;
    		}
    		else
    		{
    			if(ptr->priorPtr)
    			{
    				ptr->priorPtr->nextPtr = newnode;
    				newnode->nextPtr = ptr;
    				newnode->priorPtr = ptr->priorPtr;
    				ptr->priorPtr = newnode;
    				return;
    			}
    			newnode->nextPtr = ptr; /* newnode first node */
    			newnode->priorPtr = NULL;
    			ptr->priorPtr = newnode;
    			*start = newnode;
    			return;
    		}
    	}
    	oldptr->nextPtr = newnode;
    	newnode->nextPtr = NULL;
    	newnode->priorPtr = oldptr;
    	*last = newnode;
    }
    
    int main(void)
    {
    	char LName;
    	struct listNode  *start = NULL;         
    	struct listNode  *last = NULL;    
    	struct listNode  *data = NULL;
    	struct listNode  *tempnode  = NULL;   
    	srand( (unsigned)time( NULL ) );
    	for(LName = a; LName< z;x++)
    	{
    		data = (struct listNode  *) malloc(sizeof(listNode));
    		if(!data)
    			return -1;
    		data->data=  LName() ;
    		insert(data, &start, &last);
    	}
    	tempnode = start;
    	printf("From beginning to end\n");
    	while(tempnode) // print out the list  from beginning to end
    	{
    		printf("data = %d\n",tempnode->data);
    		tempnode = tempnode->nextPtr; 
    	}
    	tempnode = last;
    	printf("\n\nFrom end to beginning\n");
    	while(tempnode) // print out the list  from end to beginning
    	{
    		printf("data = %d\n",tempnode->data);
    		tempnode = tempnode->priorPtr; 
    	}
    	return 0;
    }
    Last edited by redmondtab; 10-06-2006 at 01:00 PM.

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, 02:46 PM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM