简体   繁体   English

在文件中写入问题

[英]problem writing in file

FILE *ExcelFile = fopen("testdata.csv","w");
if (ExcelFile == NULL)
    return -1;
fprintf(ExcelFile,"1 2 3");

fprintf(ExcelFile,"\n");
fclose(ExcelFile);

//==============================================
FILE *fa = fopen("testdata.csv","w");
if (fa == NULL)
    return -1;
fseek (fa, 6 , SEEK_SET );
fprintf(ExcelFile,"a");


fclose(fa);

in the code i have write 1 2 3 in the file and also inserted '\\n' (required for the program) now i want to place a after 3 like 1 2 3 a but hte problem iam facing is that my code erase all char an simply write a . 在代码中,我已经在文件中写入了1 2 3,并且还插入了“ \\ n”(该程序必需),现在我想像1 2 3 a一样放置3之后,但是我面临的问题是我的代码擦除了所有字符只需写一个。 help required .thanks 需要帮助。谢谢

Every time you open your file, you are opening it as a 'w' option. 每次打开文件时,都将其作为“ w”选项打开。 In C, this has a specific meaning : start writing at the beginning of the file. 在C语言中,这具有特定含义:从文件开头开始写入。

For your first write of the file, this is okay, but for every subsequent write, you're overwriting your previous content. 第一次写入文件是可以的,但是随后的每次写入都将覆盖先前的内容。

Solution Use the 'a' attribute instead here. 解决方法请在此处使用'a'属性。 Like this: 像这样:

FILE *fa = fopen("testdata.csv","a");

See more information about fopen here... 在此处查看有关fopen的更多信息...

EDIT 编辑

Reading your comments, I understand that when you write again, the next thing starts on a new line. 阅读您的评论后,我了解到您再次写信时,下一件事将从新的一行开始。 This is because of your initial write 1 2 3 \\n (The \\n makes a new line). 这是因为您最初写了1 2 3 \\n\\n换行了)。

To correct this you can : 要更正此问题,您可以:

  • Don't write a '\\n' at all. 根本不要写“ \\ n”。 OR 要么
  • Read the entire file first, rewrite it without the \\n and then write your new a and \\n 首先读取整个文件,不使用\\n重写它,然后写入新的a\\n

First of all, a CSV file should have "comma separated values", as the name indicates. 首先,如名称所示,CSV文件应具有“逗号分隔的值”。 So, rather than "1 2 3", you'd better have "1,2,3" or "1;2;3". 因此,最好不要使用“ 1,2,3”或“ 1; 2; 3”,而不是“ 1 2 3”。

Now, there are multiple ways of opening a file : you're only using the "w" as "writing" mode. 现在,有多种打开文件的方式:您仅使用“ w”作为“写入”模式。 When you're in writing mode, you're erasing your file. 处于写入模式时,正在擦除文件。 You could use the "a" as "add" mode, which mean that everything will be put after it. 您可以将“ a”用作“添加”模式,这意味着所有内容都将放在它后面。

You could also : 您还可以:

1°) First read your file with a "r" mode and store it in memory. 1°)首先以“ r”模式读取文件并将其存储在内存中。 Then, close it. 然后,关闭它。

2°) Then, open your file with a "w" mode, copy what you stored, and then make your addendum. 2°)然后,以“ w”模式打开文件,复制存储的内容,然后制作附录。 Then, close it. 然后,关闭它。

(There is a "reading and writing mode" too, check the link provided by another answer ; but this solution can easily be broken in small pieces, to have small functions doing each their part of the job). (也有一种“读写模式”,请检查另一个答案提供的链接;但是此解决方案可以很容易地分解为小部分,以便在每个工作中都有小的功能)。

You specified w for fopen() , which means "create the file or open for overwrite if already exists". 您为fopen()指定了w ,这意味着“创建文件或打开以供覆盖(如果已经存在)”。

Therefore, your second call to fopen() cleared the file's contents. 因此,第二次调用fopen()清除文件的内容。

Use a , for: "create the file, or append to it if already exists". 使用a ,为“创建文件,或者追加到它,如果已经存在”。

You want mode "r+". 您需要模式“ r +”。 Using mode "a", all writes will go to the end of the file. 使用模式“ a”,所有写入将转到文件末尾。

fopen (filename,"a") fopen(文件名,“ a”)

a = append a =附加

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

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