How To Read Input From Test Case File in C - c

Currently i am enrolled in NPTEL course. There i need to make c program as assignment.
Qusetion is in this format :-
Write a program that reads numbers which are in the range 0 to 100, till it encounters -1. Print the sum of all the integers that you have read before you encountered -1
INPUT:
A sequence of integers separated by whitespace. There may be other integers following -1.
How do i read input from test case file? plz help
I have used following code :-
while((n=scanf("%d",&n1))!=EOF)
{
printf("%d",n);
}
Loop is iterating properly ie if test case 1 has 5 input its running for 5 times. If test case 2 has 2 input hen iterating 2 time. But it is unable to read input . Please Help.

You're printing n which is the number of items read. You need to print n1.
EDIT:
Your check for the while loop is incorrect. You need to check the value that is read i.e. n1 but once again, you're using the value of n to check for EOF. You should be checking for -1 as well since that's what you want right?

Related

How to check scanf two integers in c

I have a problem, that I need to scan two integers in one line divided by a space, so the code looks something like this:
scanf("%d %d",&integer1,&integer2);
And in this code I need to check whether there were scanned two integers. Can someone help me?
Thank you in advance
By default scanf() reads the space there is no meaning in giving space. If you want to read two integers scanf("%d %d",&integer1,&integer2); and scanf("%d%d",&integer1,&integer2); both will help you.
It will accept the following inputs:
1 2
12 22
3 2 5 //EOF
your program will pass only after reading two integers. You don't need to check anything.
To eliminate EOF
By default scanf returns number of values read so make use of it.
if(scanf("%d%d",&integer1,&integer2) != 2)
{
//if more than two values are entered
//perform some error handling
}

Need help resolving weird fscanf issue

I am trying to read from a file line by line by running it through a while loop which is instructed to exit once EOF has been reached. But for some reason once the last line has been read and the while condition is checked again the program just freezes.
This is my code:
char character1;
int number1;
while(fscanf(file,"%s %d",&character1, &number1) != EOF){
//printf("%s %d\n",character1,number1)
}
My files contents:
A 1
B 2
C 3
D 4
E 5
Output:
A 1
B 2
C 3
D 4
E 5
| <---Blinking terminal pointer currently there
Can anyone help me figure this out?
EDIT: I am not opening/closing the file in main(), I am doing it in another function, could this be causing a problem?
Improve the condition checking on the while loop. The fscanf() can produce more results that EOF or a positive number. It can also return a positive number when an end-of-file occurs after conversion has begun. Meaning that you have something going wrong with a conversion and so the data is still there the next time you loop around to get more data from the stream. So you are stuck infinitely failing to convert that same failed conversion.
You are looking for 2 input items so check that the fscanf() has found 2 input items in order to continue looping.
The problem is that your fscanf reads the \r or \n in the end of every line. Just creates a buffer to ignore it and will be fine. I did the following and it worked smoothly.
char character1;
int number1;
char buffer[2]; // will read the end of line
while(fscanf(file,"%c %d",&character1, &number1) != EOF){
fgets(buffer, 2, file);// does the job
printf("[%c] [%d]\n",character1,number1);
}

How to display output after entering some integers separated by a newline without the use of arrays?

Task:
t denotes the number of inputs, followed by t lines, each containing a single integer n.
For each integer n given at input, display a line with the value of n.
Sample input:
4
1
2
5
3
Sample output:
1
2
5
6
The output should appear after all lines of integer n are taken as input, i.e, it should not display output after each line of input.
How can this be done using a while loop without the use of array to store the input numbers?
while(i<t)
{
scanf("%d",&num);
printf("%d",&num);
i++;
}
This code is working fine if the input numbers n are separated by a space and appears on the same line. But when the input numbers are provided after a newline, it displays the corresponding output after each input value.
Generally such type of input output are used in coding competition where a user is expected to match the expected output to the actual output.
Try pasting the input using a command prompt or online compilers and check it. It is totally fine as the output is as expected.
How does it take if the total input is given at once.
First it reads the t and then it reads the num and prints the number but your printing is actually after the input. This is how the output is checked in a coding competition.
PS: If you want everything after input, use arrays.
If you want to this without using array, then the best way i could think of is recursion. But note, that internally, your values will be stored in the stack frames, and your values will be printed in reverse(because stack is LIFO). Here is how you can do,
void foo(i, t)
{
if(t==i)
return;
int num;
scanf("%d",&num);
foo(++i, t);
printf("%d\n",num);
}
Note, however, that the values will be printed in reverse, as stack is LIFO

How to test my program against an input test case file

I was participating in some coding contests and luckily my code also ran. However my solution was not expected because i was wrong with the pattern of taking input.
The question involved taking in an integer as an input and performing some operation and returning a different or same integer. I do not have any problem with the program, I just don't know how to code so as to take inputs like this
Input
The input will contain several test cases (not more than 10).
Each test case is a single line with a number n, 0 <= n <= 1 000 000 000.
It is the number given as input.
Output
For each test case output a single line, containing the integer returned.
Example
Input:
12
2
Output:
13
2
My code is
#include <stdio.h>
int functionReturningInteger(int n)
{
// implementation
........
return num;
}
int main(void)
{
int number;
//printf("Enter the number: ");
scanf("%d",&number);
printf(functionReturningInteger(number));
return 0;
}
How am i supposed to know how many inputs they will give ( although they do provide a maximum limit). And if i use an array to store these inputs whose size is equal to the maximum limit, how do i check the size of an integer array in c ?
I will appreciate anybody helping out with a small piece of code. Also if am able to test it against an input test file and generate an "output.txt" (output file). I already have the desired output file "des.txt". Then how can i match whether both the files are same or not ?
#include <stdio.h>
int scanned;
while((scanned = scanf("%d", &number)) != EOF) {
printf("%d\n", functionReturningInteger(number));
}
If scanf detects the end of input before a successful conversion, it returns EOF.
For the other questions, redirect input and output,
$ ./your_prog < input.txt > output.txt
and compare
$ comp output.txt des.txt
You could read line by line until there is nothing to read from file. This way you don't need to know how many inputs is given.
There is no default way to track array size in C.
To match files you can use diff on Linux\Unix os.

How to input numbers from console

I want to read some numbers from the console. The numbers will appear in this way -
5 1 2 3 4 5
4 5 6 7 8
6 2 3 4 5 6 7
..............
EOF
The starting number represents how many number will appear in that line, i.e., the first number of the first line is 5, so there will be 5 more numbers on this line. The end of input will be denoted by the EOF (end-of-file).
I thought of reading the whole line as a string and then convert them to numbers but I want to know is there any other way to do this.
The 'standard' answer is scanf(). The trouble with the standard answer is that it won't allow you to check that there are the correct number of numbers on a line. So, your idea of reading a line and then converting it piecemeal is much better for error detection.
Take a look at How to use sscanf() in loops; the answer there shows you the basics of what you should do. It isn't an exact duplicate. You will read a first value identifying how many entries are on the line, followed by code to read that many entries into an array, with appropriate diagnostics if the data doesn't match the format claimed for it.
If you are in charge of the data format, you should consider dropping the count field - let the computer count how many values are on the line. Computers are good at counting, and it removes a source of errors to be detected and handled (so it makes the programming easier). (If you do change the input format, your question becomes a duplicate of the linked question.)
Have a look at the documentation of scanf:
int count, current, i;
while(scanf("%d", &count) > 0)
{
for(i = 0; i < count; i++)
{
scanf("%d", &current);
// store current
}
}

Resources