Hi ,

i have read that new line character gets converted to \r\n when writing characters to a file
and converted to new line again when reading.

but i observed one thing here.

i have written contents hello followed by return key .

and i have written a program to count the no of characters i got the result as 7.
then i tried printing character and its ascii value then its printing two characters with the same ascii value( 10 ) at the end . that mean its not converting \r\n to \n . if so the out put should be 6.

this is the program:

#include<stdio.h>
int main(int argc,char *argv[]) {
        FILE *fp;
        char ch;
        int nc=0;
        fp=fopen(argv[1],"r");
        if(!fp)
                printf("file doesnot exist\n");
        else {
        while((ch=fgetc(fp))!=EOF)
        {
                printf(" < %c > and < %d > \n", ch , ch );
                nc++;
        }
        printf("no of chars=%d",nc);
        }
return 0;
}

i many of the forums many ppl mentioned that there is no difference between text and binary on *nix systems.

the out put i got is:

< h > and < 104 >
< e > and < 101 >
< l > and < 108 >
< l > and < 108 >
< o > and < 111 >
<
> and < 10 >
<
> and < 10 >
no of chars=7

i am running programs on ubuntu 9.04
please help..

> i have read that new line character gets converted to \r\n when writing characters to a file
> and converted to new line again when reading.
You heard wrong then.
On *nix systems, there is no translation at all.

Check again, your input file simply has two newlines at the end of it.

Try od -Ax -t x1z file.txt

> i have read that new line character gets converted to \r\n when writing characters to a file
> and converted to new line again when reading.
You heard wrong then.
On *nix systems, there is no translation at all.

Check again, your input file simply has two newlines at the end of it.

Try od -Ax -t x1z file.txt

i have done as you suggested.

i got the results as

spark@spark-desktop:~/Cprogs/Files$ od -Ax -t x1z noc.txt
000000 68 65 6c 6c 6f 0a 0a >hello..<
000007
but i dont understand what does that mean and result as well.
i am sure that i am using only one enter at the end of line.
when i dont press enter key and try to find out the no of characters its giving 6 for the input Hello
how is that possible ? is it taking on \n as default. ?
i am working on ubuntu 9.04

See those two 0a 0a ?
Those are TWO newlines.

Use your favourite text editor (or VI), and delete one of the newlines at the end of the file.
No more confusion, the file has one newline, represented by one \n

Geek Joke Moment
VI VI VI - the editor of the beast
:)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.