Need help with your Discussion

Get a timely done, PLAGIARISM-FREE paper
from our highly-qualified writers!

glass
pen
clip
papers
heaphones

CSC 316 Simply a Sequential Stream of Bytes Question

CSC 316 Simply a Sequential Stream of Bytes Question

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 endoffile 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 n1, 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 newline 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 endoffile 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;
}

Have a similar assignment? "Place an order for your assignment and have exceptional work written by our team of experts, guaranteeing you A results."

Order Solution Now

Our Service Charter


1. Professional & Expert Writers: Eminence Papers only hires the best. Our writers are specially selected and recruited, after which they undergo further training to perfect their skills for specialization purposes. Moreover, our writers are holders of masters and Ph.D. degrees. They have impressive academic records, besides being native English speakers.

2. Top Quality Papers: Our customers are always guaranteed of papers that exceed their expectations. All our writers have +5 years of experience. This implies that all papers are written by individuals who are experts in their fields. In addition, the quality team reviews all the papers before sending them to the customers.

3. Plagiarism-Free Papers: All papers provided by Eminence Papers are written from scratch. Appropriate referencing and citation of key information are followed. Plagiarism checkers are used by the Quality assurance team and our editors just to double-check that there are no instances of plagiarism.

4. Timely Delivery: Time wasted is equivalent to a failed dedication and commitment. Eminence Papers are known for the timely delivery of any pending customer orders. Customers are well informed of the progress of their papers to ensure they keep track of what the writer is providing before the final draft is sent for grading.

5. Affordable Prices: Our prices are fairly structured to fit in all groups. Any customer willing to place their assignments with us can do so at very affordable prices. In addition, our customers enjoy regular discounts and bonuses.

6. 24/7 Customer Support: At Eminence Papers, we have put in place a team of experts who answer all customer inquiries promptly. The best part is the ever-availability of the team. Customers can make inquiries anytime.

We Can Write It for You! Enjoy 20% OFF on This Order. Use Code SAVE20

Stuck with your Assignment?

Enjoy 20% OFF Today
Use code SAVE20