Does standard I/O differ from File I/O?

I wrote a code to solve this problem “USACO” so my code worked with Standard I/O and the example was correct , but when I switched to File I/O the answer was 1 wich is not correct ! Can anyone explain why is it changing ? Its the exact same code but I used “freopen” function above .
// Sorry for bad english

Please post both your code with and without file I/O here. Make sure to also format them correctly.
Reading from files should not change the output, so maybe something else is causing this to happen.

1 Like

I am having the same issue, and maybe it will help the OP. Standard and file is below:

on USACO IDE: https://ide.usaco.guide/N9OMu-3I9CTfaxEE1DJ

Standard:

// Source: https://usaco.guide/general/io

#include <bits/stdc++.h>
#include
using namespace std;

class Person{

public:
int p;
int m;
int t;

  Person(int pt, int mt, int tt){
  	p = pt;
  	m = mt;
  	t = tt;
  }

};

// the argument is the input filename without the extension
void setIO(string s) {
freopen((s + “.in”).c_str(), “r”, stdin);
freopen((s + “.out”).c_str(), “w”, stdout);
}

int main() {
//setIO(“badmilk”);

int n, m, d, s; cin >> n >> m >> d >> s;
vector data;
for(int i = 0 ; i < d; i++){
int a, b, c; cin >> a >> b >> c;
Person temp(a, b ,c);
data.push_back(temp);
}

vector<pair<int, int>> sickness;

int firstSick = 999; //Max time is 100

for(int i = 0; i < s; i++){
int a, b; cin >> a >> b;
sickness.push_back(make_pair(a, b));
}

//Loop through all people and find perseon who gets sick the quickest;
for(int i = 0; i < s; i++){
firstSick = min(firstSick, sickness[i].second);
//cout << "First sick: " << firstSick << ", Patient time: " << sickness[d].second << endl;
}

int cures = 0;

//Run through each milk type
for(int type = 0; type < m; type++){
int milkMakesSick = 0; //Later: if milkMakesSick == total sick people, calculate how many doses
int tempDoses = 0;
bool alreadyCured[1000];

  //Run through each sick person
  for(int i = 0; i < s; i++){
  	
  	//Run through each in data, looking for that person
  	for(int j = 0; j < d; j++){
  		//If that person (data[j]) drank the type of milk 
  		if(data[j].p == type) {
  			//AND they drank it before firstSick
  			if(data[j].t < firstSick && !alreadyCured[j]){
  				alreadyCured[j] = true;
  				milkMakesSick++;
  				tempDoses++;
  			}else if(!alreadyCured[j]){ //Otherwise if they have not recieved a dose
  				tempDoses++;
  				alreadyCured[j] = true;
  			}
  		}
  	}
  	//Check to see if everyone got sick from milk before time firstSick
  	//if milkMakesSick == total sick people, update how many doses
  	if(milkMakesSick == s){
  		cures = max(tempDoses, cures);
  	}
  	//cout << "Temp doeses, cures, milkMakesSick " << tempDoses << ", " << cures << ", " << milkMakesSick << endl;
  }

}

cout << cures << endl;

}

File:

// Source: https://usaco.guide/general/io

#include <bits/stdc++.h>
#include
using namespace std;

class Person{

public:
int p;
int m;
int t;

  Person(int pt, int mt, int tt){
  	p = pt;
  	m = mt;
  	t = tt;
  }

};

// the argument is the input filename without the extension
void setIO(string s) {
freopen((s + “.in”).c_str(), “r”, stdin);
freopen((s + “.out”).c_str(), “w”, stdout);
}

int main() {
//setIO(“badmilk”);

int n, m, d, s; cin >> n >> m >> d >> s;
vector data;
for(int i = 0 ; i < d; i++){
int a, b, c; cin >> a >> b >> c;
Person temp(a, b ,c);
data.push_back(temp);
}

vector<pair<int, int>> sickness;

int firstSick = 999; //Max time is 100

for(int i = 0; i < s; i++){
int a, b; cin >> a >> b;
sickness.push_back(make_pair(a, b));
}

//Loop through all people and find perseon who gets sick the quickest;
for(int i = 0; i < s; i++){
firstSick = min(firstSick, sickness[i].second);
//cout << "First sick: " << firstSick << ", Patient time: " << sickness[d].second << endl;
}

int cures = 0;

//Run through each milk type
for(int type = 0; type < m; type++){
int milkMakesSick = 0; //Later: if milkMakesSick == total sick people, calculate how many doses
int tempDoses = 0;
bool alreadyCured[1000];

  //Run through each sick person
  for(int i = 0; i < s; i++){
  	
  	//Run through each in data, looking for that person
  	for(int j = 0; j < d; j++){
  		//If that person (data[j]) drank the type of milk 
  		if(data[j].p == type) {
  			//AND they drank it before firstSick
  			if(data[j].t < firstSick && !alreadyCured[j]){
  				alreadyCured[j] = true;
  				milkMakesSick++;
  				tempDoses++;
  			}else if(!alreadyCured[j]){ //Otherwise if they have not recieved a dose
  				tempDoses++;
  				alreadyCured[j] = true;
  			}
  		}
  	}
  	//Check to see if everyone got sick from milk before time firstSick
  	//if milkMakesSick == total sick people, update how many doses
  	if(milkMakesSick == s){
  		cures = max(tempDoses, cures);
  	}
  	//cout << "Temp doeses, cures, milkMakesSick " << tempDoses << ", " << cures << ", " << milkMakesSick << endl;
  }

}

cout << cures << endl;

}