Thread: Problem making save function?

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    28

    Problem making save function?

    Hello everyone. I am a little stuck on my project that I am working on. I am almost done, but this save file is the problem. I have to give the user an option to save what their results of the program to the save file, which is what I am trying to do. It's just not coming out? Here is my code (which is only of the save method. In my program, the prototype and call from the main function is also there):

    Code:
    void save_file()
    {
            char filename;
            char option[5];
    
            printf("Would you like to save a file of this data?");
            printf("Enter Yes or No: ");
            scanf("%c", option);
    
            if (option == Yes)
            {
                    FILE *outfile = fopen(filename, "w");
                    if(outfile == NULL)
                    {
                            printf( "The File was not successfully open.\n ");
                            return -1;
                    }
    
                    fclose(outfile);
            }
    
            else
            {
                    printf("No save file will be written\n");
            }
    }
    These are the errors I am getting:
    Code:
    ProjectTest2.c: In function âsave_fileâ:
    ProjectTest2.c:87: error: âYesâ undeclared (first use in this function)
    ProjectTest2.c:87: error: (Each undeclared identifier is reported only once
    ProjectTest2.c:87: error: for each function it appears in.)
    ProjectTest2.c:89: warning: passing argument 1 of âfopenâ makes pointer from integer without a cast
    /usr/include/stdio.h:271: note: expected âconst char * __restrict__â but argument is of type âcharâ
    ProjectTest2.c:93: warning: âreturnâ with a value, in function returning void
    I have never had to write a file before, only had to open/read. So, I hope I am even doing this correctly? I am also unsure of my use of a void (the compiler sure doesn't seem happy about it), but I am unsure of what else to use? Is it even appropriate to be doing a save in a function (the only reason I did it was because my instructor wants the maximum use of functions)? Any guidance at all would be awesome!

    Thank you!

  2. #2
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Your problems are nothing to do with writing to file. It looks like a basic lack of understanding of how strings are represented and manipulated in C.

    A file name cannot fit into a single char, a string requires double quotes around it, and comparing strings requires use of the strcmp function.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  3. #3
    Registered User
    Join Date
    Sep 2011
    Posts
    28
    Ugh, thanks. It's been a few weeks since we've done this, and I thought I was remembering correctly. I'll go back and review my old programs/notes, and will be back if I have more issues. Thanks!

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    You need to either hard code the filename or pass it into the function...
    Also you need to return a success/fail result from this operation so that your other code can govern itself accordingly.
    Also a couple of your variable types are a bit wonky...

    Something like this...
    Code:
    int save_file(char *filename)
    {
            char option;
            FILE *outfile;
    
            printf("Would you like to save a file of this data?  (Y/N) :");
            scanf("%c", option);                 // gets 1 character
    
            if (option != 'Y' && option != 'y')
              return 0;                              // not saved
    
            //open the file
           outfile = fopen(filename, "w");
           if (outfile == NULL)
             return -1;                            // file error     
    
           // write file data here
    
           fclose(outfile);
           return 1;                                // success
    }
    As a general hint... once you get something working... always spend a couple of minutes and see if there isn't a simpler way to accomplish the same goals.

  5. #5
    Registered User
    Join Date
    Sep 2011
    Posts
    28
    Edit: Never mind. Found my mistake.

    Thank you so much for your help!
    Last edited by Chinnie15; 12-13-2011 at 12:41 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Save values in a function
    By dgs012 in forum C Programming
    Replies: 3
    Last Post: 12-08-2010, 05:01 AM
  2. Save function not working
    By rogster001 in forum C++ Programming
    Replies: 3
    Last Post: 11-19-2009, 09:27 AM
  3. Re: Save & SaveAs function
    By icc_81 in forum C++ Programming
    Replies: 1
    Last Post: 01-16-2004, 04:10 PM
  4. how can i save the output of this function to use later?
    By Unregistered in forum C Programming
    Replies: 13
    Last Post: 07-18-2002, 07:27 PM
  5. Replies: 1
    Last Post: 12-26-2001, 03:00 AM