标签: lf

如何正确识别C中的不同行结尾?

我想标题本身就说明了一切。

我正在 Windows 7 上使用 g++ 和 Notepad++ 编写一个 C 程序,它比较文件的内容。

文件内容:

simple
file with lines
Run Code Online (Sandbox Code Playgroud)

文件具有 Windows 样式 CRLF 的行结尾。

当我使用以下代码计算文件长度时:

fseek(file, 0, SEEK_END);
size = ftell(file);
fseek(file, 0, SEEK_SET);
Run Code Online (Sandbox Code Playgroud)

我得到23

当我将行结尾更改为 Unix 格式 LF(使用 Notepad++)时,我得到22长度。

在比较两个文件时,这会产生某种问题。这就是为什么我问,是否有办法确定给定文件是否有 LF、CR 或 CRLF。

我知道我可以区分CR和LF,LF有ascii代码10,CR有ascii代码13。或者LF是'\n'而CR是'\r'。

但是当一个字符一个字符地读取文件时,即使有 CRLF,我总是得到 LF (ascii 10)。

我希望我说清楚了。谢谢。

c ascii lf line-endings

5
推荐指数
1
解决办法
1435
查看次数

在 git diff 中避免“文件末尾没有换行符”

我很确定我明白文件末尾没有换行符是什么意思。我想提供一个我很久以前创建并重新定位的分支的拉取请求(提交可能来自之前.gitattributes添加的时间)。我看到一些.java源代码文件只有更改

-}
\ No newline at end of file
+}
Run Code Online (Sandbox Code Playgroud)

无论配置如何,我都希望将这些更改从 PR 提交中排除。我想避免选择更改git difftool并扩大我对git.

问题是,我已经不明白这种变化是如何存在的,因为有一个.gitattributes

# Set the default behavior, in case people don't have core.autocrlf set.
* text=auto

# Explicitly declare text files you want to always be normalized and converted
# to native line endings on checkout.
*.c text
*.h text
*.java text
*.css text
*.js text
*.xml text
*.dtd text
*.xsl text
*.properties text
*.txt …
Run Code Online (Sandbox Code Playgroud)

git lf line-endings gitattributes

5
推荐指数
1
解决办法
8982
查看次数

如何将现有行结尾 CRLF 更改为 LF 并成功将其提交到存储库

在我的项目中,有很多种环境(Windows、Linux、Mac OS)。

有些人一开始对Git的设置错误,所以在git存储库中,有些文件与CRLF混淆了(我希望git存储库中的所有文件都是LF)。

我怎样才能快速将repo中现有的CRLF全部修复为LF(我的开发环境仅限于一些简单的编辑器 - Visual Studio,Sakura Editor,...)。一行一行地修改结尾非常耗时。

感谢任何建议。

git newline lf

5
推荐指数
1
解决办法
4493
查看次数

如何更改行尾约定?

我有我认为的简单任务.我需要创建一个在行尾使用unix LF约定的文本文件.但是,当我尝试使用pandas .to_csv完成此操作时,我最终得到了CR LF.如果我留在我的机器上并且如果我使用python进行所有操作,这不会是一个问题.但我不是.该代码将成为同事将使用的应用程序的一部分,并且该文件将被发送给第三方以进行自动化.代码如下:

df.to_csv("filename.txt", sep = '\t',line_terminator = '\n')
Run Code Online (Sandbox Code Playgroud)

它位于我的代码中间,但是当文件保存在目标文件夹中时,它总是切换回CR LF.我认为这可能与Windows" 有用 "有关.我试图使用这种类型的解决方案,没有运气.我也没有运气找到Python 的dos2unix类型包.无论如何,我可以编写以'\n'结尾的行并将其粘贴在Windows中(如果可能,使用to_csv)?

我使用的是Python 2.7和Windows 7.

编辑:我知道notepad ++,但我试图用Python编写这一切,所以我的同事只需要点击一个按钮(我将使用Tkinter并在完成后将其冻结为.exe)..txt可以是Windows格式的,但我将(同)将相同的信息写入.MAT和.ref文件.在那些文件中它需要'\n'行结束,但Windows仍然给我'\ r \n'.

python newline lf pandas

3
推荐指数
1
解决办法
890
查看次数

使用CRLF和LF文件的Git存储库?

我有一个git存储库,其文件的行结尾应该是签出的CRLF或LR,具体取决于具体的文件.例如,我希望所有*.sh文件都使用LR行结尾检出,而所有*.bat文件都使用CRLF.

