This needs to be done in C. Please make it as simple and basic as possible. This is an Intro to C Programming calss that I’m taking.
Assignmnet:
Don't use plagiarized sources. Get Your Custom Essay on
Question & Answer: This needs to be done in C. Please make it as simple and basic as possible. This is an Intro to C Programming c…..
GET AN ESSAY WRITTEN FOR YOU FROM AS LOW AS $13/PAGE
Open a file with a function
Read a line of a file with a function
Scan a line of a file for a specific number: 8.
Scan a line of a file for a specific letter: e
Scan all the lines for that specific letter or number and keep count
Print the results with a function
Thank you!
Expert Answer
Open a file with a function #include <stdio.h> #include <errno.h> int main (void) { FILE *fp; fp = fopen ("write.txt","w"); if (fp == NULL) { printf ("File not created okay, errno = %dn", errno); return 1; } //fprintf (fp, "Hello, there.n"); // if you want something in the file. fclose (fp); printf ("File created okayn"); return 0; }
——————————-
Read a line of a file with a function
#include <stdio.h> #include <stdlib.h> // For exit() function int main() { char c[1000]; FILE *fptr; if ((fptr = fopen("program.txt", "r")) == NULL) { printf("Error! opening file"); // Program exits if file pointer returns NULL. exit(1); } // reads text until newline fscanf(fptr,"%[^n]", c); printf("Data from the file:n%s", c); fclose(fptr); return 0; }
——————————————
#include <stdio.h> int main(void){ int end, loop, line; char str[512]; FILE *fd = fopen("data.txt", "r"); if (fd == NULL) { printf("Failed to open filen"); return -1; } printf("Enter the line number to read : "); scanf("%d", &line); for(end = loop = 0;loop<line;++loop){ if(0==fgets(str, sizeof(str), fd)){//include 'n' end = 1;//can't input (EOF) break; } } if(!end) printf("nLine-%d: %sn", line, str); fclose(fd); return 0; }
—————————————–
Print the results with a function —> Use printf()