简体   繁体   English

按文件复制数组中的元素

[英]Copying elements from array by file

I am fetching the data from file line by line and storing them in word array 我正在逐行从文件中获取数据并将其存储在word数组中

  • i want to copy the whole word into another array like if wrd has assssh in current iteration i want it whole to be copied to arr array 我想将整个单词复制到另一个数组中,例如,如果wrd在当前迭代中具有assssh,我希望将其整体复制到arr数组中
  • but what is been doing the first element in each iteration is copied in arr[i] but that is not what i want 但是每次迭代中正在做的第一个元素的内容被复制到arr [i]中,但这不是我想要的

i want the whole word to be copied at each index, actually after that i am sorting the word according to first alphabet in each array please help out 我希望整个单词在每个索引处都被复制,实际上之后,我要根据每个数组中的第一个字母对单词进行排序,请帮忙

    while (fscanf(file, " %1023s", wrd) == 1) {
        printf("%s\n", wrd);
    //Pushing the result into vector
    //strcpy(arr,wrd);
    arr[i]=wrd[0];
    i++;
    counter++;

  }
 bubbleSortAWriteToB(arr, s_arr);

Assumeing 'arr' is a two dimentional array and 'wrd' is a character array, your code should look something like below in order to achieve what you want: 假设“ arr”是一个二维数组,而“ wrd”是一个字符数组,则您的代码应如下所示,以实现所需的功能:

while (fscanf(file, " %1023s", wrd) == 1) {
    printf("%s\n", wrd);
    // calculating length of the wrd array
    int wrd_length = (int)( sizeof(wrd) / sizeof(wrd[0]); 
    int idx = 0;
    while(idx < wrd_length) {
       arr[i][idx] = wrd[idx];
       idx++;        
    }
    i++;
    counter++;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM