什么时候printf会返回一个负数?

时间:2016-10-22 11:42:27

标签: c return printf

我不太确定C中的函数printf何时返回负数。 我知道printf会在打印字符串失败时返回一个负数,很可能是-1。 但是我想知道为什么以及何时打印字符串失败。 比如我的代码:

    #include<stdio.h>
   #include<stdlib.h>
   #include<string.h>
#pragma pack(1)
typedef struct{
    char name[14];
    int age;
    int yearofbirth;
}person;
int print(person var)
{
    if(printf("%s\n", var.name)<0)
    {
        if(printf("%d\n", var.age)<0)
        {
           if(printf("%d\n\n", var.yearofbirth)<0)
           {
                return 1;
           }
        }

    }
    return 0;
}
int main(void)
{
    int i=0;
    person thing;
    FILE* file=fopen("Data.txt", "r");
    if(file)
    {
        long amountofstruct = 0;
        char* z=(char*)malloc(sizeof(char)*200);
        while(fgets(z,100,file)!=NULL)
        {
            amountofstruct++;
        }
        fseek(file,0,SEEK_END);
        free(z);
        int k=0;
       /* while(k<amountofstruct)
        {
            fseek(file,sizeof(person)*k,SEEK_SET);
            fread(&function[k], sizeof(person),1,file);
            k++;
        }
        int szt;
        printf("%p",function);
        for(szt=0;szt<amountofstruct;szt++)
        {
            printf("Person %d:\tName:%s\t Age:%i\t Year Of Birth:%i\n",szt+1,function[szt].name,function[szt].age,function[szt].yearofbirth);
        }*/
        printf("thesizeofstruct:%ld\n",sizeof(person));
        printf("%ld",amountofstruct);
//thing.name = {' ',' ',' ',' ',' ',' ',' ',' ',' ','\0'};

        char *l = (char*)malloc(11);
        while(k<amountofstruct)
        {

            fseek(file,sizeof(person)*k,SEEK_SET);
            if(fread(&thing, sizeof(thing), 1, file)!=0)
            {
                thing.name[13]='\0';
               // printf("sizeof(thing string)%lu\n sizeof(thing)%lu\n", sizeof(thing.name),strlen(thing.name));
                if(print(thing)==0)
                {
                    printf("\nThe thing failed, sorry");
                    return 0;
                }
                //char *p = strchr(thing.name, ' ');
               // *p='\0';
             //   strcpy(l,thing.name);
                //printf("Person %d: \t Name: %s\t Age: %d\t Year Of Birth: %d\n", k+1, l, thing.age, thing.yearofbirth);
        //        thing.name[0]='\0';
          //      thing.age=0;
           //     thing.yearofbirth=0;
                k++;
            }
        }
        free(l);
    }
    else
    {
        perror("The file could not be opened");
    }
    getchar();
    return 0;
}

printf打印函数print中的整数var.age和var.yearofbirth总是返回一个负数。这条线

printf("%s\n", var.name);
功能打印中的

成功,但就是这样。但在我的Data.txt中,每个人的年龄和出生年份都非常明确地提供如下。

    Name   Age   YearofBirth

那么为什么printf会返回一个负数呢? 谢谢,

1 个答案:

答案 0 :(得分:0)

例如,如果您在调用printf(3)之前-1fclose(3)关联的FILE描述符,

printf()将返回stdout。另一个例子是,如果某个其他进程已撤销您的文件描述符,或者磁盘是否已满,或者您是否超出磁盘配额,或者套接字因某种原因而关闭(情况printf(3)与套接字关联) )。有很多事情可以使-1函数族返回printf()。通常,它与内部执行的系统调用关联的外部事件相关联。假设您对管道printf(),并且读取过程(接收数据的过程)突然死亡。您的write(2)将无法write(2)缓冲区数据并从-1收到错误,然后它将返回errno,您必须检查{{1}对于错误的实际原因。

相关问题