英文:
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 "r+"
mode and read the file rewind the pointer and write content as:<br><br>
<pre>
FILE *fp=fopen("file.txt", "r+");
fscanf(fp,"%[^\n]",buffer1);
//i did something with buffer1
rewind(fp);
fprintf(fp,"My name is Aswin Don\n");
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论