开发者

putc in c programming in files

Im just creating my own library on file handling to make it more flexible,but I got stucked up at these point watch these below program

int filewrite(char *filename,unsigned char number)
{
    FILE *dill;

    i开发者_如何学运维f((dill=fopen(filename,"r"))==0)
        return(0);// error no such file exists  returns 0
    else
    {

        if(number==0)
        {
            dill=fopen(filename,"w");

            while(number!='x')
            {
                number=getche();
                putc(number,dill);
            }
        }
        else
        {
            dill=fopen(filename,"a+");

            while(number!='x')
            {
                number=getche();
                putc(number,dill);
            }
        }
    }
}

for instance I made the condition not equal to x so if I enter x letter it gets terminated, but I want that to be used too.but what is the condition to be put to use all letters numbers and special symbols when we are writing into a file becuase If I hit enter then it goes to next line but its not terminating and I want to use enter too but how to say that this is the EOF using putc ? Help me guys


If you want it to terminate when you press enter you can change the while loop to this:

while((number != 'x') && (number != '\r'))
{
    number=getche();
    putc(number,dill);
}

You should also close your file pointers using fclose(dill) to make sure the stream is flushed and the files aren't left opened when your program terminates.


If I understand this correctly (I'm not saying I do), You want to be able to enter any character into the file stream but also be able to terminate the stream by hitting the character 'x'. If that's what you're asking then this is not possible. You will have to come up with another way to terminate your stream. For instance you can try detecting if CTRL + another button is pressed instead of 'x'.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