我一直想把它拉下来没有结果,接下来是我的.gitattributes档案:

* text=auto
#
# Declare files that will always have CRLF line endings on checkout.
#

*.sh text eol=LF

#
# Declare files that will always have LF line endings on checkout.
#

*.bat text eol=CRLF

#
# Denote all files that are truly binary and should not be modified.
#

*.zip binary
*.tar binary
*.exe binary
*.dll binary
*/dropbear binary
*update-binary binary
Run Code Online (Sandbox Code Playgroud)

当我打开.sh文件或.bat文件时,两个行结尾都是CRLF.我究竟做错了什么?

注意:我在Windows计算机上开发.

编辑:系统,全局和本地配置

系统:

core.symlinks=false
core.autocrlf=true
color.diff=auto
color.status=auto
color.branch=auto …
Run Code Online (Sandbox Code Playgroud)

git newline lf line-endings core.autocrlf

2
推荐指数
1
解决办法
804
查看次数

/bin/sh^M:错误的解释器:由于不同的 GIT env 配置相互覆盖而导致没有此类文件或目录错误?

我编写的构建脚本在 ci/cd 管道(在 Linux 中运行)上失败,因为 build.sh 脚本以某种方式转换/保存为 CRLF 格式(基于我在线收集的内容),导致此错误:

/bin/sh^M: bad interpreter: No such file or directory
Run Code Online (Sandbox Code Playgroud)

脚本本身非常基本:

#!/bin/sh
mvn clean install
Run Code Online (Sandbox Code Playgroud)

我想确认原因是由 git 引起的,因为我在运行 git config 时看到了这一点。下面详细介绍我采取的补救措施:

  1. 专门在我的 IDE 上以 LF 格式保存(打开 build.sh 时 Intellij 显示选定的行结尾):

文件 k

  1. 特别配置 git 不要弄乱文件行结尾并转换为 CLRF(我之前收到过这个警告),所以我运行了以下命令git config --global core.autocrlf falsegit config --global core.eol lf并重新克隆了存储库。

这是我的 git 配置(本地、全局和整个系统)

本地配置:

        core.bare=false
        core.logallrefupdates=true
        core.symlinks=false
        core.ignorecase=true
        core.autocrlf=false
Run Code Online (Sandbox Code Playgroud)

全局配置:

http.sslverify=false
core.autocrlf=false
core.eol=lf
Run Code Online (Sandbox Code Playgroud)

跑步git config --list --show-origin

file:"C:\\ProgramData/Git/config"       core.symlinks=false
file:"C:\\ProgramData/Git/config"       core.autocrlf=true
file:"C:\\ProgramData/Git/config"       core.fscache=true
file:C:/Users/testUser/.gitconfig    http.sslverify=false …
Run Code Online (Sandbox Code Playgroud)

git shell lf sh

1
推荐指数
1
解决办法
2260
查看次数

C#\n,\ r \n或Environment.NewLine

正如标题所暗示的,我会是最好用\n,\r\nSystem.Environment.NewLine在我的C#应用程序换行符?

我有一个控制台,WinForms和WPF应用程序,目前所有使用\n(如果需要),但在我的控制台应用程序中,我发现当从CLi重定向输出并在记事本中打开输出时,换行符无法正确识别.我可以改变我对这个应用程序的行为,但我正在寻求那些一直使用特定方法的人的建议以及原因.

干杯!

更新:

所以普遍的共识是System.Environment.NewLine除非出现需要的特定要求\n(Unix/OS X),\r(OS 9)或\r\n(Windows)

谢谢大家,对不起!

c# wpf newline lf

0
推荐指数
1
解决办法
2005
查看次数

scanf读取每一个双错

我有以下代码:

    int main(void)
{
    double r;
    scanf("lf",&r);
    printf("%lf\n",r);
}
Run Code Online (Sandbox Code Playgroud)

我输入的任何值都会返回一个大约 40-50 位数字的无意义数字。scanf 可以很好地处理字符、整数、字符串等,但对于双精度数我会遇到这个问题。有谁能够帮助我?

c double scanf lf

-1
推荐指数
1
解决办法
440
查看次数

标签 统计

lf ×8

git ×4

newline ×4

line-endings ×3

c ×2

ascii ×1

c# ×1

core.autocrlf ×1

double ×1

gitattributes ×1

pandas ×1

python ×1

scanf ×1

sh ×1

shell ×1

wpf ×1