Как читать целые числа из файла построчно и обрабатывать в разных связанных списках? [на удерживании]

Для API 23+ вам необходимо запросить разрешения на чтение и запись, даже если они уже находятся в вашем манифесте.

// Storage Permissions
private static final int REQUEST_EXTERNAL_STORAGE = 1;
private static String[] PERMISSIONS_STORAGE = {
        Manifest.permission.READ_EXTERNAL_STORAGE,
        Manifest.permission.WRITE_EXTERNAL_STORAGE
};

/**
 * Checks if the app has permission to write to device storage
 *
 * If the app does not has permission then the user will be prompted to grant permissions
 *
 * @param activity
 */
public static void verifyStoragePermissions(Activity activity) {
    // Check if we have write permission
    int permission = ActivityCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE);

    if (permission != PackageManager.PERMISSION_GRANTED) {
        // We don't have permission so prompt the user
        ActivityCompat.requestPermissions(
                activity,
                PERMISSIONS_STORAGE,
                REQUEST_EXTERNAL_STORAGE
        );
    }
}

AndroidManifest.xml



Для официальных документацию о запросах разрешений для API 23+, проверьте https://developer.android.com/training/permissions/requesting.html

-1
задан Santosh Kumbhar 27 June 2019 в 18:58
поделиться

2 ответа

Рассмотрим этот цикл

ifstream myFile;
myFile.open("lists.txt");
// the loop is supposed to read each line ofrom the file
// I'll assume that 'line' is a std::string
while (getline(myFile, line ))
{
    istringstream ss(line);
    while (ss)
    {
        // What type is 'a'? 
        ss >> a;
        list1.addNode(a);

        // Even assuming that 'a' is a char, std::getline extract the newline fromn the input,
        // but it doesn't append it to the string. So there is no '\n' in 'ss'
        if (a == '\n')
            break;

        // Now, even if the last element in the row has been extracted, 'ss' is still good
        // It's only the next attempt of 'ss >> a' which fails
    }

    // The following will never execute, because either the line has been consumed
    // entirely in the previous loop, or some previous read has failed
    while (ss)
    {
        ss >> a;
        list2.addNode(a);
        if (a == '\n')
        break;
    }

    // The loop isn't really a loop, it breaks unconditionally
    break;  
}

. Вы должны проверить потоки после попытки извлечения.

while (getline(myFile, line ))
{
    istringstream s1(line);
    while (s1 >> a)
    {
        list1.addNode(a);
    }

    if ( !getline(myFile, line ) )
         break;

    istringstream s2(line);
    while (s2 >> a)
    {
        list2.addNode(a);
    }
}
0
ответ дан Bob__ 27 June 2019 в 18:58
поделиться

его частично решенный, но берущий последнее целое число строки дважды. У кого-либо есть решение?

ifstream inputFile;
inputFile.open("lists.txt");

if( getline(inputFile, line);
    istringstream list1_stream(line);
    while (list1_stream)
    {
        list1_stream >> a;
        list1.addNode(a);
    }
    getline(inputFile, line);
    istringstream list2_stream(line);
    while (list2_stream)
    {
        list2_stream >> a;
        list2.addNode(a);
    }

файл содержит:

1 2 3 8

2 4 7 9

вывод:

1 2 3 8 8

2 4 7 9 9

0
ответ дан Santosh Kumbhar 3 July 2019 в 04:10
поделиться
Другие вопросы по тегам:

Похожие вопросы: