- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在尝试编写一个程序,将行号添加到一个已经存在的 txt 文件中。
例如,如果文件当前是:
Hello
this is
an
exercise
然后运行代码后,它将是:
(1) Hello
(2) this is
(3) an
(4) exercise
我写了这段代码:
#include<stdio.h>
#include<conio.h>
FILE *fp;
void main()
{
int counter=1;
char newline;
fp=fopen("G:\\name.txt","r+");
if(fp==NULL)
printf("Failed to open file!");
fprintf(fp,"(%d)",counter);
newline=fgetc(fp);
while(newline!=EOF)
{
if(newline=='\n')
{
counter++;
fprintf(fp,"(%d)",counter);
}
newline=fgetc(fp);
}
printf("All done!");
getch();
fclose(fp);
}
而且输出很奇怪。
首先,它不会在文件开头打印。由于某种原因,它从文件末尾开始。发生的另一件奇怪的事情是只有第一次打印成功。
while
循环中的那些是乱码(看起来像小点,根本不像数字)
当我在 fopen 中使用“r+”时,整个数据都被删除,我只能看到 (1),然后是乱码。
如果我在 fopen 中使用“a+”,它会从文件末尾开始,然后写入 (1) 和乱码。
最佳答案
据我所知,您基本上不能在文件中间“插入”字节。相反,您将覆盖文件中的字节。因此,当您使用同一个文件进行读写时,您将“干扰自己”。我建议您为写入的输出创建一个临时文件,或者只写入标准输出,这样您就可以将输出通过管道传输到合适的位置。
关于c - C 中的 fopen 和 fprintf 没有按预期工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24604279/
在我的 C++ 代码中有几个 fprintf 语句,我已将其用于调试。由于我可能会再次需要它们,所以我暂时不想将它们注释掉。但是,我需要快速执行程序,所以我想避免它们被打印出来,因为它们目前是这样的(
巧合的是,我发现 disp(fprintf()) 打印了 fprintf 的字符串加上它所具有的字符数。我知道,disp() 是多余的,但出于纯粹的好奇心,我想知道为什么它会打印字符数,因为这可能有一
我正在运行一些基准测试以找到用 C++ 将巨大数组写入文件的最有效方法(在 ASCII 中超过 1Go)。 所以我将 std::ofstream 与 fprintf 进行了比较(参见下面我使用的开关)
假设我有两个线程将某些内容(相对较长)打印到 stderr 或 stdout,这两个流的函数是线程安全的,因为它们永远不会“交错”字符?因此,例如,如果我有“Hello, World”,我将永远不会得
我的应用程序(用 C 语言编写)必须将 500 个字符串出队并将它们写入文本文件。我可以将每个字符串出列并使用单独的 fprintf 语句将每个字符串写入一个文件,或者我可以将所有出列的字符串连接成一
我是编程新手,一开始试图用链表构建堆栈。 但是在打印列表中包含的元素时,fprintf()不起作用。 这是我的代码: #include #include //define stack and fu
我正在尝试代表一款战斗卡牌游戏: typedef struct list_of_cards* p_loc; typedef struct game game_s; struct list_of_car
我正在尝试使用以下 C 代码打印出我传入的数组。它应该以十六进制格式输出文本,每行一个,打开文件没有问题。当我第一次写它时,我的工作没有问题,我打开了输出文件,我的数组就在那里。我换了fileOutN
我正在尝试代表一款战斗卡牌游戏: typedef struct list_of_cards* p_loc; typedef struct game game_s; struct list_of_car
在我的代码中,fprintf 通过返回在 STREAM 中写入的字节数成功返回,但在实际文件中,我写入的字符串不存在。 最佳答案 输出可能只是经过缓冲。尝试使用 close() 关闭文件或在流上调用
我正在测试 fprintf() 的用法,但它不起作用。当我第一次编写代码时,我忘记在 fprintf() 中添加 \n 并且它起作用了。但是,当我在“test 1 2”的开头添加 \n 时,它停止了工
我正在开发一个生成 FDF 文件以填写 Adobe PDF 表单的程序。目前,我有各种被调用的过程,在每个过程终止之前,它打开 FDF 文件,写入其特定数据,然后关闭文件。 我试图将用于将数据写入
假设我有一个随机变量 a=1.2400 ,我想用四位有效数字打印它,即 1.240。我该怎么做? fprintf('%0.4g',a) % drops rightmost zero fprintf('
我真的需要一些帮助,我已经为此花了 6 个小时,我觉得我已经尝试了我能做的一切。这是我需要做的: 我需要将星号金字塔打印到 C 语言中的 triangle2.txt 文件中。 我知道我里面的代码将创建
C - fprintf 没有写入文件,知道为什么吗? #include #include int main(void){ FILE* pfile=fopen("/home/user-vla
如何在文件末尾为 fprintf() 用户输入的文本创建一个新行?我现在的代码是这样的: #include #include int main() { int lines; int
我的代码当前正在消耗日志文件输出的字符,但我不知道确切原因。 FILE* theLog; char filename[150]; theLog = fopen(filename, "w"); fpri
我正在尝试显示以下内容 static const char MSG[] = "Test %ld\n"; static int COUNT = 5; fprintf(stdout, "%s", MSG,
已关闭。这个问题是 not reproducible or was caused by typos 。目前不接受答案。 这个问题是由拼写错误或无法再重现的问题引起的。虽然类似的问题可能是 on-top
在下面的代码中,我尝试从套接字读取并将结果存储在文件中。 实际发生的情况是,我的客户端向我的服务器发送了一个 file.html 的 GET 请求。我的服务器找到该文件并将其内容写入套接字。最后,我的
我是一名优秀的程序员,十分优秀!