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; Stop having people do your work for you. Do it yourself. When you get stuck, post your own code and ...

  1. #46
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Stop having people do your work for you. Do it yourself. When you get stuck, post your own code and any errors you're getting and... and while you're at it, read this link and while you're at it, this link too. If you put as much effort into doing your project as you do in trying to get people to do it for you, it'd be done by now.

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

  2. #47
    Registered User
    Join Date
    Mar 2004
    Posts
    78
    quzah,
    I am not asking him to do my complete homework. I only wrote down what I am supposed to do so there wont be any miscommunication when I ask him a question. So for your information, I have already read the rules prior to ever posting on this site. Did I ask Rouss to do my homework? No I didnt. He is generous enough to take the time to help me. Just because I wrote the entire assignment doesnt mean I was asking him to complete it. I wrote it so if I have a question, he can refer back to it.

    Rouss,

    Back to where I was. I will post later when I get back to working on my project and if I have a question. I had a final today and just started working on this. Tommy

  3. #48
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    It's totally unneccessary to pass "fname" to create(). You don't use it before overwriting it through fopen(). Also, when defining "editor" before the while loop, set it to NULL. If you break the loop without typing 1 you will call fclose() with an uninitialized argument = bad.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  4. #49
    Registered User
    Join Date
    Mar 2004
    Posts
    78
    For my function prototype, I have FILE*character(FILE*)

    The switch statement for case 2:
    text=character(text);
    break;

    Function:
    Code:
    FILE*character(FILE*fname)
    {
    char text[81];
    char fileName[13];
    
    printf("\nPlease enter the name of the file you wish to open:  ");
    
    fgets(fileName, sizeof(fileName), stdin);
    fileName[strlen(fileName)-1]='\0';
    
    fname=fopen(fileName, "r");
    if (fname==NULL)
    {
    printf("\nThe file cannot be opened.");
    printf("\nPlease check that the file exists.");
    exit(1);
    }
    while (fgets(text,81,fname) !=NULL)
             printf("%s", text);
    return fname;
    }
    What I am trying to do is open a file and display the text. But I haven't been able to do this yet. Once I can display the file, I will be able to count the characters, words, etc...

    Magos,
    Do I just take fname out and not even use it?

  5. #50
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You don't need to return anything from that function, since there really is no point in doing so. You say you haven't been able to do what you're trying. What exactly aren't you able to do?

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

  6. #51
    Registered User
    Join Date
    Mar 2004
    Posts
    78
    Quote Originally Posted by quzah
    You say you haven't been able to do what you're trying. What exactly aren't you able to do?

    Quzah.
    I am trying to display the text of the file by using:
    Code:
    while (fgets(text,81,fname) !=NULL)
             printf("%s", text);
    Since I'm not returning anything from the function, can I just use FILE*character instead of FILE*character(FILE*fname)?

  7. #52
    I like code Rouss's Avatar
    Join Date
    Apr 2004
    Posts
    131
    Since you don't need to return anything from the file, you should make it
    Code:
    void character(FILE *fname)
    Did you see in one of my earlier post though. I think you may want to pass fileName[] to each function, and just create a new file pointer in each function. This way, you send fileName[] to the create() function. Since it is an array (and not sent as const) the changes made to it should also be in the main function. Then all you have to do is pass fileName[] to each function, open the file, then do the necessary operations. The only time the filename would change would be when the user chooses choice 1 again.

    Do you get what I'm saying?

  8. #53
    Registered User
    Join Date
    Mar 2004
    Posts
    78
    I kind of understand what you are saying. But I didn't think it would be a good idea to do this because this program is NOT using the same file each and every time. For example, option2, the one I am working on now. It asks the user to open a file, any file. fileName is whatever the user decided to name and it's whatever file the user has on his/her computer. The fileName can change each and every option, ofcourse except #8. Am I making sense or am I not understanding something?

    Or can this be used for files that aren't used for each option?

  9. #54
    Registered User
    Join Date
    Mar 2004
    Posts
    78
    Function prototype is void character(FILE*);

    When option 2 is selected:
    Code:
    void character(FILE *fname)
    {
    char text[81];
    char fileName[13];
    
    printf("\nPlease enter the name of the file you wish to open:  ");
    
    fgets(fileName, sizeof(fileName), stdin);
    fileName[strlen(fileName)-1]='\0';
    
    fname=fopen(fileName, "r");
    if (fname==NULL)
    {
    printf("\nThe file cannot be opened.");
    printf("\nPlease check that the file exists.");
    return;
    }
    
    while (fgets(text,81,fname) !=NULL)
             printf("%s", text);
    }
    Is it ok to use the return statement instead of the exit(1) right after the 2 printf statements when (fname==NULL)?

    I am doing this because when I try to open a file that doesn't exist, the program ends. The only way the program is supposed to end is when the user types 8. I haven't gotten that far yet.

  10. #55
    Registered User
    Join Date
    Mar 2004
    Posts
    78
    Why can't I print the file that I name onto the screen? Is the printf statement wrong? It's supposed to read the contents of the file, up to the 81 characters specified in the text array. I am lost as to why it's not printing. Thanks.............

  11. #56
    I like code Rouss's Avatar
    Join Date
    Apr 2004
    Posts
    131
    I just made my own text file, and made a program with only the file pointer in the main, and the function you posted. And it worked. Maybe you should open the file you are trying to test to see if anything is there? Also make sure you've include the needed libraries.

  12. #57
    Registered User
    Join Date
    Mar 2004
    Posts
    78
    Well what I did was create the file using Option1. then I chose Option2 and tried to open the file created with Option1 and for some reason it's not opening. I'll check again.

  13. #58
    I like code Rouss's Avatar
    Join Date
    Apr 2004
    Posts
    131
    I just went back and looked at some of your code, I don't know if it's current or not, but make sure you fclose(fname); in create(), that could be your problem. I know if you don't close a file most of the time the changes won't be saved. See if that works.

  14. #59
    Registered User
    Join Date
    Mar 2004
    Posts
    78
    Ok...I created a file using option1 and I named it hello.txt. I then went to Option2 and typed hello.txt and it opened the file and displayed it on screen. This is what it's supposed to do. Closing the file in create must of been the culprit.

    However, I have a small problem. If I start Microsoft Word and create a text file, we'll call it hello1. I'll save it in the same folder as my program code. When I go to option2 and type hello1.doc, I get about 4 weird characters that I can't produce using the keyboard and then it goes back to the options menu. Any clue?

  15. #60
    I like code Rouss's Avatar
    Join Date
    Apr 2004
    Posts
    131
    That would be the evils of Microsoft in the file... Kidding. When you use text processors like Word or WordPerfect, they save the formatting in the files, like all the tabs, fonts, etc... That's the 4 weird characters. Use notepad to make text file, it doesn't have all the fancy formatting. Also if you open hello1.doc in Notepad, you should see the same weird characters.

Page 4 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