Find the answer to your Linux question:
Results 1 to 6 of 6
Hello everybody I'm porting a c/c++ applications from windows to linux, and I'm having problems with the open() function. I opened the files as binary in windows, but in linux ...
  1. #1
    Just Joined!
    Join Date
    May 2007
    Posts
    3

    Question open files as binary...

    Hello everybody

    I'm porting a c/c++ applications from windows to linux, and I'm having problems with the open() function. I opened the files as binary in windows, but in linux I cannot do that. The O_BINARY flag does not exist... :o

    When I started checking out why, I found a nice bit of information on the fopen() function that rocked my world...

    (...)
    The mode string can also include the letter ``b'' either as a last character or as a character between the characters in any of the two-character strings described above. This is strictly for compatibility with C89 and has no effect; the ``b'' is ignored on all POSIX conforming systems, including Linux.
    (...)

    I have a quite large application and I'm having some problems because the files are opened as text and the read fails sometimes (other times it dosn't . don't know why )

    Any ideas how I can give this problem a twist?

    well, thanks for all the help I hope is coming my way

    André

  2. #2
    Linux Enthusiast
    Join Date
    Aug 2006
    Posts
    631
    What problems? How do you read the files? Post some snippet of your code.
    Here an example how you can read a textfile:

    Code:
    int main()
    {
    	FILE *inp;
    	char readbuf[256];
    
    	inp = fopen("textfile", "r");
    
    	while(fgets(readbuf, 255, inp) != NULL) {
    		printf("%s\n", readbuf);
    	}
    
    	fclose(inp);
    	return(0);
    }
    Note that in DOS files a line end with CRLF instead of LF.
    The following command convert a DOS file to Linux format:

    Code:
    sed 's/^M$//' input.txt > output.txt
    Press Ctrl-V then Ctrl-M to get the ^M.

    Regards

  3. #3
    Linux Enthusiast
    Join Date
    Aug 2006
    Posts
    631
    I misread your question, this is an example of reading and writing a binary file:

    Code:
    #include <stdio.h>
    #include <fcntl.h>
    
    #define PERMS 0666
    
    int main (int argc, char *argv[])
    {
    	int inp, outp;
    	char readbuf[BUFSIZ];
    	int n;
    
    	inp = open("bin_file1", O_RDONLY, 0);
    	outp = creat("bin_file2", PERMS);	
    
    	while((n = read(inp, readbuf, BUFSIZ)) > 0) {
    		if(write(outp, readbuf,n) != n)
    		    perror("Write");
    	}
    
      	return(0);
    }

    Regards

  4. #4
    Just Joined!
    Join Date
    May 2007
    Posts
    3
    Franklin52

    thanks for your replies but I'm doing the same thing as in you example code.

    I defined the flags as

    i
    Code:
    nt flags[4][3] =     
           {
           {O_RDWR|O_CREAT,S_IREAD|S_IWRITE,SH_DENYRW|S_IRWXU},
           {O_RDONLY,S_IREAD,SH_DENYNO|S_IRWXU},                           {O_WRONLY|O_TRUNC|O_CREAT,S_IREAD|S_IWRITE,SH_DENYWR|S_IRWXU},
     {O_WRONLY|O_APPEND|O_CREAT,S_IREAD|S_IWRITE,SH_DENYWR|S_IRWXU}
            };
    and used open
    Code:
    handle=open(nome,flags[my_mode][0],flags[my_mode][2],flags[my_mode][1]);
    As much as I've read so far, in Linux all files are handled as text and there is no support for binary files... in this page http://djgpp.linux-mirror.org/v2faq/faq9_4.html It is stated that:

    Code:
    Note that the above distinction between binary and text files is written into the ANSI/ISO C standard, so programs that rely on the Unix behavior whereby there's no such distinction, are strictly speaking not portable.
    and adding to all my problems

    Code:
    Well the difference is that text files contain lines (or records) of text
     and each of these has an end-of-line marker automatically appended
     to the end of it whenever you indicate that you have reached the
     end of a line. There is an end of line at the end of the text written
     with the C fwrite() function or in C++ when you <<endl. Binary files
     are not broken up into separate lines or records so the end-of line
     marker is not written when writing to a binary file.
    
    Reading from a text file or binary file is different too as a text file is
    automatically broken up into separate records as it read in based on
    the location of the end-of-line markers.
    So my problem persists(?) how do handle this...

    regards

  5. #5
    Linux Enthusiast
    Join Date
    Aug 2006
    Posts
    631
    Your open statement doesn't use proper arguments, try:

    Code:
    handle=open(nome, O_RDONLY, 0);
    To read random from the file you can use the functions fseek, ftell, rewind, fgetpos, fsetpos.
    Check the man pages.

    Regards

  6. #6
    tpl
    tpl is offline
    Linux User
    Join Date
    Jan 2007
    Location
    cleveland
    Posts
    452
    welcome to the forum

    > the files are opened *as*text*

    the files are opened--text, binary, whatever--it's all the same in *nix

    quoting Kernighan & Ritchie: "...our fopen does not recognize the "b"
    that signals binary access, since that is meaningless on UNIX
    systems..." K&R,2nd ed'n,p.178

    > the read fails sometimes...

    well, I think you'll have to look elsewhere for your problem.
    the sun is new every day (heraclitus)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...