在Windows C编程中,如何覆盖文本文件中的字符串?

huangapple go评论69阅读模式
英文:

How are strings on text file overwritten in Windows C programming?

问题

我的“*file.txt*”看起来是这样的:<br>

1 |我的名字是Yugal Bishram Don
2 |


这里的 **1** 和 **2** 表示行号,并不实际存在于文件中。<br>
它表示文件包含:`我的名字是Yugal Bishram Don\n`。<br>现在我以 `“r+”` 模式打开此文件,读取文件并将指针倒回,然后写入内容如下:<br><br><pre>

    FILE *fp=fopen("file.txt", "r+");
    fscanf(fp,"%[^\n]",buffer1);
    //我对buffer1做了一些处理
    rewind(fp);
    fprintf(fp,"我的名字是Aswin Don\n");
    fclose(fp);

</pre><br>现在我的“**file.txt**”看起来是这样的:<br>

1 |我的名字是Aswin Don
2 |am Don
3 |


但我感到困惑。<br>“**我的名字是Aswin Don\n**”有 *21* 个字符,应该覆盖在文件内容的前 *21* 个字符上,即“**我的名字是Yugal Bish**”,剩下的字符即“**ram Don\n**”应该保持不变。但它并没有按照预期写入。“**ram Don\n**”变成了“**am Don**”。所以我的问题是,“**ram Don\n**”的第一个字符‘r’去哪了?
英文:

My "file.txt" looks like this:<br>

1 |My name is Yugal Bishram Don
2 |

Here 1 and 2 mean line numbers and not really exist in the file.<br>
It means the file contains : My name is Yugal Bishram Don\n.<br>Now I open this file at &quot;r+&quot; mode and read the file rewind the pointer and write content as:<br><br>
<pre>

FILE *fp=fopen(&quot;file.txt&quot;, &quot;r+&quot;);
fscanf(fp,&quot;%[^\n]&quot;,buffer1);
//i did something with buffer1
rewind(fp);
fprintf(fp,&quot;My name is Aswin Don\n&quot;);
fclose(fp);

</pre><br>Now My "file.txt" looks like this:<br>

1 |My name is Aswin Don
2 |am Don
3 |

But I am confused.<br>"My name is Aswin Don\n" is of 21 characters which should be overwritten on first 21 characters of the content of file i.e "My name is Yugal Bish" and rest of the characters i.e "ram Don\n" should be as it is. But it is not written as it is. "ram Don\n" has become "am Don". So my question is where the first character 'r' of "ram Don\n" vanished.

答案1

得分: 1

在Windows上,标准库使用\r\n组合作为换行符。因此,在编写字符串时,\r字符会被自动添加。这就解释了观察到的一个字符偏移。在Linux/Unix上,你会得到不同的结果。

以下是一个图形表示。忽略下划线 _ 填充,就好像它们不存在一样,我添加这些是为了对齐:

(org) My name is Yugal Bish_r_am Don\r\n
(new) My name is Aswin Don\r\nam Don\r\n

你可以通过使用一些十六进制编辑器(例如HxD)或者使用一个具有显示CR/LF字符选项的文本编辑器(比如Notepad++),来检查文件在之前和之后的确切内容。许多面向程序员的文本编辑器选项中都有"显示所有字符"或"显示行尾符"的选项。

英文:

On Windows, the standard library uses \r\n combination for end of line. Thus, when you write your sting, \r character is added automatically. This explains the observed one character shift. On Linux/Unix you would get a different result.

Here is a graphical representation. Ignore the _ padding as if it wasn't there, I added these for alignment:

(org) My name is Yugal Bish_r_am Don\r\n
(new) My name is Aswin Don\r\nam Don\r\n

You can check the exact contents of the file before & after by using some hex editor, for example HxD. Or using a text editor that has an option to show CR/LF characters like Notepad++. Many text editor for programmers options like "Show all characters" or "Show line endings".

答案2

得分: 0

相同的代码我运行它会覆盖为

我的名字是阿斯温·唐
拉姆·唐

直到我的名字是尤格尔·比斯,它将替换我的名字是阿斯温·唐的20个字母。
当遇到\n时,它将替换h并换行,拉姆是唐将在新行中。

英文:

Same code i ran it overwrites as

My name is Aswin Don  
ram Don

Till My name is Yugal Bis it will replace My name is Aswin Don 20 letters.
And when \n it will replace the h and puts a newline the ram is Don will be in new line.

huangapple
  • 本文由 发表于 2023年5月18日 13:00:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/76277853.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定