CSC 316 Simply a Sequential Stream of Bytes Question
Description
Write a program to count spaces, tabs (/t), and newlines of the file ยdata.txtย.
CSC 316 Lab 10: System I/O and File Handling
Submit your C code on Canvas before the deadline. Group discussions are encouraged for this lab assignment.
Coding Task:
Write a program to count spaces, tabs (/t), and newlines of the file ยdata.txtย.
Study the code examples using C standard I/O functions from the last lecture and
the examples below.
Example 1: The getc() function
The getc() function reads a single character from the input stream (a file) at the current
position and increases the stream position to the next character. The getc() function
returns the character that is read as an integer. A return value of EOF indicates an error
or end–of–file condition.
Syntax:
#include <stdio.h>
int getc(FILE *fp);
Code example: the following program reads a file and prints its first line to the screen.
/***********************************************************************
This code reads and prints the first line from a file.
************************************************************************/
#include <stdio.h>
#defineMAX_LEN 800
void main()
{
FILE *fp;
char buffer[MAX_LEN + 1];
int i, ch;
fp = fopen(“./data.txt”,“r”);
for (i = 0; (i<(sizeof(buffer)–1) && ((ch = getc(fp)) != EOF) && (ch != ‘n‘)); i++)
{
buffer[i] = ch;
}
buffer[i] = ‘0‘;
if (fclose(fp))
{
perror(“fclose error”);
}
printf(“The first line is: %sn“, buffer);
}
Example 2: The fgets() function
The fgets() function reads a line of text from a stream (a file) one character at a time,
stopping at newline character (n), or up to the end of the stream, or until the number
of characters read is equal to n–1, whichever comes first. The fgets() function stores
the result in string and adds a null character (0) to the end of the string.
The string includes the new–line character, if read. If n is equal to 1, the string is empty.
The fgets() function returns a pointer to the string buffer if successful. A NULL return
value indicates an error or an end–of–file condition.
Syntax:
#include <stdio.h>
char *fgets(char *string, int n, FILE *fp);
Code example: the following program compares two files, printing the first line where
they differ.
/***********************************************************************
This code compares two files, printing the first line where they differ.
Usage: run: ./compare file_1.txt file_2.txt
************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_LINE_LEN 1000
int parse_arg_list(int argc, char *argv[]);
int main(int argc, char *argv[])
{
if (!parse_arg_list(argc, argv))
{
fprintf(stderr, “Error: invalid arguments.n“);
exit(EXIT_FAILURE);
}
char *program_name = argv[0];
FILE *fp1;
FILE *fp2;
if ((fp1 = fopen(argv[1], “r”)) == NULL)
{
fprintf(stderr, “%s: can’t open %s.n“, program_name, argv[1]);
exit(EXIT_FAILURE);
}
if ((fp2 = fopen(argv[2], “r”)) == NULL)
{
fprintf(stderr, “%s: can’t open %s.n“, program_name, argv[2]);
exit(EXIT_FAILURE);
}
char line_1[MAX_LINE_LEN];
char line_2[MAX_LINE_LEN];
size_t line_number = 1;
while (fgets(line_1,MAX_LINE_LEN,fp1)!=NULL && fgets(line_2,MAX_LINE_LEN,fp2)!=NULL)
{
if (strcmp(line_1, line_2) != 0)
{
printf(“%s [%zu]: %s“, argv[1], line_number, line_1);
fclose(fp1);
printf(“%s [%zu]: %s“, argv[2], line_number, line_2);
fclose(fp2);
break;
}
line_number++;
}
exit(EXIT_SUCCESS);
}
int parse_arg_list(int argc, char *argv[])
{
if (argc != 3)
{
return 0;
}
return 1;
}
Get your college paper done by experts
Do my question How much will it cost?Place an order in 3 easy steps. Takes less than 5 mins.
Leave a Reply
Want to join the discussion?Feel free to contribute!