[英]Reading and writing from a file in C
我被困在 C 中的一些基本文件处理代码中。基本上我想解析一个输入文件以获取一些信息,然后将它放在另一个文件中(更多信息在输入文件中)。 我为此使用了在线 gcc 编译器,一切都很棒。 当我enter code here
,用 gcc 命令编译 cmd 中的代码,输出文件为空。
#include <stdio.h>
#include <string.h>
int main() {
char c[1000];
FILE *fptr,*resultfp;
if ((fptr = fopen("inputfile.txt", "r")) == NULL) {
printf("can't be opened");
}
if ((resultfp = fopen("outputfile.txt", "w")) == NULL) {
printf("can't be opened");
}
while(strcmp(c,"END OF FILE")){
fscanf(fptr, "%[^\n]", c);
fseek(fptr, 1,SEEK_CUR);
if(strstr(c,"Example name") || strstr(c,"Example description") )
{
fscanf(fptr, "%[^\n]", c);
fprintf(resultfp,"%s\n", c);
}
}
fclose(fptr);
fclose(resultfp);
return 0;
}
这是输入文件:
This is an example
Example name:
example1
Example description:
description1
Example name:
example2
Example description:
description2
I want to parse this file
In order to get example name and example description
END OF FILE
首先,即使 OP 代码工作没有问题,输入文件也有问题。 输入文件中的第二个条件的任何实例都不会被读取,也不会发布到输出文件中。 即代码中的搜索条件是:
"Example description"
但 inputfile.txt 行是:
"Example descripton:"//mis-spelled
接下来,线条...
char c[1000];
...
while(strcmp(c,"END OF FILE")){
...导致在初始化之前访问变量c
。 这反过来会导致未定义的行为。 修复很简单,在第一次使用变量之前总是初始化它们:`char c[1000] = {0};
还有,这条线...
while(strcmp(c,"END OF FILE")){
根据c
的内容会有不同的结果。 在此处阅读有关strcmp 的信息。
最后,这一行:
fseek(fptr, 1,SEEK_CUR);
对文本文件使用fseek时,必须满足以下条件之一:
offset 的值1
不满足这些标准中的任何一个。
如果您愿意尝试更简单的读取/解析方法,则使用while(fgets(...)){...}
构造利用比使用fscanf
变体更简单的解析。 以下循环直到找到指定的标记,然后读取文件中的下一行以获取值。 当fgets看到EOF 时,它将退出循环。 (注意,EOF 是文本文件固有的,不需要 inputfile.txt 中的最后一行)
int main() {
char c[1000] = {0};//always initialize before using
FILE *fptr,*resultfp;
if ((fptr = fopen("inputfile.txt", "r")) == NULL) {
printf("can't be opened");
return 0;
}
if ((resultfp = fopen("outputfile.txt", "w")) == NULL) {
printf("can't be opened");
fclose(fptr);
return 0;
}
// all is well so far, continue
while(fgets(c, sizeof(c), fptr))
{
if(strstr(c,"Example name") ||
strstr(c,"Example description") )
{
fgets(c, sizeof(c), fptr);
fprintf(resultfp,"%s\n", c);
}
}
fclose(fptr);
fclose(resultfp);
return 0;
}
由于指定输入文件的方式,检查每个完整行中的各种标记值对就足够且简单,在这种情况下,在单独的行中呈现,此方法简化了每一对的解析和测试。
你的程序有很多问题,但杀死它的是它假设换行符是单个字符的事实,即
fseek(fptr, 1,SEEK_CUR);
会跳过。
但在 Windows 上实际上并非如此:为了兼容性,stdio 库会将\\r\\n
转换为\\n
,但fseek
只会跳过一个字节。
如果你把它改成
fseek(fptr, 2,SEEK_CUR);
你的程序会起作用——对于“工作”的一个非常广泛的定义。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.