我正在尝试读取一个看起来如下的整数,
6
1
2 5
2 7
2 9
1
1
我想读取一个在数字2之后的整数,它在两者之间有空格。但我无法阅读它。我尝试过如下。
$i = 0;
while($i<10){
fscanf(STDIN, "%d %d", $arr[$i]);
// I have tried fscanf(STDIN, "%d\t%d", $arr[$i]); this too.
$i++;
}
print_r($arr);
输出为:
[0] => 6
[1] => 1
[2] => 2
[3] => 2
[4] => 2
[5] => 1
[6] => 1
但我的预期输出是:
[0] => 6
[1] => 1
[2] => 2
[3] => 5
[4] => 2
[5] => 7
[6] => 2
[7] => 9
[8] => 1
[9] => 1
我也试过以下它也没有用。
while(fgets(STDIN)){
echo $i++;
echo "line: $f";
}
答案 0 :(得分:1)
在阅读两个输入
时,您必须添加两个变量 @Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
if (requestCode != INPUT_FILE_REQUEST_CODE || mFilePathCallback == null) {
super.onActivityResult(requestCode, resultCode, data);
return;
}
Uri[] results = null;
// Check that the response is a good one
if (resultCode == Activity.RESULT_OK) {
if (data == null) {
// If there is not data, then we may have taken a photo
if (mCameraPhotoPath != null) {
results = new Uri[]{Uri.parse(mCameraPhotoPath)};
}
} else {
if (data.getData() != null) {
String dataString = data.getDataString();
if (dataString != null) {
// results = new Uri[]{Uri.parse(dataString)};
// Will return "image:x*"
String wholeID = DocumentsContract.getDocumentId(Uri.parse(dataString.replace("%3A", ":")));
// Split at colon, use second item in the array
String id = wholeID.split(":")[1];
String type = wholeID.split(":")[0];
String[] column = {MediaStore.Images.Media.DATA};
// where id is equal to
String sel = MediaStore.Images.Media._ID + "=?";
Cursor cursor = getContentResolver().
query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
column, sel, new String[]{id}, null);
String filePath = "";
int columnIndex = cursor.getColumnIndex(column[0]);
if (cursor.moveToFirst()) {
filePath = cursor.getString(columnIndex);
results = new Uri[]{Uri.parse("file:" + filePath)};
}
cursor.close();
}
} else {
if (data.getClipData() != null) {
ClipData clipData = data.getClipData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
results = new Uri[clipData.getItemCount()];
for (int i = 0; i < clipData.getItemCount(); i++) {
ClipData.Item image = clipData.getItemAt(i);
Uri uri = image.getUri();
// Will return "image:x*"
String wholeID = DocumentsContract.getDocumentId(uri);
// Split at colon, use second item in the array
String id = wholeID.split(":")[1];
String type = wholeID.split(":")[0];
String contentUri;
if ("image".equals(type)) {
contentUri = MediaStore.Images.Media.DATA;
} else if ("video".equals(type)) {
contentUri = MediaStore.Video.Media.DATA;
} else if ("audio".equals(type)) {
contentUri = MediaStore.Audio.Media.DATA;
}
String[] column = {MediaStore.Images.Media.DATA};
// where id is equal to
String sel = MediaStore.Images.Media._ID + "=?";
Cursor cursor = getContentResolver().
query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
column, sel, new String[]{id}, null);
String filePath = "";
int columnIndex = cursor.getColumnIndex(column[0]);
if (cursor.moveToFirst()) {
filePath = cursor.getString(columnIndex);
}
cursor.close();
results[i] = Uri.parse("file:" + filePath);
}
}
}
}
}
mFilePathCallback.onReceiveValue(results);
mFilePathCallback = null;
} else if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.KITKAT) {
if (requestCode != FILECHOOSER_RESULTCODE || mUploadMessage == null) {
super.onActivityResult(requestCode, resultCode, data);
return;
}
if (requestCode == FILECHOOSER_RESULTCODE) {
if (null == this.mUploadMessage) {
return;
}
Uri result[] = null;
try {
if (resultCode != RESULT_OK) {
result = null;
} else {
if (data.getData() != null)
result[0] = data.getData();
else {
if (data.getClipData() != null) {
result = new Uri[data.getClipData().getItemCount()];
for (int i = 0; i < data.getClipData().getItemCount(); i++) {
result[i] = data.getClipData().getItemAt(i).getUri();
}
} else {
result = null;
}
}
// retrieve from the private variable if the intent is null
//result = data == null ? mCapturedImageURI : data.getData();
}
} catch (Exception e) {
Toast.makeText(getApplicationContext(), "activity :" + e,
Toast.LENGTH_LONG).show();
}
mUploadMessage.onReceiveValue(result);
mUploadMessage = null;
}
}
return;
}
$handle = fopen('users.txt', "r");
$i = 0;
$result = array();
while($i<10){
fscanf($handle, "%d\t%d",$arr[$i],$arr1[$i] );
$counter = count($result);
$result[$counter] =$arr[$i];
if(!empty($arr1[$i])){
$counter = count($result);
$result[$counter] =$arr1[$i];
}
$i++;
}
echo "<pre>"; print_r($result);
文件包含
users.txt
答案 1 :(得分:1)
在运行时获取输入的另一种方法。
$arr = array();
$i = 0;
while($i<20){
fscanf(STDIN, "%d\t%d",$arr[],$arr[]);
$i++;
}
print_r(array_filter($arr));
希望,这对于从运行时获取输入值非常有用。
在上面的输出中跳过空格的更简洁的方法是
while($array = fscanf(STDIN, "%d\t%d")){
//fscanf(STDIN, "%d %d",$arr[],$arr[]);
//$i++;
list($var1,$var2) = $array;
echo $var1;
echo $var2;
}
<强>输出:强>
6125272911