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

  1. #31
    Registered User
    Join Date
    Mar 2004
    Posts
    78
    Thanks Rouss for your help. I have classes in the am and will be home later in the afternoon. This project is becoming harder than I thought it would be Tommy

  2. #32
    Registered User
    Join Date
    Mar 2004
    Posts
    78
    Rouss,
    The file is created and it should contain no more than 81 characters or 1 paragraph. Just thought you would know.

  3. #33
    I like code Rouss's Avatar
    Join Date
    Apr 2004
    Posts
    131
    Ok, I might have something for you. Give me a few minutes.

    // After a few minutes.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #define FLUSH while(getchar() != '\n')
    
    FILE *create(FILE*); /*function prototype*/
    
    int main()
    {
    	int ch;
    	FILE *editor;
    
    	ch = 0;
    	while(ch != 8)
    	{
    		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);
    		FLUSH;
    
    		switch(ch) {
    		case 1:
    			editor=create(editor);
    			break;
    		case 2:
    			printf("\nJust is just a test");
    			break; 
    		}
    	}
    	fclose(editor);
    	return 0;
    
    }
    
    FILE *create(FILE *fname)
    {
    	char fileName[13];
    	char text[82];
    
    	printf("Enter the name of file to create: ");
    	fgets(fileName, sizeof(fileName), stdin);
    	fileName[strlen(fileName)-1] = '\0';
    
    	fname = fopen(fileName, "w");
    	if(fname == NULL)
    	{
    		printf("\nThe file cannot be opened.");
    		printf("\nPlease check that the file exists.");
    		exit(1);
    	}
    
    	printf("Enter the text to write to the file: ");
    	fgets(text, sizeof(text), stdin);
    	fprintf(fname, "%s", text);
    
    	return fname;
    }
    Compile that and run it. It works for me. See if it is what you need.
    Last edited by Rouss; 04-26-2004 at 07:35 PM.

  4. #34
    Registered User
    Join Date
    Mar 2004
    Posts
    78
    Ok...that works great except that when I write text to the file and when I am done and I hit enter, it goes back to the menu plus I have "Enter name of the file to create:" This sentence shouldnt be here. Only the choice with the 8 options. Also, when I chose option 8, it crashes. For option 8, not only will it need to exit the program, I will need to: Dispaly the number of times each choice was selected. Like how the heck do I do that. Use a counter? And once I display this, I will then exit the program.

  5. #35
    Registered User
    Join Date
    Mar 2004
    Posts
    78
    what does this mean:

    #define FLUSH while(getchar() != '\n')

  6. #36
    I like code Rouss's Avatar
    Join Date
    Apr 2004
    Posts
    131
    Sorry, had to make a trip to burger king...
    #define FLUSH while(getchar() != '\n')
    When you type FLUSH; in as a line of code, it will be the same as typing while(getchar() != '\n');
    What that does is basically remove trash from the stdin stream.

    And it doesn't crash when you choose choice 8, I made it exit on choice 8, just so I could stop the program with a choice.

    Ok, to fix the error (what happened was you entered more than 81 chars) add the following lines after
    fgets(text, sizeof(text), stdin);
    Code:
    text[strlen(text)-1] = '\0';
    FLUSH;

  7. #37
    I like code Rouss's Avatar
    Join Date
    Apr 2004
    Posts
    131
    To keep track of how many times each function was called, use an int array.

    Code:
    int calls[7] = {0};
    That will create an int array named calls with 7 integer values set to 0.

    Then after the scanf("%d", &ch"); FLUSH; part, (before the switch) add this line.
    Code:
     if(ch > 0 && ch < 8) calls[ch-1]++;
    That will increment the number of times each choice was selected. But remember, choice one will be call[0], choice two will be call[1] and so on.

    Then to print this you would just use a for loop with a print statement.

  8. #38
    Registered User
    Join Date
    Mar 2004
    Posts
    78
    As soon as I compile and I get the menu, I chose option 8 and immediately I get a "final.exe has encountered a problem and needs to close. We are sorry for any inconvenience. " This is what I mean by crashing. I dont even write a file. I just type option 8 and hit enter and this happens.

  9. #39
    I like code Rouss's Avatar
    Join Date
    Apr 2004
    Posts
    131
    Change the while loop in the main. Change it from while(ch != 8) to while(ch != -1)
    or something, just for now. So we can exit the program.

  10. #40
    Registered User
    Join Date
    Mar 2004
    Posts
    78
    ok...that'll work. I removed the FLUSH from this statement because once I created the file and named it and I was done writing to it, I had to hit enter twice to get back to the main menu. With this FLUSH removed, I only have to hit enter once. Thanks for your help.

    printf("Enter the text to write to the file: ");
    fgets(text, sizeof(text), stdin);
    text[strlen(text)-1] = '\0';
    FLUSH;

    The FLUSH just removes garbage from the buffer, is this correct?


    For option 8 and after the count is displayed, do I use a return 0 to exit the program. One other question since I dont want to keep you, how would you encrypt a text. Would the easiest way for a beginner like me just add or subtract a certain number from/to each character? Once I encrypt, the next option is to decrypt, which would be the opposite of encrypt.

  11. #41
    Registered User
    Join Date
    Mar 2004
    Posts
    78
    One other thing I forgot to mention. Each option except for option #8, I need to request the file. So for Option #2, after I request the file, it says display the input text. Since I am displaying the input text from a file the suer has specified, I am opening and using "r" for read to read the file. For my function prototype and the rest of my function prototypes, I will be using:

    FILE *create(FILE*);

    With the name "create" changing to reflect the name of my function. My function call:

    editor=create(editor);

    will also change to reflect the new name of the function prototype.

    Last but least, the function itself will be different:

    FILE *create(FILE *fname)

    with create changing to reflect the name of the function. The fname could stay the same since it's local to each function.

    Does this sound ok to you? I hope I am not confusing you. I know it's getting late.

  12. #42
    I like code Rouss's Avatar
    Join Date
    Apr 2004
    Posts
    131
    Ok, you won't need the FLUSH you removed unless the person enters more than 81 characters. Or the rest will read over into the menu next time it comes up... That's the problems you were having a moment ago.

    You can use a return 0; or an exit(100) to exit the program.
    For encrypting. I would say adding or subtracting will be fine. unless you want to keep them all letters.. That will take a little more than just adding or subtracting.. But if the encrypted characters can be any character. I would say that is the way to go.

    And I'll be around for about another half an hour to an hour if you have any more questions. And I'll check the board tomorrow if I missed any.

    Rouss

  13. #43
    I like code Rouss's Avatar
    Join Date
    Apr 2004
    Posts
    131
    Ok, we replied at the same time..
    Stay tuned for an answer...

    /** Updated **\

    Ok, since you are going to need to use the file in every function. I think you need to change a few things. (sorry to change things up, but I've just been going piece by piece).

    You will want to close the file pointer in create();
    And not return the pointer. And you don't even need to send the pointer to the file. But you need to send a filename array.
    declare char fileName[13] in your main
    so change the prototype and definition etc to
    void create(char fileName[])

    The reason for this is, you have opened it in w mode, and you will need to read from it, it will be easier to just pass the filename to the functions and reopen the file as needed in "r" or "w" mode.
    And you don't need to return the fileName[] because the changes will be saved automatically.

    So from looking at your should make all of your functions similar to this
    void function_name(char fileName[]);

    And just open the file in the function, do whatever you need to, then close it.

    This would be the simpliest way. Or do you have to do it the way you stated?
    Last edited by Rouss; 04-26-2004 at 09:19 PM.

  14. #44
    Registered User
    Join Date
    Mar 2004
    Posts
    78
    I was working on my other class assignments. I just wanted to make one thing clear. My teacher said that I wont neccesarily be using the same file every time. I will be requesting A file, but not the same one. It can be, but it's whatever file the user specifies. Does this make sense. So If I chose the file that I created from Option 1, then that's fine. But if I wanted to say encrypt a file I have on my desktop, that this needs to work just as well. Hope this doesn't mess things up for you. I will be going to bed here soon too. We can catch up tomorrow if you like. It's almost 0100 here. Will try to post again soon, but I have clinicals from 3pm-10:30pm and I also have a doctor's appt wednesday morning and then clinicals again wednesday afternoon. It wont be long before I post again

  15. #45
    Registered User
    Join Date
    Mar 2004
    Posts
    78
    To make thing easier, I will write what I have to do for all 8 options. Just so you know and maybe you'll be able to understand a little better.

    Option 1:
    1.)Reuqest the name of the file.
    2.)Request the text (Write text to the file).
    3.)Write the text to the file.

    Option 2
    1.)Request file name.
    2.)Display the input text.
    3.)Count the number of characters in the text file, not including spaces or periods. Display the result on the screen.
    4.)Count the number of words in the file. Words are seperated by one or more spaces. Display the result on the screen.
    5.)Count the number of sentences in the file. Assume a sentence ends with a period. Display the result on the screen.

    Option 3
    1.)Request the file name.
    2.)Convert the text to lowercase.
    3.)Write the lowercase text to a new line.
    4.)Allow the user to name the output file.

    Option 4
    1.)Request the file name.
    2.)Convert the text to uppercase.
    3.)Write the uppercase text to a new file.
    4.)Allow the user to name the output file.

    Option 5/6
    1.)Request the file name.
    2.)Encrypt or decrypt the text using any method you choose. For example, you can create your own encrypting scheme by adding/subtracting a certain number to/from each character.
    3.)Write the text to a new file.
    4.)Allow the suer to name the output file.

    Option 7
    1.)Request the name of the file.
    2.)Display the text in the file.

    Option 8
    1.)Display the number of times each choice was selected.
    2.)Exit the program.

    Program requirements:
    1.)Assume text is no more than 80 characters and 1 paragraph.
    2.)Use local variables.
    3.)Divide the program into appropriate functions (Main() should consist primarily of function calls.
    4.)Do not use break (except for switch), continue, or goto (Except for gotoxy).
    5.)Present attractive screens.
    6.)Use meaniningful variable names.
    7.)After completing an option, except for 8, return the menu to the screen.

Popular pages Recent additions subscribe to a feed