Scan of input file ....

Write here if you have problems with your C source code

Moderator: Board moderators

Post Reply
kYsis
New poster
Posts: 8
Joined: Sun Mar 10, 2002 2:00 am

Post by kYsis »

How do I scan an input file ?
Fro example, an input file made of sentences on separate lines ...
Stefan Pochmann
A great helper
Posts: 284
Joined: Thu Feb 28, 2002 2:00 am
Location: Germany
Contact:

Post by Stefan Pochmann »

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.
kYsis
New poster
Posts: 8
Joined: Sun Mar 10, 2002 2:00 am

Post by kYsis »

Ok, so I program in C and I just need to know another thing :
How do I scan an input file made of several lines
Example :
//Beginning of the file
15
45
12

//End of file
I have to do something for each line, like for the 495th problem ...
Stefan Pochmann
A great helper
Posts: 284
Joined: Thu Feb 28, 2002 2:00 am
Location: Germany
Contact:

Post by Stefan Pochmann »

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]
kYsis
New poster
Posts: 8
Joined: Sun Mar 10, 2002 2:00 am

Post by kYsis »

and that's it ?

Thanks alot :wink:
Subeen
Experienced poster
Posts: 127
Joined: Tue Nov 06, 2001 2:00 am
Location: Bangladesh
Contact:

Post by Subeen »

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 :wink:
just do ur program without considering files. now attatch the following codes:

[c]freopen("inputfilename", "rt", stdin);
freopen("outputfilename", "wt", stdout);[/c]

so easy............................................ :lol: [/c]
Fresh
New poster
Posts: 46
Joined: Mon Apr 15, 2002 10:42 am
Contact:

scanf/printf

Post by Fresh »

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 :wink:
TanveerAhsan
New poster
Posts: 5
Joined: Fri Oct 26, 2001 2:00 am
Location: Bangladesh

Post by TanveerAhsan »

[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"
Fresh
New poster
Posts: 46
Joined: Mon Apr 15, 2002 10:42 am
Contact:

Thanks

Post by Fresh »

Thanks, finally i got it.
changgica
New poster
Posts: 3
Joined: Mon Sep 16, 2002 6:27 pm
Location: Singapore

Post by changgica »

hi, coud you tell me the range of all the data types above? ( the data types used in ACM competition and in online judge).

I'm learning to use C or C++.
Thank so much. :)
pahmed
New poster
Posts: 2
Joined: Wed Sep 25, 2002 3:35 am

3n+1 problem

Post by pahmed »

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*/
ec3_limz
Learning poster
Posts: 79
Joined: Thu May 23, 2002 3:30 pm
Location: Singapore

detecting blank lines

Post by ec3_limz »

I have a problem with problems with multiple input :evil:

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.
Yarin
Problemsetter
Posts: 112
Joined: Tue Sep 10, 2002 5:06 am
Location: Ume
Contact:

Post by Yarin »

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.
ec3_limz
Learning poster
Posts: 79
Joined: Thu May 23, 2002 3:30 pm
Location: Singapore

Problem with sscanf()

Post by ec3_limz »

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? :(
Adil
Learning poster
Posts: 57
Joined: Sun Sep 29, 2002 12:00 pm
Location: in front of the monitor :-)
Contact:

Post by Adil »

hello ec3_limz. you could use:
[c]i = 0;
while(sscanf(line, "%d %[^\0]", &d[i++], line) == 2);
[/c] instead of
[c]
for (int i = 0; sscanf(line, "%d", &d) == 1; i++);
[/c]

there of course are other ways. hope this helps.
Post Reply

Return to “C”