1

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;

    if((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

cody
  • 137
  • 2
  • 7

2 Answers2

0

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.

iceaway
  • 1,204
  • 1
  • 8
  • 12
0

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'.

Dark Star1
  • 6,986
  • 16
  • 73
  • 121
  • thats what i want dark but how to put CTRL in condition it says undefined – cody Jul 01 '11 at 13:34
  • I would then recommend you trap a signal such as CTRL+C or something similar.. and use the signal handler to cleanly terminate the stream. This is a good example of trapping and implementing a signal handler: http://stackoverflow.com/questions/4217037/catch-ctrl-c-in-c (answer 3) – Dark Star1 Jul 01 '11 at 14:43