write funtion and CR/LF issue

This is a discussion on write funtion and CR/LF issue within the C Programming forums, part of the General Programming Boards category; here's the point, as the help from TC says: "On text files, when write sees LF (linefeed) character, it outputs ...

  1. #1
    Registered User
    Join Date
    May 2007
    Posts
    3

    write funtion and CR/LF issue

    here's the point, as the help from TC says:

    "On text files, when write sees LF (linefeed) character, it outputs a CR/LF (carriage-return/linefeed)pair"

    and here is a little example from my code:
    Code:
                     i=0;
    	 do
    	 {
    	    read(fd,&c,1);
    	    if(c!='\n' || !eof(fd))
    	    {
    		x.telefono[i]=c;
    		i++;
    	    }
    	 }while(c!='\n' && !eof(fd));
    	 x.telefono[i]='\0';
    
    	 if(strcmp(x.nombre,nom)!=0)
    	 {
    	    int fd2=open("temp.txt",4);
    	    if(fd2<0)
    	       fd2=creat("temp.txt",751);
    	    else
    	    {
    	       lseek(fd2,0,2);
    	       write(fd2,"\n",1);
    	    }
    	    write(fd2,&x.nombre,strlen(x.nombre));
    	    write(fd2,"|",1);
    	    write(fd2,&x.direccion,strlen(x.direccion));
    	    write(fd2,"|",1);
    	    write(fd2,&x.telefono,strlen(x.telefono));
    	    close(fd2);
    	 }
    so my problem is this:

    In the last part (x.telefono), i dont want that the function write automatically put the CR character on my file, so anyone have an idea??

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,185
    Write in binary mode?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    May 2007
    Posts
    3
    i cant use binary because this practice is about archives with a delimitator structure (thats way i am writing the '|' character).

    I tried to fix it not writing the LF character (see code):
    Code:
    i=0;
    	 do
    	 {
    	    read(fd,&c,1);
    	    if(c!='\n' || !eof(fd))
    	    {
    		x.telefono[i]=c;
    		i++;
    	    }
    	 }while(c!='\n' && !eof(fd));
    	 //x.telefono[i]='\0';
    but there is not change, it works the same with or without the LF character, thats is why i am confused.

  4. #4
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    I don't understand what this is supposed to do.
    Code:
            read(fd,&c,1);
            if(c!='\n' || !eof(fd))
    	{
    	    x.telefono[i]=c;
    	    i++;
    	}
    it shure doesn't skip any '\n'
    Kurt

  5. #5
    Registered User
    Join Date
    May 2007
    Posts
    3
    Code:
                     do
    	 {
    	    read(fd,&c,1);
    	    if(c!='\n' || !eof(fd))
    	    {
    		x.telefono[i]=c;
    		i++;
    	    }
    	 }while(c!='\n' && !eof(fd));
    this part is filling x.telefono with the data from the file, telefono is the las part of all my registry, so if a CR is write in the end of the archive it mess it all because when i capture a new registry it "jumps" two lines and my file becomes useless... if you like a can post the full "delete" function i have.

  6. #6
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    Are you worried about the CRLF pair or an extra line at the end of your "registry" ?

    Different OSs have different ideas about what bytes should represent a newline. When you open a file in text mode, you don't have to worry about it - to the program a newline will be '\n'. You'll only notice the difference if you open the file in binary mode.

    So, ask yourself: Is your registry a binary file, or a text file? If it's "archives with a delimitator structure", which sounds like lines of text with itermittent | characters, then it's probably a text file. Open it in text mode, and do not worry about the extra character being written - this is the way it should work.

    If you're writing a '\n' after every record in your registry, then you might be ending up with a blank line at the end. If this is the case, then simply don't write a '\n' after the last line of the file. (Or handle the trailing blank line whenever you load the file)

    EDIT: Also:
    Code:
    open("temp.txt",4);
    Why are you passing '4'? From the man pages:
    Values for oflag are constructed by a bitwise-inclusive-OR of flags from the following list, defined in <fcntl.h>.
    None of the flags on my system can be OR'd to equal '4', so I'm in the dark as to what you're passing.

    Still confused as to what your problem/question is...
    Last edited by Cactus_Hugger; 05-05-2007 at 02:28 PM.
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21