Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I am currently writing a project in C language, the only header file we could in

ID: 3749476 • Letter: I

Question

I am currently writing a project in C language, the only header file we could include is stdio.h.

This project is only about reading different files and process the information.

the only c library functions that we could use is printf(),scanf() and feof().

To start this project I have no idea how to receive inputs from different files,

On the project discription he mentioned after our file has been compiled, we could use the command:

projectFile.x < something.input

But i still do not understand what should i do if i want to use scanf() and feof() to store the file content into the array?

Should it be a char or int (or something else) array?

** this program is able to receive files with different files w/ different names

Explanation / Answer

In order to understand the usage of scanf() and feof(). Please understand the basics on what these functions are for. Below is a detailed explaination on what these functions are used for -

Scanf()

int scanf(const char *format, ...)

The scanf() function is a C library fuction, which reads formatted input from stdin.

Now the format specifier in the scanf() function is like this  
%[*][width][modifiers]type - [] is optional
where
a) [*] = optional starting asterisk indicates that the data is to be read from the stream but ignored, i.e. it is not stored in the corresponding argument.

b) [width]=This specifies the maximum number of characters to be read in the current reading operation.

c) [modifier]=Specifies a size different from - int (in the case of d, i and n),
unsigned int (in the case of o, u and x)
or float (in the case of e, f and g)
for the data pointed by the corresponding additional argument:
h : short int (for d, i and n),
or unsigned short int (for o, u and x)
l : long int (for d, i and n),
or unsigned long int (for o, u and x),
or double (for e, f and g)
L : long double (for e, f and g).

d) type = A character specifying the type of data to be read and how it is expected to be read.
i) c = char *
ii) d = int *
iii) e, E, f, g, G = float *
iv) o = int * (Octal integer)
v) s = char *
vi) u = unsigned int *
vii) x,X = int * (Hexadecimal int)

Example of scanf() usage:
//string input
printf("Enter name: ");
scanf("%s", str1);

//int input as signed decimal number
printf("Enter name: ");
scanf("%d", &n);

//check the 19th char of input string
char word[20];
if(scanf("%19s", word) == 1)
puts(word);

scanf is usually used in situations when the program cannot guarantee that the input is in the expected format. Therefore a robust program must check whether the scanf call succeeded and take appropriate action. If the input was not in the correct format, the erroneous data will still be on the input stream and must be read and discarded before new input can be read.

feof()

int feof(FILE *stream)

feof() is a C library fuction, tests the end-of-file indicator for the given stream.
This indicator is generally set by a previous operation on the stream that attempted to read at or past the end-of-file.

Notice that stream's internal position indicator may point to the end-of-file for the next operation, but still, the end-of-file indicator may not be set until an operation attempts to read at that point.

This indicator is cleared by a call to clearerr, rewind, fseek, fsetpos or freopen. Although if the position indicator is not repositioned by such a call, the next i/o operation is likely to set the indicator again.

Parameters
stream Pointer to a FILE object that identifies the stream

Return Value
A non-zero value is returned in the case that the end-of-file indicator associated with the stream is set.
Otherwise, zero is returned.

Example:
#include <stdio.h>

int main ()
{
FILE * pFile;
int n = 0;
pFile = fopen ("myfile.txt","rb");
if (pFile==NULL) perror ("Error opening file");
else
{
while (fgetc(pFile) != EOF) {
++n;
}
if (feof(pFile)) {
puts ("End-of-File reached.");
printf ("Total number of bytes read: %d ", n);
}
else puts ("End-of-File was not reached.");
fclose (pFile);
}
return 0;
}

So to use a user input on the file name use the scanf() function based on which you will need to replace the "myfile.txt" in the above example of code and move the string which will store your file name. That should allow you to read and process different files based on user input each time a new file name is entered during the code execution.