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

How do I get this code to read to the end of every line until the end of the ent

ID: 651341 • Letter: H

Question

How do I get this code to read to the end of every line until the end of the entire file?

I need to have it read each word individually until the end of the line, then I will be using a function to insert that data into a queue, at which point I will need the read function to continue reading the file line by line.

Here is what I have so far:

char readInput()
{
FILE *fp1;
char oneword[100];
char c;

   fp1 = fopen("INPUT2014.TXT","r");

   do {
      c = fscanf(fp1,"%s",oneword); /* got one word from the file */
      printf("%s ",oneword);       /* display it on the monitor */
   } while (c != EOF);              /* repeat until EOF           */

   fclose(fp1);
}

Language is C

Explanation / Answer

char readInput()
{
FILE *fp1;
char oneline[100];
char c;

   fp1 = fopen("INPUT2014.TXT","r");

   do {
     c = fscanf(oneline, "%[^ ]s", cLine) /* got one line from the file */

      printf("%s ",oneword);       /* display it on the monitor */
   } while (c != EOF);              /* repeat until EOF           */

   fclose(fp1);
}

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