Scan of input file ....
Moderator: Board moderators
-
- A great helper
- Posts: 284
- Joined: Thu Feb 28, 2002 2:00 am
- Location: Germany
- Contact:
There are several ways to open and close files and then you can use them very similar to stdin. In C, go for gets(...) and in C++, for getline(...) to read a full line.
For the online judge, you don't need to work with files. You have to use stdin/stdout. And to test it on your home computer, just pipe the input file into the program like this:
program < infile
This works in Unix and DOS. Don't know much about Windows, Mac, etc, sorry. But there's definitely a way there, too.
If you need more info, please ask more specific questions or get an introductory book for your programming language. If you work with Unix, reading the manpages for scanf, fscanf, gets, fgets, fopen etc will help.
For the online judge, you don't need to work with files. You have to use stdin/stdout. And to test it on your home computer, just pipe the input file into the program like this:
program < infile
This works in Unix and DOS. Don't know much about Windows, Mac, etc, sorry. But there's definitely a way there, too.
If you need more info, please ask more specific questions or get an introductory book for your programming language. If you work with Unix, reading the manpages for scanf, fscanf, gets, fgets, fopen etc will help.
-
- A great helper
- Posts: 284
- Joined: Thu Feb 28, 2002 2:00 am
- Location: Germany
- Contact:
You're a liar. In 495, you have to do sth for each number, not each line! Sorry for calling you a liar, but I wanted to make clear you shouldn't worry about lines there. Do it like this:
[c]int n;
while( scanf( "%d", &n ) == 1 ){
doSomethingWith( n );
}[/c]
Or in C++:
[cpp]int n;
while( cin >> n ){
doSomethingWith( n );
}[/cpp]
[c]int n;
while( scanf( "%d", &n ) == 1 ){
doSomethingWith( n );
}[/c]
Or in C++:
[cpp]int n;
while( cin >> n ){
doSomethingWith( n );
}[/cpp]
ya, no need for file in online judge. but in real time contests most of the times u will need to read from files and write in files. but don't worry. there are lots of ways with files. i know a good trick
just do ur program without considering files. now attatch the following codes:
[c]freopen("inputfilename", "rt", stdin);
freopen("outputfilename", "wt", stdout);[/c]
so easy............................................
[/c]

just do ur program without considering files. now attatch the following codes:
[c]freopen("inputfilename", "rt", stdin);
freopen("outputfilename", "wt", stdout);[/c]
so easy............................................

scanf/printf
Hi,
Just for my additional knowledge. Could someone tell me the format string for every declaration below;
[c]
unsigned char s[100]
scanf("%us",s); //is it correct?
short x;
unsigned short x;
long long x;
long long int x;
long int x;
long double x;
unsigned long x;
unsigned int x;
unsigned double x;
unsigned long double x;
unsigned char c;
[/c]
Thanks.
-novice
Just for my additional knowledge. Could someone tell me the format string for every declaration below;
[c]
unsigned char s[100]
scanf("%us",s); //is it correct?
short x;
unsigned short x;
long long x;
long long int x;
long int x;
long double x;
unsigned long x;
unsigned int x;
unsigned double x;
unsigned long double x;
unsigned char c;
[/c]
Thanks.
-novice

