Home>

The data (.txt) output from the experimental device is CR, so I want to replace it with LF.

Error message
Fgets () doesn't read properly if the newline character is CR.
Applicable source code
Source code
Tried

While Microsoft's Word2011 has converted newline characters, I want to express it in code.

Supplemental information (FW/tool version etc.)

Please provide more detailed information here.

c
  • Answer # 1

    C is unnecessary. Yes, shell art!

    $nkf -Lu<file.txt>newfile.txt

  • Answer # 2

    I think there are many ways, Is the following code,

    int indata;
        FILE * fpIn = fopen ("MojiCode.txt", "rb");
        FILE * fpOut = fopen ("MojiOut.txt", "wb");
        while ((indata = fgetc (fpIn))! = EOF) {
            if (indata == '\ r') indata = '\ n';
            fputc (indata, fpOut);
        }

    I didn't want to write it as it was, but it was faster to make than to explain.
    Open the file in binary, read one character at a time, and if it is CR, just change it to LF. I have only confirmed it on Windows, but the same goes for others.
    However, as a restriction, Unicode is not supported.

  • Answer # 3

    You can use a sophisticated text editor to convert line breaks, but
    If you do it with code,

    Read a file with a buffer of a certain size

    Store the data until the CR code comes in another buffer

    When CR comes, write the data up to that as a character string to another file. \ n with

    Loop to 1 until file is exhausted

    You will never have to implement it.

  • Answer # 4

    Rewriting the text to'\ n'with an editor etc. is the best you can think of,
    If you can't do that,

    I have never done it,
    fscanf (fp,"% [^ \ r]" ;, readbuff);
    Can't you do it like this?
    Anything that skips'\ r'in MSDOS text mode,
    I think you need to fopen in binary mode.
    Please handle the character limit and discarding'\ r'appropriately.

    https://en.wikipedia.org/wiki/Scanf#%E7%A9%BA%E7%99%BD%E3%80%81%E3%82%BF%E3%83%96%E3% 81% AE% E8% AA% AD% E3% 81% BF% E9% A3% 9B% E3% 81% B0% E3% 81% 97