Hi i have been working on this C program assignment for awhile now and the only part of this assignment i can't get to process right is for it to get the next line from the inputed files to run in my loops to print to a new created file. I am new to programming
The program is setup to read two .txt files. "requests.txt" and "toys.txt" line by line and process them. Then write the processed strings to new file "gifts.txt"
If the first line "string" in request has one word it will attach the first line from toys.txt to it Then write it to new file "gift". If request has two words it prints the sting as is to gift. If just a blank line is read from request it goes to the next line.
...........................................................................................................
requests.txt example
Brian
John Airplane
Jack
Paul Money
.........................................................................................................
gifts.txt example
Car
Laser
Computer
Flashlight
..........................................................................................................
CODE
/*My program*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define CHAR 1000
int main (void)
{
char inarray1[CHAR];
char inarray2[CHAR];
char *x;
FILE *request,*toy,*gift;
if ((gift = fopen("gifts.txt","w")) == NULL)
{exit(1);}
if ((request = fopen("requests.txt","r")) == NULL)
{exit(1);}
while ( fgets(inarray1,CHAR,request) != NULL)
fclose (request);
if ((toy = fopen("toys.txt","r"))== NULL)
{exit(1);}
while (fgets(inarray2,CHAR,toy) != NULL)
fclose (toy);
x = inarray1;
while ((*x == ' ') || (*x == '\t')) x++;
{
if ((*x == '\n') || (*x == '\0'))
{
fgets(inarray1,CHAR,request);
}
}
while ((*x != ' ' && *x != '\t') && (*x != '\n' && *x != '\0'))x++;
while ((*x == ' ') || (*x == '\t'))x++;
{
if ((*x != '\n') && (*x != '\0'))
{
fprintf (gift,"%s",inarray1);
fgets (inarray1,CHAR,request);
}
else
{
*x = '\0';
fprintf (gift,"%s\t",inarray1);
fprintf (gift,"%s",inarray2);
fgets (inarray1,CHAR,request);
fgets (inarray2,CHAR,toy);
}
}
fclose (gift);
return 0;
}
.............................................................................................................
When i process using my program i only get the first line output to the newly created file gifts.txt and i am not sure what im doing wrong or not doing to make it do the rest of the files.
My output would just read
Brian Car
Any insight i would really appreciate!
*Mod Edit: added code tags.