Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

How can I make my program to read from file and export the result in another fil

ID: 666027 • Letter: H

Question

How can I make my program to read from file and export the result in another file?

#include <stdio.h>
#include <string.h>
#include <time.h>
#include <ctype.h>


#define STR_LEN 80
#define MAX_SEN 10

int main ()

{

char *article[] = {"a", "any", "one", "some", "the"};
char *noun[] = {"boy", "car", "dog", "girl", "town"};
char *verb[] = {"drove", "jumped", "ran", "skipped", "walked"};
char *preposition[] = {"from", "on", "over", "to", "under"};
int num;
int a = 0;
int j = 0;
char sentence[MAX_SEN][STR_LEN];
char (*i)[STR_LEN];

srand (time(NULL));

  

printf ("WORD BANK");
   printf (" ---------- ");

printf (" Articles: ");
for (j = 0; j < 5; j++)
{
printf ("%s ", article[j]);
}
printf (" ");

printf (" Nouns: ");
for (j = 0; j < 5; j++)
{
printf ("%s ", noun[j]);
}
printf (" ");

printf (" Verbs: ");
for (j = 0; j < 5; j++)
{
printf ("%s ", verb[j]);
}
printf (" ");

printf (" Prepositions: ");
for (j = 0; j < 5; j++)
{
printf ("%s ", preposition[j]);
}
printf (" ");

printf ("GENERATED SENTENCES");
   printf (" -------------------- ");

for (i = sentence; i < sentence + MAX_SEN; i++)
{
printf (" %d. ", (++a));

num = rand() % (sizeof (article)/sizeof (article[0]));
strcpy (*i, article[num]);
  
num = rand() % (sizeof (noun)/sizeof (noun[0]));
strcat (strcat (*i, " "), noun[num]);
  
num = rand() % (sizeof (verb)/sizeof (verb[0]));
strcat (strcat (*i, " "), verb[num]);
  
num = rand() % (sizeof(preposition)/sizeof(preposition[0]));
strcat (strcat (*i, " "), preposition[num]);

num = rand() % (sizeof (article)/sizeof (article[0]));
strcat (strcat (*i, " "), article[num]);

num = rand() % (sizeof (noun)/sizeof (noun[0]));
strcat (strcat (*i, " "), noun[num]);

printf ("%s! ", *i);

}

return 0;

}

Explanation / Answer

Answer:

/*****Here is the modified code that reads from file and store in file*********/

#include<iostream>

#include <stdio.h>

#include <string.h>

#include <time.h>

//#include <ctype.h>

#include<conio.h>

#include<stdlib.h>

#define STR_LEN 80

#define MAX_SEN 10

struct wordstruct

{

     char article[5];

     char noun[10];

     char verb[10];

     char preposition[10];

};

int main ()

{

FILE *file1,*file2;

struct wordstruct wstruct[5];

   int num;

    int a = 0;

    int j = 0,k1;

    char sentence[MAX_SEN][STR_LEN];

char i[100];

    srand (time(NULL));

       

file1=fopen("inputfile.txt","r");

if(file1!=NULL)

{

          for (j = 0; j < 5; j++)

    {

          fscanf (file1,"%s ", wstruct[j].article);

    }

   

    for (j = 0; j < 5; j++)

    {

          fscanf (file1,"%s ", wstruct[j].noun);

    }

    for (j = 0; j < 5; j++)

    {

          fscanf (file1,"%s ", wstruct[j].verb);

    }

    for (j = 0; j < 5; j++)

    {

          fscanf (file1,"%s ", wstruct[j].preposition);

    }

}

fclose(file1);

printf ("WORD BANK");

    printf (" ---------- ");

    printf ("        Articles: ");

    for (j = 0; j < 5; j++)

    {

          printf ("%s ", wstruct[j].article);

    }

    printf (" ");

    printf ("        Nouns: ");

    for (j = 0; j < 5; j++)

    {

          printf ("%s ", wstruct[j].noun);

    }

    printf (" ");

   printf ("        Verbs: ");

    for (j = 0; j < 5; j++)

    {

          printf ("%s ", wstruct[j].verb);

    }

    printf (" ");

    printf ("        Prepositions: ");

    for (j = 0; j < 5; j++)

    {

          printf ("%s ", wstruct[j].preposition);

    }

    printf (" ");

    printf (" ");

     file2=fopen("outputfile12.txt","w");

    printf ("GENERATED SENTENCES");

    printf (" -------------------- ");

    for (k1 = 0; k1 < 10; k1++)

    {

        printf ("        %d. ", (++a));

        num = rand() % (5/sizeof (wstruct[k1].article));

          //cout<<num;

        strcpy (i, wstruct[num].article);

       

       num = rand() % (sizeof(wstruct[k1].noun)/sizeof (wstruct[0].noun));

        strcat (strcat (i, " "), wstruct[num].noun);

        

        num = rand() % (sizeof(wstruct[k1].verb)/sizeof (wstruct[0].verb));

        strcat (strcat (i, " "),wstruct[num]. verb);

       

        num = rand() % (sizeof(wstruct[k1].preposition)/sizeof(wstruct[0].preposition));

        strcat (strcat (i, " "), wstruct[num].preposition);

        num = rand() % (5/sizeof (wstruct[k1].article));

          strcat (i, " ");

        strcat (i, wstruct[num].article);

        num = rand() % (sizeof(wstruct[k1].noun)/sizeof (wstruct[0].noun));

        strcat (strcat (i, " "), wstruct[num].noun);

        fprintf (file2,"%s! ", i);

    }

     fclose(file2);

getch();

return 0;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote