从2d数组切换到1d(字符串)

时间:2016-01-15 02:11:35

标签: c arrays string malloc

我试图读取类似

的文件的输入
public class MainActivity extends AppCompatActivity {

private PdUiDispatcher dispatcher;
Button newActivity;
Switch highLow;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    initGUI();

    try{
        initPD();
        loadPD_Patch();
        loadPD_RecordPatch();
    }catch (IOException error){
        Log.e("Pd initialization error", String.valueOf(error));
    }
}

private void initGUI(){
    Switch onOffSwitch = (Switch) findViewById(R.id.onOffSwitch);
    highLow = (Switch)findViewById(R.id.highLow);
    newActivity = (Button) findViewById(R.id.newActivity);
    onOffSwitch.setOnCheckedChangeListener(
        new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                float val = (isChecked) ? 1.0f : 0.0F;
                PdBase.sendFloat("onOff", val);
            }
        }
    );
}

private void initPD() throws IOException{
    int sampleRate = AudioParameters.suggestSampleRate();
    PdAudio.initAudio(sampleRate,0,2,8,true);

    dispatcher = new PdUiDispatcher();
    PdBase.setReceiver(dispatcher);
    dispatcher.addListener("highLow", myListener);
}

private void loadPD_Patch() throws IOException{
    File dir = getFilesDir();
    IoUtils.extractZipResource(getResources().openRawResource(R.raw.simple_android_patch), dir, true);
    File pdPatch =  new File(dir,"simple_android_patch.pd");
    PdBase.openPatch(pdPatch.getAbsoluteFile());
}

private void loadPD_RecordPatch() throws IOException{
    File dir = getFilesDir();
    IoUtils.extractZipResource(getResources().openRawResource(R.raw.audio_in_android), dir, true);
    File pdPatch =  new File(dir,"audio_in_android.pd");
    PdBase.openPatch(pdPatch.getAbsoluteFile());
}

private final PdListener myListener = new PdListener() {
    @Override
    public void receiveMessage(String source, String symbol, Object... args) {
        Log.i("receiveMessage symbol:", symbol);
        for (Object arg: args) {
            Log.i("receiveMessage atom:", arg.toString());
        }
    }
    /* Receive a list from Pd */

    @Override
    public void receiveList(String source, Object... args) {
        for (Object arg: args) {
            Log.i("receiveList atom:", arg.toString());
        }
    }

    /* Receive  symbol from Pd */
    @Override public void receiveSymbol(String source, String symbol) {
        Log.i("receiveSymbol", symbol);
    }

    /* Receive float from Pd */
    @Override public void receiveFloat(String source, float x) {
        Log.e("highLow", String.valueOf(x));
        if(x == 1.0){
            highLow.setChecked(true);
        }else {
            highLow.setChecked(false);
        }
    }

    /* Recieve  bang from Pd */
    @Override public void receiveBang(String source) {
        Log.i("receiveBang", "bang!");
    }

};


@Override
protected void onResume(){
    super.onResume();
    PdAudio.startAudio(this);
}

@Override
protected void onPause(){
    super.onPause();
    PdAudio.stopAudio();
}
}

所以,我试图用每个提到的字符串获取一个字符串数组(如果需要的话,还有更多)。到目前为止,我已将输入转换为二维数组,但我似乎无法弄清楚将读取字符放入字符串数组中。

RGGG
RGYY 
YYYY

这就像打印一样:

char ch, file_name[100];
FILE *fp;

printf("Enter the name of file you wish to see\n");
scanf("%s", file_name);

fp = fopen(file_name, "r"); // read mode

if (fp == NULL) {
    perror("Error while opening the file.\n");
    exit(EXIT_FAILURE);
}

printf("The contents of %s file are :\n", file_name);

Matrix = (char **)malloc(4 * sizeof(char *));
for (int i = 0; i < 4; i++)
    Matrix[i] = (char*)malloc(8 * sizeof(char));

for (int i = 0; i < 4; i++) {
    int j = 0;
    while (j < 4 && fscanf(fp, "%c", &Matrix[i][j++]) == 1);
}

for (i = 0; i < 4; i++) {
    for (j = 0; j < 8; j++) {
        printf("Matrix[%d][%d] = %c\n", i, j, Matrix[i][j]);
    }
}

能否获得有关获取的一些见解:

Matriz[0][0] = R
Matriz[0][1] = G
Matriz[0][2] = G
Matriz[0][3] = G
Matriz[0][4] =

Matriz[0][5] = R
Matriz[0][6] = G
Matriz[0][7] = Y
Matriz[1][0] = Y
Matriz[1][1] =

Matriz[1][2] = Y
Matriz[1][3] = Y
Matriz[1][4] = Y
Matriz[1][5] = Y
Matriz[1][6] = ═
Matriz[1][7] = ═

我只是错过了一些非常明显的东西吗?感谢。

1 个答案:

答案 0 :(得分:1)

您应该跳过每行末尾的'\n'

for (int i = 0; i < 4; i++) {
    int j = 0;
    while (j < 4 && fscanf(fp, "%c", &Matrix[i][j++]) == 1);
    getc(fp);
}

或更简单:

for (int i = 0; i < 4; i++) {
    memset(Matrix[i], 0, 8);
    fscanf(fp, "%7s", Matrix[i]);
}

以这种方式转储字符串:

for (int i = 0; i < 4; i++) {
    printf("Matrix[%d] = %s\n", i, Matrix[i]);
}
相关问题