- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的输入文件包含以下格式的产品集合:
name
price
symbol
示例文件是:
Ball
6.24
u
我想读取文件,将文本解析为struct
,并使用相同的元素重写文件,但不包含带有符号u
的元素。这是我的代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
struct Product
{
char name[30];
char amount;
double price;
};
int main()
{
FILE * fp;
char * line = NULL;
size_t len = 0;
ssize_t read;
struct Product products[100];
fp = fopen("magazyn.txt", "r+");
if (fp == NULL)
exit(EXIT_FAILURE);
int counter = 0;
int amount = 0;
while ((read = getline(&line, &len, fp)) != -1) {
if (counter != 0 && counter % 3 == 0)
{
counter = 0;
amount++;
}
if (counter % 3 == 0) {
strcpy(products[amount].name, line);
}
if (counter % 3 == 1)
products[amount].price = atof(line);
if (counter % 3 == 2)
products[amount].amount = line[0];
counter++;
}
truncate("magazyn.txt", 0);
for(int i=0; i<amount; i++)
{
if (products[amount].amount != 'u')
{
fprintf(fp, "%s\n%lf\n%c\n",
products[amount].name,
products[amount].price,
products[amount].amount);
}
}
fclose(fp);
if (line)
free(line);
return 0;
}
由于某种原因,我得到 \00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00球
。为什么会发生这种情况?
最佳答案
代码正在从文件中读取一些行,然后将新行附加到同一文件(是的,它会附加,请参见下文)。
调用truncate()
将文件系统上的文件大小设置为0。但它不会重置已打开文件的当前文件偏移量。
因此,在写入新行时,它们会附加在先前内容的末尾,同时先前的内容会被 0 替换:它正在创建一个稀疏文件,一个有洞的文件,洞用 0 填充。
关于c - Fputs 在开头插入无效数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54638482/
以下编程在“fputs”的第一个实例处崩溃,尝试将“fname”打印到文件 Member_inf。我还尝试打印常量字符串“abcdefg”,但仍然失败。到底做错了什么?看来是文件的初始化。如果可能的话
我的输入文件包含以下格式的产品集合: name price symbol 示例文件是: Ball 6.24 u 我想读取文件,将文本解析为struct,并使用相同的元素重写文件,但不包含带有符号u的元
总的来说,我对 C 语言和内存分配还很陌生。基本上我想做的是复制未知大小的输入文件的内容并使用递归反转其内容。我觉得我已经非常接近了,但是当我尝试放入我认为是文件反转内容的内容时,我不断遇到段错误(我
fseek(ofile, 13, SEEK_SET); fputs("\t", ofile); do { ch = getc(ofile); printf("%c", ch);
当运行下面的代码时,我没有得到任何输出,但我不知道为什么。 # include int main() { fputs("hello", stdout); while (1);
我试图在文件中写入数组的全部内容。这是我的代码: fichier = fopen(patch, "w+"); if (fichier != NULL) { if(methode=
#include int main() { FILE *fp; char str[60]; char data[50]; char * pch; /* opening
我在完成下面的作业时遇到了问题。每当我尝试将 fputs 参数合并到一行中时,错误都会显示 fputs 中的参数过多。 我想做的是在每个子文件中创建一个输出,其中包括时间以及一行中的文本,但下面的代码
我有以下代码: #include #include #include using namespace std; int main(){ size_t size; char *li
int main(int argc, char *argv[]) { FILE *datafile, *outputfile; char c, *line, *line1, *line
在这个程序中,我的 edit() 函数无法正常工作。当我尝试写一些东西时,它只会删除整个内容,而在 appendText() 中只有一个单词被附加?有没有办法将整个字符串写入文件? #include
我目前正在尝试用 C 语言创建一个数据库,使用 .txt 文档作为存储所有数据的地方。但是我无法让 fputs() 换行,所以我的程序在此 .txt 文档中写入的所有内容都只在一行上。 int
我有以下简单代码: #include int main(){ char buffer[20] = "abc"; FILE *pFile; pFile
我终于让自己去看了一些 Linux 代码。我现在正在查看 ls.c。 在底部的函数 usage() 中,我发现了很多这样的语句: fputs (_("\ List information about
不可能执行类似 fputs(4, fptOut); 的操作,因为 fputs 不喜欢整数。我该如何解决这个问题? 执行 fputs("4", fptOut); 不是一个选项,因为我正在使用计数器值。
或等效地, 为什么 puts() 会追加一个尾随换行符? 当输出字符串时,可能会用到两个类似的函数:fputs() 和puts()。 puts() 在输出时自动在末尾添加一个新行,但 fputs()
我在 Windows 上遇到文件写入失败的问题。我将它简化为这个例子: FILE* f = fopen("test.out", "r+b"); fseek(f, -1, SEEK_END); // o
为什么 fputs() 或 fprintf() 没有写入 %s 的 file.txt ?我尝试了这两个函数,但它们似乎没有写入文件。但是,当对 %d 使用相同的函数时,它目前确实有效。至于预期输出与当
为什么 fputs() 或 fprintf() 没有写入 %s 的 file.txt ?我尝试了这两个函数,但它们似乎没有写入文件。但是,当对 %d 使用相同的函数时,它目前确实有效。至于预期输出与当
假设test.txt是这样的: aaaaa aaaaa aaaaa 测试代码是这样的: #include #include #include int main() { FILE *f = f
我是一名优秀的程序员,十分优秀!