-
Getting a variable
Is there a way to pull any variable type from the input stream, and if the stream is empty then just skip it? I know about gets() but that does char arrays, I want to be able to do numbers. I'm trying to solve a few problems at a programming competition board, and the judge gives the program a long list of numbers in the stream like:
45 1003 24 1 19
19 24 145 111 1
432 123 51 4 4
etc....
An arbitrary amount numbers up to 10 are in each line and an arbitrary number of lines between 1 and 1000 are given and with no number like -1 to signal when the stream is done so I never know if 1 line or 1000 lines are in the stream. I REALLY don't want to use gets() and have to parse, that would really make me want to quit... :(
-
no that does not exist. If it did, socket programming would be a hell of a paradise
-
actually, for your case and knowing that the input flow is in ASCII you could do some scanf but that C not C++
how hard is it to use strtok() to parse the string by blanks ' ' and to then do a atoi on each word.
read all the lines until your read an error or until EOF
-
ok, since I am waiting for .NET to finish to install, I'll give you an example of how to use the library to do the dirty work:
Code:
#define LEN = 256
#define CHARSET = " \r\n\t"
int main(int argc, char *argv[], char *envp[])
{
int len;
int readInt;
// read up to 256 char
while ( fgets(line, LEN-1, stdin) != NULL ) {
// make sure the line ends with 0
line[LEN-1] = 0;
for ( char * p = strtok(NULL, CHARSET); p != NULL; p = strtok(p,CHARSET) ) {
// et hop!! here is an integer read from the flow
readInt = atoi(p);
// do whatever you want with that
}
}
// done
return 0;
}
it's up to you to test it
-
ok ok, I wrote a little too fast, I compiled the following one. I didn't test it though. you can do it.
Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define LEN 256
#define CHARSET " \r\n\t"
int main(int argc, char *argv[], char *envp[])
{
int readInt;
char line[LEN];
// read up to 256 char
while ( fgets(line, LEN-1, stdin) != NULL ) {
// make sure the line ends with 0
line[LEN-1] = 0;
for ( char * p = strtok(NULL, CHARSET); p != NULL; p = strtok(p,CHARSET) ) {
// et hop!! here is an integer read from the flow
readInt = atoi(p);
// do whatever you want with that
}
}
// done
return 0;
}
-
Since you're using C++, use fstreams, not FILE *.
Code:
#include <iostream>
#include <fstream>
int main( void ) {
std::ifstream in( "data" );
int n;
while( !in.eof( ) ) {
in>>n;
std::cout<<n<<std::endl;
}
}
-
But that post applies to C and FILE *s. Are you sure it applies to C++ fstreams?
-
I'm pretty sure eof() for streams has the same characteristics, good and bad, as feof() for FILE *s. Proof of the pudding would be to run your code and see if you get the extra input tacked to the end of the report. I usually do when I use this syntax.
-
LOL wow! that might be the cause of this weird bug in my highscores function :D Well, what can I say... thank you elad for mentioning "the extra input tacked to the end of the report" :p Ok, I'll butt out now.