Write a C program: Just need this beginning part... How to do the file reading stuff.
The program will be invoked as P0 [file] where file is an optional argument. If the file argument is not given the program reads data from the keyboard as a device (10%). If the argument is given, the program reads data file file.fs19. (note that file is any name and the extension is implicit). Programs improperly implementing file name or executable will not be graded. 20% is for style. 10% interactive input. 70% performance. Example invocations: P0 // read from the keyboard until simulated EOF P0 < somefile // same as above except redirecting from somefile instead of keyboard, testing keyboard input P0 somefile // read from somefile.fs19
Hi, Please find the solution and rate the answer.
//
// main.c
// FileReading
//
#include <stdio.h>
int main(int argc, const char * argv[]) {
if(argc==2){
FILE *f;
f = (FILE*) fopen(argv[1], "r");
char ch;
while((ch=fgetc(f))!=EOF){//assignment to ch in inner brackets and then comparison.
printf("%c",ch);
}
}else{
char data[1000];
printf("Enter the File Data");
fgets(data, 1000, stdin);
printf("%s\n.Above is the data entered",data);
}
}
Sample out: This was a text file on hard drive.
filename path
Get Answers For Free
Most questions answered within 1 hours.