段故障(核心转储)错误

时间:2016-04-19 20:38:44

标签: c

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

void stringReverse(char* s){
    char tmp;
    int i = 0;
    int j = (strlen(s)-1);
    while(i>=j){
        tmp = s[i];
        s[i] = s[j];
        s[j] = tmp;
        i++;
        j--;
    }

}

int main(int argc, char* argv[]){
    FILE* in; /* file handle for input */
    FILE* out; /* file handle for output */
    char word[256]; /* char array to store words from the input file */

   /* checks that the command line has the correct number of argument */
    if(argc !=3){
        printf("Usage: %s <input file> <output file>\n", argv[0]);
        exit(EXIT_FAILURE);
    }

    /* opens input file for reading */
    in = fopen(argv[1], "r");
    if(in==NULL){
        printf("Unable to read from file %s\n", argv[1]);
    }

    /* opens ouput file for writing */
    out = fopen(argv[2], "w");
    if(out==NULL){
        printf("Unable to read from file %s\n", argv[2]);
        exit(EXIT_FAILURE);
    }

    /* reads words from the input file and reverses them and prints them on seperate lines to the output file */
    while(fscanf(in, " %s", word) != EOF) {
        stringReverse(word);
        fprintf(out, "%s\n", word);
    }

    /* closes input and output files */
    fclose(in);
    fclose(out);

    return(EXIT_SUCCESS);
}

我不断收到段错误(核心转储)错误。我在做什么 错误?我的out文件也是空的,不应该发生。

我的输入是

abc def ghij 
klm nopq rstuv 
w xyz

,输出应为

cba 
fed 
jihg 
mlk  
qpon  
vutsr 
w  
zyx

1 个答案:

答案 0 :(得分:4)

您肯定应该花一些时间学习使用valgrindwhile(i<=j)。 至于代码,第9行不应该是while(i>=j)而不是while(i>=j)吗?

<强>解释

Barmar和loginn之间的讨论实际上在解释为什么在这里发生分段错误方面做得很好。逻辑s[-1]是错误的,但除非您的输入文件中有单个字符(例如样本输入中的“w”),否则您不会因此而出现分段错误。为什么单个字符输入会导致分段错误?因为那时你开始你的循环,i = 0和j = 0,它满足循环条件,然后进入i = 1和j = -1,这也满足不正确的循环条件。此时,尝试访问无效地址(<!-- language: lang-xml --> <table name="blog" phpName="Blog"> <column name="id" type="INTEGER" required="true" primaryKey="true" autoIncrement="true" /> <column name="title" phpName="Title" type="VARCHAR" size="100" required="true" /> <column name="short_link" phpName="ShortLink" type="VARCHAR" size="64" required="false" /> <column name="description" phpName="Description" type="LONGVARCHAR" required="false" /> <column name="content" phpName="Content" type="LONGVARCHAR" required="false" /> <column name="visibility" phpName="Visibility" type="INTEGER" default="-1" required="true" /> <behavior name="i18n"> <parameter name="i18n_columns" value="title, short_link, description, content" /> </behavior> <behavior name="timestampable" /> <behavior name="soft_delete" /> <behavior name="versionable" /> </table> <table name="blog_i18n" phpName="blogi18n"> <column name="id" type="INTEGER" required="true" primaryKey="true" /> <column name="locale" type="VARCHAR" size="5" required="true" primaryKey="true" /> <column name="title" phpName="Title" type="VARCHAR" size="100" required="false" /> <column name="short_link" phpName="ShortLink" type="VARCHAR" size="64" required="false" /> <column name="description" phpName="Description" type="LONGVARCHAR" required="false" /> <column name="content" phpName="Content" type="LONGVARCHAR" required="false" /> <foreign-key foreignTable="blog" onDelete="setnull" onUpdate="cascade"> <reference local="id" foreign="id" /> </foreign-key> <behavior name="timestampable" /> <behavior name="soft_delete" /> <behavior name="versionable" /> </table> )将导致分段错误。

如果您的输入文件中没有任何单个字符,那么您的程序将只运行并打印输出文件中的单词。它不会反转任何单词,因为代码不会因为错误的条件而进入反转的while循环。但它也不会导致任何分段错误。

我希望我已经解释得很清楚。