简体   繁体   English

具有模式或格式的C scanf

[英]C scanf with pattern or format

i have to scanf the matrix to the dynamic 2D array (use malloc) with unknown size in this format or pattern: 我必须以这种格式或模式将矩阵扫描到具有未知大小的动态2D数组(使用malloc):

%d, %d, %d ... %d,%d,%d ...

1, 2, 3 4, 5, 6 7, 8, 9 1,2,3 4,5,6 7,8,9

Therefore, every continuing line MUST be as long as the first one. 因此,每条连续线必须与第一条线一样长。 If on the first line are 8 interegrs: 如果在第一行是8个句号:

5, 6, 7, 8, 5, 0, 4, 5 5、6、7、8、5、0、4、5

then every next line MUST contain the same count of integers.. 那么每一行都必须包含相同数量的整数。

Then i can press CTRL+D to end standart input and continue with other calculations. 然后,我可以按CTRL + D结束标准输入并继续进行其他计算。

Well, i have really NO idea, how use scanf to read to dynamic array data in this format and use them. 好吧,我真的不知道如何使用scanf读取这种格式的动态数组数据并使用它们。 I can malloc the 2D array an use it, but I don't know, how to do this. 我可以分配2D数组以使用它,但我不知道该怎么做。 I hope, it's easy to understand and I appreciate any help. 我希望这很容易理解,感谢您的帮助。

Thank you very much and I'm sorry for my terrible english.. Nikolas Ch. 非常感谢,对不起,我的英语不好。.Nikolas Ch。

scanf is NOT good for reading input where newlines are meaningful (in particular, when the difference between a newline and a space is important). scanf用于在换行有意义的情况下读取输入(特别是当换行和空格之间的差异很重要时)。 That's because it treats all whitespace the same (it mostly just skips it) with nothing different between spaces and newlines. 这是因为它将所有空白都相同(大多数情况下只是跳过它),而空格和换行符之间没有任何区别。

If you need to read data where newlines are meaningful (eg, if you want to verify that each line of input has the same number of numbers, as you describe), then you really need to read entire lines (with fgets) and then read numbers out of those lines with sscanf. 如果您需要在换行有意义的地方读取数据(例如,如果您要验证输入的每一行都具有相同数量的数字,如您所描述的),那么您确实需要先读取整行(使用fgets),然后再读取用sscanf在那些行中编号。 You may be able to use something like: 您可能可以使用类似:

char line[2048];
int i = 0;

while (fgets(line, sizeof(line), input)) {
    char *p = line;
    int j, l;
    while (sscanf(p, "%d %n", &yourArray[i][j], &l) > 0) {
        j++;
        p += l;
        if (*p == ',') p++; }
    i++; }

You may use this code: 您可以使用以下代码:

 for (int i = 0; i < column; i++)
 {
     for (int j = 0; j < row; j++)
     {
         scanf("%d", &yourArray[i][j]);
     }
 }

Regarding your statement: I can malloc the 2D array an use it, but I don't know, how to do this . 关于您的声明: 我可以分配2D数组以使用它,但我不知道如何执行此操作 I was not sure if you were asking help in mallocing a 2D array of numbers... If so, here is a way to create and free memory for an array of numbers. 我不确定您是否正在寻求帮助来分配2D数字数组...如果是,这是一种为数字数组创建和释放内存的方法。 (this one is for int , but can easily be modified for any numeric type) (这是给int ,但是可以很容易地为任何数字类型修改)

This, coupled with the other suggestions should provide what you need. 结合其他建议,可以满足您的需求。

int ** Create2D(int **arr, int cols, int rows)
{   
    int space = cols*rows; 
    int    y;

    arr   = calloc(space, sizeof(int));
    for(y=0;y<cols;y++)
    {
        arr[y] = calloc(rows, sizeof(int)); 
    }
    return arr;
}

void free2DInt(int **arr, int cols)
{
    int i;
    for(i=0;i<cols; i++)
        if(arr[i]) free(arr[i]);
    free(arr);  
}

Use it like this: 像这样使用它:

#include <ansi_c.h>
int main(void)
{
    int **array=0, i, j;
    array = Create2D(array, 5, 4);//get the actual row/column values from reading the file once.
    for(i=0;i<5;i++)
        for(j=0;j<4;j++)
            array[i][j]=i*j; //example values for illustration
    free2DInt(array, 5);

    return 0;

}

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

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