简体   繁体   English

C语言,读取自己的源文件并编辑源文件

[英]C language, reading own source file and editing source file

I am currently working on a project which involves reading its own source code, editing it, then recompiling it. 我目前正在研究一个项目,该项目涉及读取其自身的源代码,对其进行编辑,然后对其进行重新编译。 My question is how does the fopen function actually work? 我的问题是fopen函数实际上如何工作? Is it affected when the source file calling it is open? 调用它的源文件打开时会受到影响吗?

I have currently already written a function which reads in and finds the areas in the source code that I have to edit. 我目前已经编写了一个函数,该函数可以读取并找到我必须编辑的源代码中的区域。 When I open my source file I am able to read it in, store it in memory, iterate through it, and find what I need. 当我打开源文件时,我可以读取它,将其存储在内存中,对其进行遍历,然后找到所需的文件。 The problem is when I attempt to check if the area to edit is correct through a strcmp function call nothing happens even though it is exact (I have accounted for newlines etc.). 问题是,当我尝试通过strcmp函数调用检查要编辑的区域是否正确时,即使它是准确的,也没有任何反应(我已经考虑了换行符等)。

When I copy the source code to another .c file, and have my program read that file instead (an exact copy) it works perfectly fine. 当我将源代码复制到另一个.c文件时,让我的程序改为读取该文件(完全相同的副本),它可以很好地工作。 I am really confused why that file works and why my source code file does not. 我真的很困惑为什么该文件有效,为什么我的源代码文件无效。 They are exactly the same file. 它们是完全相同的文件。

If you are using windows, then beware that a file can usually only be open in a single process at a time . 如果您使用的是Windows,请注意, 通常一次只能在单个进程中打开一个文件

So if the source file you are trying to open is already opened in your IDE, you may not be able to open in in your program using fopen . 因此,如果要打开的源文件已经在IDE中打开,则可能无法使用fopen在程序中fopen

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM