Function is called and I am trying to open a file

This is a discussion on Function is called and I am trying to open a file within the C Programming forums, part of the General Programming Boards category; I am using C...

  1. #16
    Registered User
    Join Date
    Mar 2004
    Posts
    78
    I am using C

  2. #17
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    conio.h is a DOS based io header, found in old DOS compilers. It allows for non-buffered keystroke reading and such.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #18
    Registered User
    Join Date
    Mar 2004
    Posts
    78
    I'm using the Microsoft Visual C++

  4. #19
    /*enjoy*/
    Join Date
    Apr 2004
    Posts
    159
    you have forget to put
    #include "fstream.h"/*library of input output file */

  5. #20
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    a) It's #include <fstream>
    b) That's C++, this is the C forum.
    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. #21
    /*enjoy*/
    Join Date
    Apr 2004
    Posts
    159
    use
    [tags]
    #include "ftream.h" /* if you are using c ++ */[/tags]

  7. #22
    Registered User
    Join Date
    Mar 2004
    Posts
    78
    Rouss,
    Yes, it worked. Thanks. The reason why the exit(1) wasn't working was because I didnt have the #include <stdlib.h> header file. That's my fault.

    Ok...so when I type in 1 and hit enter, I get the "Please enter a file you wish you to open". I type in a file name and when I hit enter, it goes back to the options. I need to request the text and then write the text to the file. We got the first one correct (Request the name of the file). I can just add code right after the } before the return fname, or do I add it within the if (fname==NULL).

  8. #23
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Quote Originally Posted by enjoy
    use
    [tags]
    #include "ftream.h" /* if you are using c ++ */[/tags]
    Quote Originally Posted by tommy69
    I am using C
    Jeez.
    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

  9. #24
    Registered User
    Join Date
    Apr 2004
    Posts
    26
    I just want to add something to tommy's program:

    Code:
     case 8:
            exit(1);
            break;
    It is always good to have exit option in program.

    Just a little help from me.

  10. #25
    Registered User
    Join Date
    Mar 2004
    Posts
    78
    Quote Originally Posted by barim
    I just want to add something to tommy's program:

    Code:
     case 8:
            exit(1);
            break;
    It is always good to have exit option in program.

    Just a little help from me.
    Barim,

    Thanks. I havent gotten that far yet, but I will be including that. I could just include the code now.

    For my program, I need to request the name of the file. I just did that. I then need to request the text. What does this mean? Then, I need to write the text to the file. Am I creating a file named create and then writing text to this newly created file? I am not sure how this is done.

    This program has 8 options. Each option except option 8 Requests the file name. I need to assume text is no more than 80 characters and one paragraph, use only local variables, divide the program into appropriate functions (main() should consist of primarily of function calls), do not use break (except for switch), continue or goto(except for gotoxy), and after completing an option, except for #8, return the menu to the screen.

    This is what I am looking at and what I am supposed to do. But for now, I am struggling with option 1. Thanks again for your help.........................tommy

  11. #26
    I like code Rouss's Avatar
    Join Date
    Apr 2004
    Posts
    131
    Sorry this is so late. I had to run errands. Yeah, just add the code after the } and before the return fname.

    You don't want to put it in the if(fname == NULL), because if fname is = to NULL, then the file didn't open.

    // Update

    Okay so you see how to get input from the user. I used scanf().

    I assume you mean your line won't be longer than 80 characters?
    How big is your paragraph? 4 lines?

    You would need a string (char array), such as char line[81].
    Code:
    char line[81];
    
    printf("Enter Line: ");
    sscanf("%s", line);
    /* To print to the file use fprintf */
    fprintf(fname, "%s\n", line);
    If you know how many lines you will have you could put it in a loop. Otherwise I don't know how you would know when to stop. Unless you used a sentinel-controlled loop.
    Last edited by Rouss; 04-25-2004 at 08:38 PM.

  12. #27
    Registered User
    Join Date
    Mar 2004
    Posts
    78
    Quote Originally Posted by Rouss
    I assume you mean your line won't be longer than 80 characters?
    How big is your paragraph? 4 lines?

    If you know how many lines you will have you could put it in a loop. Otherwise I don't know how you would know when to stop. Unless you used a sentinel-controlled loop.
    Well the program requirements are:

    "Assume text is no more than 80 characters and one paragraph" I am assuming she is meaning the text that I enter and the text that's in the file(s). These program requirements are for 7 options. For example, for option 2, I have to:
    1.) Request the file name.
    2.)Display the input text.
    3.)Count the number of characters in the etxt file, not including spaces or periods. Display the result on the screen.
    4.)Count the number of sentences in the file. Assume a sentence ends with a period.
    5.)Dispaly the result on the screen.

    I don't know how big the paragraph is. I haven't created a file yet. I dont know if I have too, but it looks as if I am creating a file in Option 1. I think I will be using this file for the rest of the options except option 8. I don't want to bore everyone by listing the rest of the options. I will probably do that when I need help.

    Thanks again for your help.

  13. #28
    I like code Rouss's Avatar
    Join Date
    Apr 2004
    Posts
    131
    Ok, I think I understand it somewhat.
    The first thing that needs to be clarified though. Are you creating the file and writing the lines to it, and then reading those same lines from it? Or are you opening a file and reading the lines from it.
    We're going to assume it's the first for now.

    You're going to have to input the text, which is a paragraph of no more than 80 characters.
    So it'll be like I said last time, except no loop.

    Code:
    char text[81];
    printf("Enter Text: ");
    scanf("%80s", text);
    
    /******* SAMPLE INPUT
    Enter Test: This is the first line. This is the second line. So on and so forth.
    *******/
    You enter all the text at once. Does this sound correct?

  14. #29
    Registered User
    Join Date
    Mar 2004
    Posts
    78
    I am assuming that I am creating and writing to a file. I will know for sure Monday afternoon and if this is wrong, I will let you know. I will probably let you know for sure so we are on the same track. Thanks for helping me. I entered the code and for the switch statement I also have:
    }while (ch>=1 || ch<=8); because if the user selects any other number, then it wont be valid. Or do you think it would be better to have while ch!=EOF or while ch!='\0'. If it's '\0', I would let the suer know that they can exit the program by typing in a 0.

    I ran the program and got this as output:

    Please choose an option below by typing a number between 1 and 8.

    1 Create text file

    2 Count Characters,words and sentences

    3 Convert to lowercase

    4 Convert to uppercase

    5 Encrypt text

    6 Decrypt text

    7 Display text file

    8 Exit program

    1

    Please enter the file you wish to open: tanks
    Enter Text: I am writing text to this file

    Please choose an option below by typing a number between 1 and 8.

    1 Create text file

    2 Count Characters,words and sentences

    3 Convert to lowercase

    4 Convert to uppercase

    5 Encrypt text

    6 Decrypt text

    7 Display text file

    8 Exit program


    Please enter the file you wish to open: Enter Text:
    Please choose an option below by typing a number between 1 and 8.

    1 Create text file

    2 Count Characters,words and sentences

    3 Convert to lowercase

    4 Convert to uppercase

    5 Encrypt text

    6 Decrypt text

    7 Display text file

    8 Exit program


    Please enter the file you wish to open: Enter Text:
    Please choose an option below by typing a number between 1 and 8.

    1 Create text file

    2 Count Characters,words and sentences

    3 Convert to lowercase

    4 Convert to uppercase

    5 Encrypt text

    6 Decrypt text

    7 Display text file

    8 Exit program


    Please enter the file you wish to open: Enter Text:
    Please choose an option below by typing a number between 1 and 8.

    1 Create text file

    2 Count Characters,words and sentences

    3 Convert to lowercase

    4 Convert to uppercase

    5 Encrypt text

    6 Decrypt text

    7 Display text file

    8 Exit program

    It went into a loop like 4 times. Not sure why. So I want to be able to write to the file. Not sure if this file needs to be saved somewhere or not. If I can or do save it, it would be saved along with the program file.

  15. #30
    I like code Rouss's Avatar
    Join Date
    Apr 2004
    Posts
    131
    You're going to run into a problem if you choose choice 1 more than once. Because you will open another file without ever closing the first one.
    In your main function set editor to NULL, then in the switch statement add the following if statement before editor = create(editor).
    Code:
    if(editor != NULL)
        fclose(editor);
    Also, I am sorry... I am dumb... I don't use scanf much anymore and I just remembered, if you read a string, it won't read the spaces... So it'll only read the first word. You should probably use fgets()
    Code:
    fgets(text, sizeof(text), stdin);
    text[strlen(text)-1] = '\0';    /* Gets rid of the newline at the end of the string */
    Whenever I ran this in my compiler it would print all distorted... I would play around with it more, but it's late and I have early class tomorrow.
    But I will work on it with you tomorrow.

    Rouss

Page 2 of 6 FirstFirst 123456 LastLast
Popular pages Recent additions subscribe to a feed

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