Thread: Function is called and I am trying to open a file

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    78

    Function is called and I am trying to open a file

    I am calling a function that's supposed to ask the user to name the file he wishes to open. Then, this file is opened using the fopen() function. I am a little lost as how this works. Here's the code, which doesn't work as it crashes the program.
    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <string.h>
    
    void create(FILE*);	/*function prototype*/
    
    int main()
    {
    	int ch;
    	FILE*editor;
    	 
    
    	do 
    	{
    
    	printf("\nPlease choose an option below by typing a number between 1 and 8.\n\n");
    
    	printf("1\tCreate text file\n\n");
    	printf("2\tCount Characters,words and sentences\n\n");
    	printf("3\tConvert to lowercase\n\n");
    	printf("4\tConvert to uppercase\n\n");
    	printf("5\tEncrypt text\n\n");
    	printf("6\tDecrypt text\n\n");
    	printf("7\tDisplay text file\n\n");
    	printf("8\tExit program\n\n");
    	scanf("%d", &ch);
    
    	
    
    	switch(ch) {
    		case 1:
    		  create(editor);
    		case 2:
    			printf("\nJust is just a test");
    		}
    }while(ch!=0);
    
    return 0;
    
    }
    void create(FILE*fname)
    {
    	//FILE*fname;
    	char fileName[13];
    
    	printf("\nPlease enter the file you wish to open:  ");
    	gets(fileName);
    
    	fname=fopen(fileName,"w");
    
    	if (fname==NULL)
    	{
    		printf("\nThe file cannot be opened.");
    		printf("\nPlease check that the file exists.");
    		//exit(1);
    	}
    
    	return;
    }
    I only wanted to see if the option 1 worked and when I get it to work, I'll move to option 2. With option 1, it's supposed to do 3 things:
    1.) Request the name of the file
    2.) Request the text.
    3.) Write the text to the file.

    Then, after completing n option, except for #8, supposed to return the menu to the screen. The main() will consist mainly of function calls. Thanks for any help! Tommy

  2. #2
    I like code Rouss's Avatar
    Join Date
    Apr 2004
    Posts
    131
    How far does the program get before it crashes?

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    You're not returning a result from your create function

    You're better off saying
    editor = create();

    And in the create function, finishing off with
    return fname;

    > gets(fileName);
    FAQ
    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.

  4. #4
    Registered User
    Join Date
    Mar 2004
    Posts
    78
    When I press the 1 and hit enter, it crashes immediately. I can press 2 and get the "This is a test" and it goes back to the options screen.

  5. #5
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Code:
    switch(ch) {
    		case '1':
    		  create(editor);
    		  break;
    		case '2':
    		  printf("\nJust is just a test");
    		  break;
    		default:
    		  printf("\nInvalid input");	
    		}
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  6. #6
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    since you are reading the input is as a character:
    Code:
    case 1:
    should be
    Code:
    case '1':
    Edit: Dang XSquared was just a little faster then me

  7. #7
    Registered User
    Join Date
    Mar 2004
    Posts
    78
    Quote Originally Posted by Salem
    You're not returning a result from your create function
    It doesn't need to return a value. All I am doing is requesting the file, requesting the text (whatever this means?) and then I am writing some text to the file. That's it...so it shouldnt return a value.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    But your function only opens the file
    If main or another function is going to do everything else you've said, then it needs to return a value

    Either way, passing an uninitialised pointer to the function, such that it will remain uninitialised after the call is a waste of time.
    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.

  9. #9
    I like code Rouss's Avatar
    Join Date
    Apr 2004
    Posts
    131
    Actually, you do need to return you the file pointer from create().
    I had a similar problem with a program I am working on. I send my FILE * to a function to open it. It opened the file, but I got segmentation errors when I tried to use any file funtions with the pointer... The only way I could get it to work was to return the file pointer from the function that opens the file.
    So you need,
    Code:
    editor = create(editor);
    
    FILE *create(FILE *fname)
    {
    ...
    ...
    return fname
    }
    Like Salem said.

  10. #10
    Registered User
    Join Date
    Mar 2004
    Posts
    78
    ok, so then I removed the void from the create function. Then I put this :
    [code]
    editor = create(editor);

    FILE *create(FILE *fname)
    {
    ...
    ...
    return fname
    }
    [\code]
    in my create function. I am still getting 3 errors.. This is really confusing me.

  11. #11
    I like code Rouss's Avatar
    Join Date
    Apr 2004
    Posts
    131
    What are the errors?

  12. #12
    Registered User
    Join Date
    Mar 2004
    Posts
    78
    heres the code:
    [code]
    #include <stdio.h>
    #include <conio.h>
    #include <string.h>

    create(FILE*); /*function prototype*/

    int main()
    {
    int ch;
    FILE*editor;


    do
    {

    printf("\nPlease choose an option below by typing a number between 1 and 8.\n\n");

    printf("1\tCreate text file\n\n");
    printf("2\tCount Characters,words and sentences\n\n");
    printf("3\tConvert to lowercase\n\n");
    printf("4\tConvert to uppercase\n\n");
    printf("5\tEncrypt text\n\n");
    printf("6\tDecrypt text\n\n");
    printf("7\tDisplay text file\n\n");
    printf("8\tExit program\n\n");
    scanf("%d", &ch);



    switch(ch) {
    case 1:
    create(editor);
    break;
    case 2:
    printf("\nJust is just a test");
    break;
    }
    }while(ch!=0);

    return 0;

    }
    create(FILE*fname)
    {
    editor=create(editor);
    FILE*create(FILE*fname);

    char fileName[13];

    printf("\nPlease enter the file you wish to open: ");
    gets(fileName);

    fname=fopen(fileName,"w");

    if (fname==NULL)
    {
    printf("\nThe file cannot be opened.");
    printf("\nPlease check that the file exists.");
    exit(1);
    }

    return fname;
    }
    [\code]
    Here are the errors:
    1.)'editor' undeclared identifier
    2.)overloaded function differs only by return type 'int__cdecl create (struct_ioubf *)'
    3.)see declaration of 'create'
    4.)'create':'struct_ioubf *(struct_ioubf *)' differs in levels of indirection from 'int (struct_ioubf *)'
    5.)'exit' undeclared identifier

  13. #13
    I like code Rouss's Avatar
    Join Date
    Apr 2004
    Posts
    131
    I'm sorry, I guess the way I put my code close together was bad.

    In the switch statement. You want to put editor = create(editor);

    Code:
    switch(ch)
    {
    case '1':
        editor = create(editor);
        break;
    case '2':
        printf("\nJust is just a test");
        break;
    }
    This should be your declaration for create()
    Code:
    FILE *create(FILE *fname)
    {
        char fileName[13];
    
        printf("\nPlease enter the file you wish to open: ");
        gets(fileName);
    
        fname=fopen(fileName,"w");
    
        if (fname==NULL)
        {
            printf("\nThe file cannot be opened.");
            printf("\nPlease check that the file exists.");
            exit(1);
        } 
    
        return fname;
    }
    Also you should consider using fgets() instead of gets().
    But try this code.

  14. #14
    Registered User
    Join Date
    Mar 2004
    Posts
    78
    ok, I now have this:
    [code]
    #include <stdio.h>
    #include <conio.h>
    #include <string.h>

    FILE* create(FILE*); /*function prototype*/

    int main()
    {
    int ch;
    FILE*editor;


    do
    {

    printf("\nPlease choose an option below by typing a number between 1 and 8.\n\n");

    printf("1\tCreate text file\n\n");
    printf("2\tCount Characters,words and sentences\n\n");
    printf("3\tConvert to lowercase\n\n");
    printf("4\tConvert to uppercase\n\n");
    printf("5\tEncrypt text\n\n");
    printf("6\tDecrypt text\n\n");
    printf("7\tDisplay text file\n\n");
    printf("8\tExit program\n\n");
    scanf("%d", &ch);



    switch(ch) {
    case 1:
    editor=create(editor);
    break;
    case 2:
    printf("\nJust is just a test");
    break;
    }
    }while(ch!=0);

    return 0;

    }
    FILE*create(FILE*fname)
    {

    char fileName[13];

    printf("\nPlease enter the file you wish to open: ");
    gets(fileName);

    fname=fopen(fileName,"w");

    if (fname==NULL)
    {
    printf("\nThe file cannot be opened.");
    printf("\nPlease check that the file exists.");
    //exit(1);
    }

    return fname;
    }
    [\code]
    I have to have the exit commented out because if I dont, I get an error message that says: 'exit': undeclared identifier.

    When I compile with the exit commented out, I get No errors. However, when I press 1 and enter, the program crashes and and a screen pops up and says:
    Debug Assertion Failed!
    File: fopen.c
    Line: 54

    Expression: *file!=_T('\0')

  15. #15
    I like code Rouss's Avatar
    Join Date
    Apr 2004
    Posts
    131
    Ok, give me a little time. I'll update this post in a few minutes.

    / Updated / Is this C++? My compiler doesn't support conio.h

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    FILE* create(FILE*); /*function prototype*/
    
    int main()
    {
            int ch;
            FILE*editor;
    
    
            do
            {
    
            printf("\nPlease choose an option below by typing a number between 1 and 8.\n\n");
    
            printf("1\tCreate text file\n\n");
            printf("2\tCount Characters,words and sentences\n\n");
            printf("3\tConvert to lowercase\n\n");
            printf("4\tConvert to uppercase\n\n");
            printf("5\tEncrypt text\n\n");
            printf("6\tDecrypt text\n\n");
            printf("7\tDisplay text file\n\n");
            printf("8\tExit program\n\n");
            scanf("%d", &ch);
            switch(ch) {
            case 1:
            editor=create(editor);
            break;
            case 2:
            printf("\nJust is just a test");
            break;
            }
            }while(ch!=0);
    
            return 0;
    
    }
    
    FILE *create(FILE *fname)
    {
    
            char fileName[13];
    
            printf("\nPlease enter the file you wish to open: ");
            scanf("%13s", fileName);
    
            fname=fopen(fileName,"w");
    
            if (fname==NULL)
            {
                    printf("\nThe file cannot be opened.");
                    printf("\nPlease check that the file exists.");
                    exit(1);
            }
    
            return fname;
    }
    Ok, I tested this in my compiler and it worked. See if it will work in yours.

    Rouss
    Last edited by Rouss; 04-25-2004 at 02:57 PM.

Popular pages Recent additions subscribe to a feed