data reading
Moderator: Board moderators
data reading
I've faced many data reading problems. The ways of giving data in almost every problem are different. Especially in multi input problems, they are nightmares to me. In 469, for example, you have to analyze if the input is data part or query part. And in 10324, you have to stop the program if you get '\n' or EOF. I'm troubled with these problems. I often gets RE instead of acceptd because of the data reading. Could someone share your experience and methods? And in what book I can find these information?
You can use gets.
If it's a blank line, the string get from gets is of strlen 0.
e.g. for a program runs until reading a blank line:
[c]char line[100000];
while(gets(line))
{
if(strlen(line) == 0) break;
......
}
[/c]
If it's the EOF, the gets returns NULL.
e.g. for a program runs until the EOF is read:
[c]char line[100000];
while(gets(line) != NULL)
{
......
}
[/c]
Hope can help~
If it's a blank line, the string get from gets is of strlen 0.
e.g. for a program runs until reading a blank line:
[c]char line[100000];
while(gets(line))
{
if(strlen(line) == 0) break;
......
}
[/c]
If it's the EOF, the gets returns NULL.
e.g. for a program runs until the EOF is read:
[c]char line[100000];
while(gets(line) != NULL)
{
......
}
[/c]
Hope can help~
Re: data reading
The following snippet takes care of '\n' and EOF:htl wrote:And in 10324, you have to stop the program if you get '\n' or EOF. I'm troubled with these problems.
Code: Select all
while(cin>>s)
{
// .....
}