-
- New poster
- Posts: 5
- Joined: Fri Oct 26, 2001 2:00 am
- Location: Bangladesh
[c]
unsigned char s[100]
scanf("%us",s); //is it correct?
// no. 'u' is a type specifier not a type modifier. use "%s" instead
short x;
// "%hd" or "%hi" for decimel conversion
// "%ho" for octal conversion
// "%hx" for hexadecimel conversion
unsigned short x;
// "%hu" for decimel conversion
// nothing for octal or hex input.
// Actually octal and hex input are not meant to be preceded by a "-".
// They are interpreted as a group of 3 or 4 bit respectively.
long long x;
// "%lld" or "lli" for decimel
long long int x;
// same as before.
// long is basically a type modifier rather than a type specifier.
// when you write "long" then "long int" is implied
long int x;
// "%ld" or "%li" for dec
// "%lo" for octal
// "%lx" for hex
long double x;
// "%Lf"
unsigned long x;
// "%lu"
unsigned int x;
// "%u"
unsigned double x;
unsigned long double x;
// floating point number are always signed (so far i know)
unsigned char c;
// "%c"
[/c]
For a solid understanding of C the following two books are highly recommended
K&R C book and
Steve Summit's "C Programming FAQs"
unsigned char s[100]
scanf("%us",s); //is it correct?
// no. 'u' is a type specifier not a type modifier. use "%s" instead
short x;
// "%hd" or "%hi" for decimel conversion
// "%ho" for octal conversion
// "%hx" for hexadecimel conversion
unsigned short x;
// "%hu" for decimel conversion
// nothing for octal or hex input.
// Actually octal and hex input are not meant to be preceded by a "-".
// They are interpreted as a group of 3 or 4 bit respectively.
long long x;
// "%lld" or "lli" for decimel
long long int x;
// same as before.
// long is basically a type modifier rather than a type specifier.
// when you write "long" then "long int" is implied
long int x;
// "%ld" or "%li" for dec
// "%lo" for octal
// "%lx" for hex
long double x;
// "%Lf"
unsigned long x;
// "%lu"
unsigned int x;
// "%u"
unsigned double x;
unsigned long double x;
// floating point number are always signed (so far i know)
unsigned char c;
// "%c"
[/c]
For a solid understanding of C the following two books are highly recommended
K&R C book and
Steve Summit's "C Programming FAQs"
Thanks
Thanks, finally i got it.
3n+1 problem
my program gets the reply " Wrong answer". but it run correctly on my computer.
/*@judge_id: 22085KW 100 C */
#include<stdio.h>
int A(unsigned long int x);
void main()
{
int p,i,j,max=0,g,iReal,jReal;
scanf("%d %d\n",&i,&j);
iReal=i;jReal=j;
if (i>j)
{g=i;i=j;j=g;}
for (g=i;g<j+1;g++)
{
p=A(g);
if (p>max) max=p;
}
printf("%d %d %d\n",iReal,jReal,max);
}
int A(unsigned long int x)
{
int q=0;
an:
if (x==1){q++;return q;}
else if(x%2==1)
{x=3*x+1;q++;goto an;}
else
{x=x/2;q++;goto an;}
}
/*@end_of_source_code*/
/*@judge_id: 22085KW 100 C */
#include<stdio.h>
int A(unsigned long int x);
void main()
{
int p,i,j,max=0,g,iReal,jReal;
scanf("%d %d\n",&i,&j);
iReal=i;jReal=j;
if (i>j)
{g=i;i=j;j=g;}
for (g=i;g<j+1;g++)
{
p=A(g);
if (p>max) max=p;
}
printf("%d %d %d\n",iReal,jReal,max);
}
int A(unsigned long int x)
{
int q=0;
an:
if (x==1){q++;return q;}
else if(x%2==1)
{x=3*x+1;q++;goto an;}
else
{x=x/2;q++;goto an;}
}
/*@end_of_source_code*/
detecting blank lines
I have a problem with problems with multiple input
How do I detect the blank lines between test cases when using scanf() to scan in ints?
Here is what I mean.
// start of input file
3
5
3
6
// end of input file
How do I detect that "3 5" is one test case, and "3 6" is another?
Thanks.
I need help to finish up my solution for #497.

How do I detect the blank lines between test cases when using scanf() to scan in ints?
Here is what I mean.
// start of input file
3
5
3
6
// end of input file
How do I detect that "3 5" is one test case, and "3 6" is another?
Thanks.
I need help to finish up my solution for #497.
Problems with multiple input where each test case end with end-of-file (that is, a blank line in the multiple input format!) is really nasty. On such problem, I always read the whole input using gets(s) and then sscanf(s,...) and so on. To detect the end of a testcase, check (feof(stdin) || s[0]) A bit ugly, but I feel that is the safest way to proceed.
Problem with sscanf()
Thanks Yarin.
But I have another problem. I seem to encounter errors when my program tries to read multiple integers using sscanf(). Here's what I mean.
[cpp]
while (gets(line)) {
if (strlen(line) == 0)
break;
for (int i = 0; sscanf(line, "%d", &d) == 1; i++);
}
[/cpp]
So, for example, I input the line "1 2 3 4 5", the int array d[] keeps reading in the value 1 without moving on. This creates an infinite loop which in the end will result in a program crash due to invalid memory access.
Have I made any mistakes?
But I have another problem. I seem to encounter errors when my program tries to read multiple integers using sscanf(). Here's what I mean.
[cpp]
while (gets(line)) {
if (strlen(line) == 0)
break;
for (int i = 0; sscanf(line, "%d", &d) == 1; i++);
}
[/cpp]
So, for example, I input the line "1 2 3 4 5", the int array d[] keeps reading in the value 1 without moving on. This creates an infinite loop which in the end will result in a program crash due to invalid memory access.
Have I made any mistakes?
