Thread: Code's problem

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    5
    Rep Power
    0

    Code's problem


    Hello all,

    I'm new to C programming, and i took some project on me.

    The project seems to be easy.

    The project purpose:
    To take path and file-name from the user.
    If the file exist to ask the user if to overwrite the file, and what to write over it, and to write.
    If the file doesn't exist to ask the user if to create the file.


    - The user enters a path.

    - The user enters a file-name.

    - If the file is exist:
    - Ask the user if he wants to overwrite the file, and what to
    write over the file, and then to write on the file.

    - If the file doesn't exist:
    - Ask the user if he wants to create the file.

    ** Have to use the 'createfile' function of win32api.

    Here's what I did:

    Code:
    # include <stdio.h>
    # include <string.h>
    # include <stdlib.h>
    int main() {
        FILE* fp;
        char path[50];
        char fileName[20];
        char indx;
    
        char whatever[50];
    
        printf("Enter a path: ");
        gets(path);
    
        printf("Enter a fileName: (ONLY .txt extension\n");
        gets(fileName);
        strcat(path,fileName);
        puts(path);
        if ((fp=fopen(path, "w")) == NULL) {
            printf("You know the file doesn't exist\n");
            printf("Shall I create the file: ? Type y if you want, and type n if you don't.\n");
            gets(&indx);
            if (indx == 'y') {
                fopen(path,"wt+");
    
            }
            if (indx == 'n') {
                printf("I'm doing nothing, have a nice day !\n");
            }
        }
        else {
            printf("Overwrite the file ?. Type y if you want, and type n if you don't.\n");
            gets(&indx);
            if (indx == 'y') {
                fp=fopen(fileName, "w");
                printf("What to overwrite the file ?.\n");
                gets(whatever);
                puts(whatever);
                fputs(whatever,fp);
    
            }
            if (indx == 'n') {
                printf("I'm doing nothing, have a nice day !\n");
            }
    
    
        }
    
    
        fclose(fp);
        return;
    }

    - The code for some reason, failed exactly at the last line of the program, which mean at the last '}' sign.

    - One more important thing:
    For now, the program takes only .txt files. Is there a way
    to make the program to take any file extension along
    with .txt files ???


    I will appreciate any help.
    Really need to solve this problem.

    Thanks a lot :)
  2. #2
  3. Java Junkie
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jan 2004
    Location
    Mobile, Alabama
    Posts
    4,020
    Rep Power
    1285
    When you say it failed, what error did it give?
  4. #3
  5. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,703
    Rep Power
    480
    Because of buffer overrun, never use gets. Do use fgets.
    Where you have

    fopen(path,"wt+");

    you probably intended

    fp = fopen(path,"wt+");
    if (NULL == fp) exit((puts("uncle"),1));


    Elaborating on bullet's question,
    for whatever input you gave your program we'd also need to know about the state of your file system. Did that file exist? What are the permissions? Basically it's too much information so I hope that assigning fp fixes the problem.
    [code]Code tags[/code] are essential for python code and Makefiles!
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    5
    Rep Power
    0
    Debug Assertion Failed!

    Program: ...Folders\Documents\Visual Studio 2010\Projects\CC\Debug\CC.exe
    Files: f:\dd\vctools\crt_bld\self_x86\crt\src\fclose.c
    Line: 46

    Expression: (stream != NULL)

    For information on how your program can cause an assertion failure, see the Visual C++ documentation on asserts.

    (Press retry to debug the application)
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    5
    Rep Power
    0
    Originally Posted by b49P23TIvg
    Because of buffer overrun, never use gets. Do use fgets.
    Where you have

    fopen(path,"wt+");

    you probably intended

    fp = fopen(path,"wt+");
    if (NULL == fp) exit((puts("uncle"),1));


    Elaborating on bullet's question,
    for whatever input you gave your program we'd also need to know about the state of your file system. Did that file exist? What are the permissions? Basically it's too much information so I hope that assigning fp fixes the problem.
    Still doesn't work :S
  10. #6
  11. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,364
    Rep Power
    1870
    When you replace gets with fgets, you have to take care to remove the newline

    > Still doesn't work :S
    Thanks for clearing that up, I guess it's all solved now

    > gets(&indx);
    Aside from anything else, indx is a single char, and gets (and fgets) will always write at least TWO chars (remember, there's a \0 to follow) if they read input.

    So, post your latest code, not just "it doesn't work".
    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
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    5
    Rep Power
    0
    Originally Posted by salem
    When you replace gets with fgets, you have to take care to remove the newline

    > Still doesn't work :S
    Thanks for clearing that up, I guess it's all solved now

    > gets(&indx);
    Aside from anything else, indx is a single char, and gets (and fgets) will always write at least TWO chars (remember, there's a \0 to follow) if they read input.

    So, post your latest code, not just "it doesn't work".

    It still the same code i posted.

    Code:
    # include <stdio.h>
    # include <string.h>
    # include <stdlib.h>
    int main() {
        FILE* fp;
        char path[50];
        char fileName[20];
        char indx;
    
        char whatever[50];
    
        printf("Enter a path: ");
        gets(path);
    
        printf("Enter a fileName: (ONLY .txt extension\n");
        gets(fileName);
        strcat(path,fileName);
        puts(path);
        if ((fp=fopen(path, "w")) == NULL) {
            printf("You know the file doesn't exist\n");
            printf("Shall I create the file: ? Type y if you want, and type n if you don't.\n");
            gets(&indx);
            if (indx == 'y') {
                fopen(path,"wt+");
    
            }
            if (indx == 'n') {
                printf("I'm doing nothing, have a nice day !\n");
            }
        }
        else {
            printf("Overwrite the file ?. Type y if you want, and type n if you don't.\n");
            gets(&indx);
            if (indx == 'y') {
                fp=fopen(fileName, "w");
                printf("What to overwrite the file ?.\n");
                gets(whatever);
                puts(whatever);
                fputs(whatever,fp);
    
            }
            if (indx == 'n') {
                printf("I'm doing nothing, have a nice day !\n");
            }
    
    
        }
    
    
        fclose(fp);
        return;
    }

    Thanks for any further help :)
  14. #8
  15. No Profile Picture
    I haz teh codez!
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Dec 2003
    Posts
    2,540
    Rep Power
    2337
    So, you're just ignoring advice given to you.

    Sigh.
    I ♥ ManiacDan & requinix

    This is a sig, and not necessarily a comment on the OP:
    Please don't be a help vampire!
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    5
    Rep Power
    0
    Originally Posted by ptr2void
    So, you're just ignoring advice given to you.

    Sigh.
    I'm not ignoring advice given to me.
    Here: i have changed and have many errors now.

    Also i'm really new to C programming and not everything you guys write, is saying something to me..
    Please understand.

    I'm trying the best i can.

    Here the changes on the code:

    Code:
    # include <stdio.h>
    # include <string.h>
    # include <stdlib.h>
    int main() {
        FILE* fp;
        char path[50];
        char fileName[20];
        char indx;
    
        char whatever[50];
    
        printf("Enter a path: ");
        gets(path);
    
        printf("Enter a fileName: (ONLY .txt extension)");
        fgets(fileName);
        strcat(path,fileName);
        puts(path);
        if ((fp=fopen(path, "w")) == NULL) {
            printf("You know the file doesn't exist");
            printf("Shall I create the file: ? Type y if you want, and type n if you don't.");
            fgets(&indx);
            if (indx == 'y') {
                fp=fopen(path,"wt+");
    			if (NULL == fp) exit((puts("uncle"), 1));
    
            }
            if (indx == 'n') {
                printf("I'm doing nothing, have a nice day !\n");
            }
        }
        else {
            printf("Overwrite the file ?. Type y if you want, and type n ifyou don't.");
            fgets(&indx);
            if (indx == 'y') {
                fp=fopen(fileName, "w");
                printf("What to overwrite the file ?.");
                fgets(whatever);
    			puts(whatever);
                fputs(whatever,fp);
    
            }
            if (indx == 'n') {
                printf("I'm doing nothing, have a nice day !\n");
            }
        }
    
    
        fclose(fp);
        return;
    }
  18. #10
  19. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,090
    Rep Power
    2222
    Read what the error messages are telling you. Understand what the error messages and warnings too are telling you. Much of the time, warnings are far more important than error messages, so never ever ignore warnings.

    One of the error messages is about this line, right?
    fgets(&indx);
    Did you read the documentation on fgets? Since you obviously didn't, remember to always RTFM! ("Read The Frakking Manual!") If you don't have a help file or the man pages on your computer, then Google for the man page on fgets with man page fgets. The first of the about 46,000 search results is at http://linux.die.net/man/3/fgets, which gives fget's prototype as:
    char *fgets(char *s, int size, FILE *stream);

    . . .

    fgets() reads in at most one less than size characters from stream and stores them into the buffer pointed to by s. Reading stops after an EOF or a newline. If a newline is read, it is stored into the buffer. A terminating null byte (aq\0aq) is stored after the last character in the buffer.

    . . .

    Return Value

    gets() and fgets() return s on success, and NULL on error or when end of file occurs while no characters have been read.

    . . .

    Bugs

    Never use gets(). Because it is impossible to tell without knowing the data in advance how many characters gets() will read, and because gets() will continue to store characters past the end of the buffer, it is extremely dangerous to use. It has been used to break computer security. Use fgets() instead.
    In replacing gets with fgets, the file that you will pass to fgets is the standard input file, stdin.

    And an error that would clobber your stack to smithereens and cause a resounding crash is highlighted in red:
    Code:
        char indx;
    
        char whatever[50];
    
        printf("Enter a path: ");
        gets(path);
    
        printf("Enter a fileName: (ONLY .txt extension)");
        fgets(fileName);
        strcat(path,fileName);
        puts(path);
        if ((fp=fopen(path, "w")) == NULL) {
            printf("You know the file doesn't exist");
            printf("Shall I create the file: ? Type y if you want, and type n if you don't.");
            fgets(&indx);
    Now I repeat what salem told you but you chose to ignore:
    Originally Posted by salem
    > gets(&indx);
    Aside from anything else, indx is a single char, and gets (and fgets) will always write at least TWO chars (remember, there's a \0 to follow) if they read input.
    When you enter something that way, you will overwrite whatever is in the stack next to indx. That is called clobbering memory. It can simply result in "unexpectedly" changing another variable's values resulting in data corruption and possible bizaare behavior of your program (eg, if a for-loop control variable is clobbered, you could loop either more or fewer times than you expect). Or it can overwrite control values (eg, pointer where the stack frame is, return address from the function, etc) which can result in a crash. Or overwrite a pointer value which can result in a crash.

    Don't ignore it this time!
  20. #11
  21. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,703
    Rep Power
    480
    Code:
    /*use fgets.  Look up the man page for fgets to learn what arguments it requires */
        gets(path);
    
    /* fgets needs 3 arguments.  Please supply them */
        fgets(fileName);
    
    /* (a little strange---not clear for the user who should supply the path separator */
        strcat(path,fileName);
    
    /* index is a single character.  This is just wrong */
            fgets(&indx);
    
    /* index is a single character.  Again, fix it */
            fgets(&indx);
    
    /* study fgets interface, learn how to use it */
                fgets(whatever);
    
    /* return an integer to the operating system. */
        return EXIT_SUCCESS;
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo