简体   繁体   English

在C中使用strsep和fwrite时输出奇怪

[英]Strange output while using strsep and fwrite in C

I was going to make a python program to read from an XML document and use it as a reference while doing an action. 我打算创建一个python程序来读取XML文档,并在执行操作时将其用作参考。 However, the XML document would have been tedious to create by hand, so I decided to make a C program to do the bulk of it, excluding the first line and the root element. 但是,XML文档手动创建会很繁琐,所以我决定制作一个C程序来完成它的大部分工作,不包括第一行和根元素。 Here is the code: 这是代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(){
    FILE * fp = fopen("/Users/rajshrimali/Desktop/SPC_CMS.txt", "r");
    FILE * fw = fopen("/Users/rajshrimali/Desktop/SPC_XML", "a");
    char *line = NULL;
    size_t linecap = 0;
    ssize_t linelen;
    while ((linelen = getline(&line, &linecap, fp)) > 0){
        char *token;
        int x = 1; 
        while((token = strsep(&line, ",")) != NULL){
            printf("%s", token);
            if(x == 1){
                fwrite("<item> \n", linelen, 1, fw);
                fwrite("<name> \n", linelen, 1, fw);
                fwrite(token, linelen, 1, fw);
                fwrite("\n </name> \n", linelen, 1, fw);
                x++; 
            }
            if(x == 2) {
                fwrite("<SPC> \n", linelen, 1, fw);
                fwrite(token, linelen, 1, fw);
                fwrite("\n </SPC> \n", linelen, 1, fw);
                fwrite("</item> \n", linelen, 1, fw);
            }
        }
    }
}

The code compiled with no errors. 编译的代码没有错误。 However, when I ran it, the file SPC_XML was not nearly right: 但是,当我运行它时,文件SPC_XML几乎不对:

<item> 
�<na<name> 

 <Agate�0.80

 </name> 
�<SPC> 

 </Agate�0.80

 </SPC> 
<</item> 
,�<item> 
<na<name> 

This nonsense continued for some time. 这段废话持续了一段时间。 The input file, fp , had data in this format: 输入文件fp具有以下格式的数据:

Agate,0.80
Aluminum bronze,0.436
Aluminum,0.87
Antimony,0.21
Apatite,0.84

I think the error is with fwrite, although I have no idea what it is. 认为错误是用fwrite,虽然我不知道它是什么。 What is the problem? 问题是什么?

You have some obvious errors with your if statements here, you are using = assignment when you should be using == equality, for example: 这里你的if语句有一些明显的错误,当你应该使用==相等时你正在使用=赋值,例如:

 if(x = 1)

should be: 应该:

if(x == 1)

I would suggest turning on warning, using gcc with -Wall -W -pedantic you will see something like this: 我建议打开警告,使用带-Wall -W -pedantic gcc,你会看到这样的东西:

warning: suggest parentheses around assignment used as truth value [-Wparentheses]
         if(x = 1){

When you call fwrite , the second argument should be the length of the string that you are writing. 当你调用fwrite ,第二个参数应该是你正在编写的字符串的长度。 So, when you do: 所以,当你这样做时:

fwrite("<item> \n", linelen, 1, fw);

you will end up writing extra data, or maybe not enough, depending on what linelen is. 你最终会写出额外的数据,或者可能还不够,这取决于linelen是什么。 Instead, you should manually count the size of your strings or call strlen on each of them. 相反,您应该手动计算字符串的大小或在每个字符串上调用strlen So, the call above could be turned into: 所以,上面的调用可以变成:

fwrite("<item> \n", 8, 1, fw);

and when you write out the tokens, you should call strlen : 当你写出令牌时,你应该打电话给strlen

fwrite(token, strlen(token), 1, fw);

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

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