Thread: Converting text files from Windows to Linux in C

  1. #1
    Registered User
    Join Date
    Apr 2021
    Posts
    19

    Thumbs up Converting text files from Windows to Linux in C

    Hello everyone. For my C study exercise, I wrote a program under Linux that converts any text file from Unix format to Windows (it involves changing the line terminator from LF to CRLF and vice versa) and vice versa. Now I have brought this code back to Windows and I have recompiled it. Unfortunately, I can't do the text file conversion step from Windows to Linux (CRLF to LF) here.
    The written code is attached below.
    Can you help me solve this problem ?
    Attached Files Attached Files

  2. #2
    Registered User
    Join Date
    Sep 2020
    Posts
    226
    Can you show where you open the file to read it?

  3. #3
    Registered User
    Join Date
    Dec 2017
    Posts
    1,125
    You need to set stdout to binary mode. On Windows in text mode an output stream automatically writes a '\r' before every '\n'; an input stream in text mode ignores a '\r' before a '\n'. Binary mode doesn't transform line endings. On linux, text mode is the same as binary mode, so linux sees the '\r' before every '\n' with windows line endings.
    Code:
    #ifdef _WIN32
      #include <io.h>
      #include <fcntl.h>
    #endif
     
    // in main:
    #ifdef _WIN32
        setmode(fileno(stdout), O_BINARY);
    #endif
    The best argument against democracy is a five minute conversation with the average voter. - Churchill

  4. #4
    Registered User
    Join Date
    Apr 2021
    Posts
    19

    [solved]

    thanks john.c, I found on the Microsoft site that you have to use:
    Code:
    _setmode(_fileno(stdout), _O_BINARY);
    obviously using between
    Code:
    #if (_WIN32) and #endif
    in fact it works correctly.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 02-16-2019, 07:47 PM
  2. Replies: 18
    Last Post: 10-01-2015, 01:54 PM
  3. Replies: 4
    Last Post: 04-29-2010, 08:02 PM
  4. partition for windows and linux files
    By MisterSako in forum Tech Board
    Replies: 5
    Last Post: 09-21-2004, 05:38 PM
  5. Replies: 3
    Last Post: 05-03-2002, 05:18 PM

Tags for this Thread