-1

I'm having quite a bit of trouble figuring out why my code won't work.

The objective of this program is to make a series of random sentences from the given arrays, and to then either output them to the screen or to a text file.

I'm not exactly sure what the problem is, but when I go to input a title for the file, I get an unhanded exception error.

In the case where I change the FILE** stream parameter to NULL instead of write in fopen_s, I get a debug assertion error.

I believe the problem lies with the way I've declared my pointer.

#include <.stdio.h>
#include <.conio.h>

int main()
{
    char * article[5] = { "the", "one","some","any","a" };
    char * noun[5] = { "boy","girl","dog","town","car" };
    char * verb[5] = { "drove","jumped","ran","walked","skipped" };
    char * preposition[5] = { "to","from","over","under","on" };
    int x = 0;
    char * output[100] = {0};

    //char output = { "" };

    FILE ** write = "C:\Users\dilli\Downloads\test.txt";

    while (5) {
        printf("Enter one(1) to output to screen, two(2) to output to file:\n");
        scanf_s("%d",&x);
        if(x==1)
            printf_s("%s %s %s %s %s %s.\n", article[rand() % 5], noun[rand() % 5], verb[rand() % 5],
                preposition[rand() % 5], article[rand() % 5], noun[rand() % 5]);
        else if (x == 2)
        {
            printf("Enter name of output file:\n");
            scanf_s("%s",&output,100);
            printf("output:\n%s",output);
            fopen_s(write,output, "w");//This is where we are getting an unhandled exception.
            fprintf("%s %s %s %s %s %s.\n", article[rand() % 5], noun[rand() % 5], verb[rand() % 5],
                preposition[rand() % 5], article[rand() % 5], noun[rand() % 5]);
            fclose(write);
        }
    }
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770

1 Answers1

0

First, you cannot assign a string literal to a FILE** variable. That should not even compile. And you are not even using the string literal for anything anyway, since you are prompting the user for the output filename instead. So just get rid of the string literal.

Second, you are misusing fopen_s() and scanf_s(), that is why your code crashes.

  • When prompting the user for a filename, you are asking scanf_s() to read into an array of pointers instead of into an array of characters. That alone will likely crash, or at least cause undefined behavior when you try to access the contents of the array later on.

  • After that, you are passing an invalid FILE** pointer, and the invalid char[] array, to fopen_s(). It expects you to pass a pointer to a valid FILE* variable, and a null-terminated char string, not an array of char* pointers.

Third, you are not passing the opened FILE* to fprintf() at all.

With that said, try this instead:

#include <stdio.h>
#include <conio.h>

int main()
{
    const char* article[5] = { "the", "one","some","any","a" };
    const char* noun[5] = { "boy","girl","dog","town","car" };
    const char* verb[5] = { "drove","jumped","ran","walked","skipped" };
    const char* preposition[5] = { "to","from","over","under","on" };
    char fileName[260] = {0};
    FILE *write = NULL;
    int i, x;

    for (i = 0; i < 5; ++i) {
        printf("Enter one(1) to output to screen, two(2) to output to file:\n");
        x = 0;
        scanf_s("%d", &x);
        if (x == 1) {
            printf_s("%s %s %s %s %s %s.\n", article[rand() % 5], noun[rand() % 5], verb[rand() % 5], preposition[rand() % 5], article[rand() % 5], noun[rand() % 5]);
        }
        else if (x == 2) {
            printf("Enter name of output file:\n");
            scanf_s("%s", fileName, 260);
            printf("output:\n%s", fileName);
            if (fopen_s(&write, fileName, "w") == 0) {
                fprintf(write, "%s %s %s %s %s %s.\n", article[rand() % 5], noun[rand() % 5], verb[rand() % 5], preposition[rand() % 5], article[rand() % 5], noun[rand() % 5]);
                fclose(write);
            }
        }
    }

    return 0;
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • thank you. I'm curious about several changes though. Why did you re-declare all my variables as const chars? Also where does this write to, since we didn't specify where? Instantiating char filename to 0 sets it to Null correct? Also why are we doing a boolean operation on fopen_s? doesnt that just open the file to be written? thank you so much. – Urist_mcDwarf Oct 17 '18 at 02:34
  • @Urist_mcDwarf "*Why did you re-declare all my variables as const chars?*" - string literals are const, and you are storing pointers to string literals. "*where does this write to, since we didn't specify where?*" - you read a filename from the user, then open that file and write to it. "*Instantiating char filename to 0 sets it to Null correct?"* - it explicitly sets the first char to 0, and implicitly value-initializes the remaining chars, setting them to 0 too. "*why are we doing a boolean operation on fopen_s?*" - it returns an error code, where 0 is success and non-zero is error. – Remy Lebeau Oct 17 '18 at 03:18