Search code examples
cprintinglinefopenfgets

Read print lines from any data function


Second, of my assignment, I am new to C program user, but way too behind it. I have to solve it that is due today. I thought in the main (it is really complicated for me to understand the function with file) is supposed taking fscan to read the file and to output with new lines from the file. The file is also included for example too I am using. I know 5 points seem awkward a lot but I am new to C program and have never done with the function part of the C program. I wanted to output with i to count each line to print. I also thought to include the comments help a little bit further info. For the function, I don't know what to write the code for fooen and fgets for the first time.

For example:

Example input/output:
   ./a.out testfile

   1: Bob
   2: Tiffany
   3: Steve
   4: Jim
   5: Lucy
   ...
/* 5 points */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAXLEN 1000
#define MAX_LINE_LEN 4096
/**
 * Complete the function below that takes an array of strings and a integer
 * number representing the number of strings in the array and prints them out to
 * the screen with a preceding line number (starting with line 1.)
 */
void
printlines (char *a[], int n)
{

}    
/**
 * Create a main function that opens a file composed of words, one per line
 * and saves them to an array of MAXLEN strings that is then printed using
 * the above function.
 *
 * Hints:
 *  - Use fopen(3) to open a file for reading, the the returned file handle *    with the fscanf() function to read the words out of it.
 *  - You can read a word from a file into a temporary word buffer using the
 *    fscanf(3) function.
 *  - You can assume that a word will not be longer than MAXLEN characters.
 *  - Use the strdup(3) function to make a permanent copy of a string that has
 *    been read into a buffer.
 * 
 * Usage: "Usage: p7 <file>\n"
 * 
 * Example input/output:
 * ./p7 testfile
 *  1: Bob
 *  2: Tiffany
 *  3: Steve
 *  4: Jim
 *  5: Lucy
 * ...
 */
int main (int argv, char *argc[])
{  if (argc < 2)
    {
      printf ("Usage: p7 <file>\n");
    }
      char buffer[MAX_LINE_LEN];

  /* opening file for reading */
  FILE *fp = fopen (argv[1], "r");
  if (!fp)
    {
      perror ("fopen failed");      exit (EXIT_FAILURE);
    }

  int i = 0;
  while ((i <= MAX_LINES) && (fgets (buffer, sizeof (buffer), fp)))
    {
      printf ("%2i: %s", i, buffer); //i is to count from each lines
      i++;
    }
  fclose (fp);
  return (0);


}

View for testfile:

Bob
Tiffany
Steve
Jim
Lucy
Fred
George
Jill
Max
Butters
Randy
Dave
Bubbles

Solution

  • You can do like this:

    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #define MAXLEN 1000
    #define MAX_LINE_LEN 4096
    
    void printlines (char *a[], int n)
    {
        for (int i = 0; i != n; ++i)
            printf("%2d: %s\n", i, a[i]);
    }
    
    int main (int argc, char *argv[])
    {  
        if (argc < 2)
        {
            printf ("Usage: p7 <file>\n");
            return -1;
        }
    
        FILE *fp = fopen (argv[1], "r");
        if (!fp)
        {
            perror ("fopen failed");
            exit (EXIT_FAILURE);
        }
    
        char* a[MAXLEN];
        char buffer[MAX_LINE_LEN];
        int i = 0;
        while (i < MAXLEN && fscanf(fp, "%s", buffer) == 1)
            a[i++] = strdup(buffer);
        printlines(a, i);
        fclose (fp);
        return (0);
    }