C language in Emacs Using the Emacs editor create a file cal

C language in Emacs

Using the Emacs editor create a file called testdata4 (no extension) that contains 5 positive integer values. Write a C program that reads all five values from this file and prints them back to the screen. Each number should be read using the fscanf function embedded inside a for-loop. The last printed integer should be followed by a newline.

Solution

// File testdata4

5

7

2

3

6

//fileReader.c

#include<stdio.h>
#include<stdlib.h>

int main(void)
{
FILE *fp;

if( (fp = fopen(\"testdata4\", \"r\")) == NULL)
{
printf(\"No such file\ \");
exit(1);
}

if (fp == NULL)
{
printf(\"Error Reading File\ \");
}

int num;
while(fscanf(fp,\"%d\", &num) == 1)
{
printf(\"%d\", num);
}
printf(\"\ \");
return 0;
}

/*

Sample output

$ gcc fileReader.c

$ ./a.out

57236

$

*/

C language in Emacs Using the Emacs editor create a file called testdata4 (no extension) that contains 5 positive integer values. Write a C program that reads a

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site