I really want to understand whether there are conflicts regarding Go's ReadString and Scanf functions when reading from a redirected standard input. FYI, developement is done on Ubuntu.
I currently have a recursive function that runs recursively, where in each iteration it asks the user to input the number of integers that will be tested, for then to have a testing session be done within a newly created 'testcase' object. So here's the following code for the function:
func (T *Test) Testing() {
if I == T.N {
return
} else {
//ask for number of integers per testcase
var input string
x:=0
_,err = fmt.Scanf("%v
", &x)
//check for correct user input first
if err != nil {
fmt.Println("Error: ", err, "LOL")
ProgramExit()
}
//receive string as input (to accomodate the itegers that are going to be put in a line, with space-seperated integers), terminate at newline
input,err = in.ReadString('
')
input = strings.Trim(input, "
")
//check for correct user input first
if err != nil {
fmt.Println("Error: ", err, "Errorororo")
ProgramExit()
}
//split any spaces in the input
input_array := strings.Split(input, " ")
if l := len(input_array); l!=x {
fmt.Printf("inconsistent length of input! Inputted number of integers is %v, being ", l)
fmt.Println(input_array)
os.Exit(1)
}
//make new TestCase object, with its sum and i zeroed first
T.Tst[I] = &TestCase{x,make([]int,x),0,0}
//fill TestCase object
T.Tst[I].Fill(input_array)
//increase I
I++
//this is where println could be added to check for iterations
//iterate by doing recursion back to method, instead of doing a for loop
T.Testing()
}
For clarification, ProgramExit() is just a function to exit the entire program when error occurs. While Fill() is a function to fill up T object using input_array's data.
Now my problem is that when I run the program with the mentioned loop, it can successfully read the inputs that I manually typed into the command line below:
(format of input is:
num of testcases
num of integers for testcase 1
num of input integers for testcase 1
num of integers for testcase 2
num of input integers for testcase 2
...
)
2
4
3 -1 1 14
5
9 6 -53 32 16
However when I put the same inputs into a .txt file and redirect that as the input for my program, it gets an EOF error, that seems to happen during the 2nd iteration of the function (i.e. when it tries to read in for 2nd testcase object). Here is the output that I got in the CLI:
$ main < data.txt
0
Error: EOF LOL
Incorrect input! needs to be an integer input!
And this is the output when I put a println function for every successful iteration to print out value of I (I'th iteration)
$ main < data.txt
1
0
Error: EOF LOL
Incorrect input! needs to be an integer input!
So it does seem that my function is iterating properly however unfortunately after reading the an entire line with ReadString, Scanf would read the next line as a newline (or EOF), instead of reading the next line properly. (Which does not happen when I inputted the integers manually).
Could anyone please help point out whether there is a problem with my function that does this, or is there an alternative way to do this properly? (please note that I am developing this for a challenge, and in this challenge, for loop is not allowed lol)
Thanks in advance!