1

这是我正在处理的程序的一部分,它正在复制打开的文件,然后将其放入数组 ( file1)。但是,当我尝试打印出file1.

我曾尝试将 MAX_MAC_ADD 设置为 50,将 BIG_NUM 设置为 30000,这样它就足以支持fgets().

我打开的文件有 4 个部分,每个部分由一个“标签”分隔

例如 1one 1two 1three 1four 2one 2two 2three 2four

char    file1[MAX_MAC_ADD][BIG_NUM];
int         num_MAC = 0;

char    *Programe_Name;

int saperate_fields1(char line[])
{
int i = 0;
int f = 0;

while(line[i] != '\0' && line[i] != '\n')
{
    int c = 0;

    while(line[i] != '\t' && line[i] != '\0' && line[i] != '\n')
    {
        file1[f][c] = line[i];
        ++c;
        ++i;
    }
    file1[f][c] = '\0';

    ++f;
    if(f == (MAX_MAC_ADD-1))
    {
        break;
    }
    ++i;
}
return f,i;
}

void read_file1(char filename[])
{
//OPEN FOR READING
FILE    *fp = fopen(filename,"r");

if(fp == NULL)
{
    printf("%s: cannot open '%s'\n", Programe_Name, filename);
    exit(EXIT_FAILURE);
}
char    line[BUFSIZ];
while(fgets(line, sizeof line, fp) != NULL)
{
    saperate_fields1(line);     //SAPERATE INTO FIELDS
    num_MAC = num_MAC + 1;
    printf("%d times\n", num_MAC);
}
fclose(fp);
printf("line is:\n%s\n", line); //TO CHECK WHERE DO THE PROGRAM STOP READING
printf("file1 is:\n%s\n", file1);
}
4

1 回答 1

1

您将指向 s 数组的指针传递给char格式说明符,该说明符%s需要指向 a 的指针char。如果要打印数组数组,则char需要单独打印元素,例如:

for (int i = 0; i != end; ++i) {
    printf("file1[%d]='%s'\n", i, file1[i]);
}
于 2013-09-19T01:45:39.813 回答