Simple question (for you not for me!)

This is a discussion on Simple question (for you not for me!) within the C Programming forums, part of the General Programming Boards category; If you post the program in full, we can probably work out what's causing the bug. If it's large, I ...

  1. #31
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    868
    If you post the program in full, we can probably work out what's causing the bug. If it's large, I suggest you give a url if possible, or make it an attachment, as opposed to just posting the whole thing inline.

  2. #32
    C_newbie
    Join Date
    Oct 2005
    Location
    Greece
    Posts
    32
    I have uploaded to my homepage:
    homepage
    Here you have to download the files from Project 2! nvi383.c is my code the others are for compilation (memory.c&memory.h about using MALLOC instead of malloc-the professor wants it so!) nvi.txt is a *txt file about testing the editor. Any other *.txt file should work.
    I will get a correct version next week, but if you can find my error, you are welcome!
    Thanks again for your help.
    CU
    AA
    AC/DC: Highway to Hell!
    No speed limit, no stop signs, nobody 's gonna slow me down!

  3. #33
    Registered User
    Join Date
    Mar 2003
    Posts
    143
    Ah, thought so... you use both scanf() and fgets() functions - don't do this in the same program - otherwise you get exactly this problem. If you are having trouble choosing between the two then always use fgets().

    The reason behind the problem is that scanf() leaves the carriage return on the input buffer so as soon as you call fgets() following an scanf() it immediately picks up the carriage return and thinks that's your input.
    DavT
    -----------------------------------------------

  4. #34
    C_newbie
    Join Date
    Oct 2005
    Location
    Greece
    Posts
    32
    Thanks DavT, I will try to replace the scanf's with fgets as soon as I go back home today. I will report when I have done this.
    AC/DC: Highway to Hell!
    No speed limit, no stop signs, nobody 's gonna slow me down!

  5. #35
    C_newbie
    Join Date
    Oct 2005
    Location
    Greece
    Posts
    32
    Sorry people, I have tryed to change the scanf's with fgets, but I didn't make it! I gave up. I suppose the programm will stay as it is.
    AC/DC: Highway to Hell!
    No speed limit, no stop signs, nobody 's gonna slow me down!

  6. #36
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,059
    I didn't read this whole thread since I have an extremely short attention span. But from what I gather, you're having problems with input using fscan and fgets. So, listed below is a different approach. Only the input function and the main driver function are listed. I haven't thoroughly tested the code. That is your responsiblity. I hope it meets your requirements.

    Also, the code listed on the website has a few issues that must be addressed. I would suggest you list all items that you do not understand or do not know how to resolve. We will then resolve each item individually.

    Have fun
    Bob

    Code:
    #include <string.h>
    #include <stdlib.h>
    #include <stdio.h>
    #include "memory.h"
    
    
    #define INITIAL 64
    #define REALLOCATE ( INITIAL + 64 )
    
    char *GetInputLine(FILE *f)
    {
        size_t stBufferSize, stInput;
        char *pBuffer = NULL, *pSaveBuffer = NULL, *pInput = NULL;
    
        if ( ( pBuffer = malloc ( INITIAL ) ) == NULL )
            return NULL;
    
        stBufferSize = stInput = INITIAL;
        pInput = pBuffer;
    
        if ( fgets ( pInput, stInput, f ) == NULL ) {
            free ( pBuffer );
            return NULL;
        }
        while ( ( pSaveBuffer = strchr ( pInput, '\n' ) ) == NULL ) {
            stInput = REALLOCATE + 1;
            stBufferSize += REALLOCATE;
            if ( ( pSaveBuffer = realloc ( pBuffer, stBufferSize ) ) == NULL )
                return pBuffer;
            pBuffer = pSaveBuffer;
            pInput = pBuffer + ( stBufferSize - REALLOCATE - 1 );
    
            if ( fgets ( pInput, stInput, f ) == NULL ) {
                free ( pBuffer );
                return NULL;
            }
        }
        *pSaveBuffer = '\0';
        stInput = pSaveBuffer - pBuffer;
        if ( ( pSaveBuffer = realloc ( pBuffer, stInput + 1 ) ) == NULL )
            return pBuffer;
        return pSaveBuffer;
    }
    
    
    /*The function MAIN*/
    int main()
    {
        int n,i;            
        char szCurrentLine[MAXLINE] = {0};
        char newline[MAXLINE];
        //char filename;
        char filename[128] = {0};
        char s,t;
        char *l;
        /*Allocate memory*/
        struct list *buffer = MALLOC(sizeof (struct list));
    
        /*initialize the list*/
        buffer->head = 0;
        buffer->tail = 0;
        buffer->size = 0;
        buffer->currentpos = 0;
        buffer->current = 0;
    
        /*-----------------------------------------------*/
        /*-------------Computer-User Dialog--------------*/
        /*-----------------------------------------------*/
    
        /*Welcome Message!*/
        printf("\t--------------------------------\n");
        printf("\t| Welcome to TExt EDItor v0.01 |\n");
        printf("\t--------------------------------\n");
        printf("\t|           ()**()             |\n");
        printf("\t|          ( T  T )            |\n");
        printf("\t|           (_ee_)             |\n");
        printf("\t|          (d)  (i)            |\n");
        printf("\t|         (_)----(_)           |\n");
        printf("\t--------------------------------\n");
    
    
        printf("Insert command (or h for help):\n");
        while(1)        //To repeat the dialog!
        {
            printf("TEEDI>");
            //scanf("%s",&l);
            l =  GetInputLine (stdin);
    
            if(l[0]=='h'){
                /*List of commands*/    
                printf("You can insert the following commands:\n");
                printf("\t1. r, to read a file.\n");
                printf("\t2. w, to save the text in a file.\n");
                printf("\t3. p, to print the current line.\n");
                printf("\t4. g, to change the current line.\n");
                printf("\t5. d, to delete the current line.\n");
                printf("\t6. b, to insert a new line, before the current line.\n");
                printf("\t7. a, to insert a new line, after the current line.\n");
                printf("\t8. q, to exit\n");
            }   
    
            else if (l[0]=='r'){
                /*Dialog to read the File */
                printf("Insert filename to be read:\nTEEDI>");
    
                //scanf("%s", &filename);
                strcpy(filename, GetInputLine (stdin));
                printf("filename = %s\n", filename);
                ReadFile(buffer, filename);
            }
    
            else if (l[0]=='w'){
                /*Dialog to write/store the File*/
                printf("Insert filename to be written:\nTEEDI>");
                //scanf("%s", &filename);
                strcpy(filename, GetInputLine (stdin));
                WriteFile(buffer, filename);
            }
    
            else if (l[0]=='p'){
                /*Print the Line*/
                printf("The current line is:\n");
                PrintLine(buffer);
            }
    
            else if (l[0]=='g'){
                /*Dialog to change current line*/
                printf("Insert the number of the line you want to make current:\nTEEDI>");
                strcpy(szCurrentLine, GetInputLine (stdin));
                //scanf("%d",&n);
                //  GoToLine(buffer, n);
                GoToLine(buffer, atoi(szCurrentLine));
            }
    
            else if (l[0]=='d'){
                /*Delete the line*/
                DeleteLine(buffer);
            }
    
            else if (l[0]=='b'){
                fflush(stdout);                 /*To clean up the memory!*/
                /*Dialog to insert a new line before current line.*/
                printf("Insert the new line:\nTEEDI>");     /*The function works but I can't read the line correctly*/
                strcpy(newline, GetInputLine (stdin));
                //     fgets(newline, MAXLINE,stdin);          /*If i put the line directly in InsertBefore it gets inserted*/
                InsertBefore(buffer, newline);          /*The same at Insert after*/
            }
    
            else if (l[0]=='a'){
                fflush(stdout);
                /*Dialog to insert a new line after current line.*/
                printf("Insert the new line:\nTEEDI>");
                strcpy(newline, GetInputLine (stdin));
                //fgets(newline,MAXLINE,stdin);
                InsertAfter(buffer, newline);
            }
    
            else if (l[0]=='q'){
                /*Exit the programm*/
                exit(2);
            }
    
            else{
                /*Dialog in case of False command.*/
                printf("ERROR! False command! Try again or give command h to see a list whith all the available");
                printf(" commands.\n");
            }
        }
    }

Page 3 of 3 FirstFirst 123
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple question regarding variables
    By Flakster in forum C++ Programming
    Replies: 10
    Last Post: 05-18-2005, 08:10 PM
  2. Simple class question
    By 99atlantic in forum C++ Programming
    Replies: 6
    Last Post: 04-20-2005, 11:41 PM
  3. Simple question about pausing program
    By Noid in forum C Programming
    Replies: 14
    Last Post: 04-02-2005, 08:46 AM
  4. simple question.
    By InvariantLoop in forum Windows Programming
    Replies: 4
    Last Post: 01-31-2005, 11:15 AM
  5. simple fgets question
    By theweirdo in forum C Programming
    Replies: 7
    Last Post: 01-27-2002, 05:58 PM

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