I am currently writing a project in C#, the only header file we could include is
ID: 3748180 • Letter: I
Question
I am currently writing a project in C#, the only header file we could include is stdio.h.
This project is only about reading different files and process the information.
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 coukld 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() what should i write to receive the proper input?
** this program is able to receive files with different files w/ different names
Explanation / Answer
//This simple example shows how input redirection from file works. In your case reading from a file given as below
./a.out < sample.txt
I have writter program to show that.
//main file
#include <stdio.h>
int main()
{
int arr[100];
int count,i;
while(scanf("%d",&arr[count])!=EOF)
{
count++;
}
//print integeres read from file
printf("Array elements: ");
for(i = 0; i < count; i++)
{
printf("%d ",arr[i]);
}
printf(" ");
return 0;
}
-------------------------------------------------------------
I have file called sample.txt with following data
1 2 3 4 5
6 7 8 9 10
------------------------------------------
//now I want my program to read from sample.txt file into array and display the content of file.
To do that , first compile the program, I have named my C file as main.c u can give whatever name you have given to your C.
$gcc main.c
Which gives the a.out , now run as below, note $ is command prompt
$./a.out < sample.txt
//output after executing above command is
Array elements: 1 2 3 4 5 6 7 8 9 10
-------------------------------------
you can just use scanf for input file redirection. It reads from file instad of standard input. Hope it helps
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.