我正在编写一个将一个文件保存到另一个名称不同的文件中的代码-但是,我不确定是否需要关闭两个文件?
FILE *logfile = fopen("log.txt", "a+");
while(1) {
char filename[500];
char logline[512];
char channel[512];
//Do stuff
sprintf(filename, "%s.log.txt", channel);
freopen(filename, "a+", logfile);
log_to_file(logline, logfile);
}
答案 0 :(得分:4)
package com.nurettinabaci.mapweather; import android.os.AsyncTask; import android.util.Log; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import java.io.IOException; import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.Response; public class requestWeatherAsync extends AsyncTask<String, Void, String> { OkHttpClient client = new OkHttpClient(); @Override protected String doInBackground(String... params) { Request request = new Request.Builder() .url(params[0]) .get() .build(); try { Response response = client.newCall(request).execute(); Log.d("ASYNCTASK",response.body().string()); return response.body().string(); } catch (IOException e) { e.printStackTrace(); } return null; } @Override protected void onPostExecute(String jsonData) { super.onPostExecute(jsonData); Log.i("ASYNCTASK", "onPostExecute"); try { JSONObject json = new JSONObject(jsonData); // temperature JSONObject tempData = new JSONObject(json.getString("main")); double temp = Double.parseDouble(tempData.getString("temp")); MapsActivity.temperature = (int) (temp -273.15); // weather description JSONArray weatherInfo = new JSONArray(json.getString("weather")); // weather blok JSONObject weatherData = weatherInfo.getJSONObject(0); MapsActivity.description = weatherData.getString("description"); MapsActivity.mPlaceName = json.getString("name"); } catch (JSONException e) { e.printStackTrace(); } } }
函数打开名称为path指向的字符串的文件,并将stream指向的流与其关联。 原始流(如果存在)已关闭。 [...]
因此,您无需显式关闭前一个流,一旦使用完毕,只需关闭最近的流即可。