Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/57.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
OS X版本之间的C问题会导致未定义的行为 编辑_C_Osx Yosemite_Osx Elcapitan - Fatal编程技术网

OS X版本之间的C问题会导致未定义的行为 编辑

OS X版本之间的C问题会导致未定义的行为 编辑,c,osx-yosemite,osx-elcapitan,C,Osx Yosemite,Osx Elcapitan,我试图将c初始化为charc[2]={'\0'}具有相同的结果,并且改变了strncpy((char*)c,tmp,1)到strncpy(c,tmp,1)也具有相同的结果 也将strcat(tmp,(char*)c)更改为strcat(tmp,c),结果相同 我还尝试在c上使用带有空字节的memset(),结果相同。它仍按10.10.5中的预期执行,但也会在10.11.6中向字符串添加? 我一直在练习C语言,并决定在工作中编写一个文本解析器。它从文件中读取数据,并根据传递给它的不同参数对其执行

我试图将
c
初始化为
charc[2]={'\0'}具有相同的结果,并且改变了
strncpy((char*)c,tmp,1)
strncpy(c,tmp,1)也具有相同的结果

也将
strcat(tmp,(char*)c)
更改为
strcat(tmp,c)
,结果相同

我还尝试在c上使用带有空字节的
memset()
,结果相同。它仍按10.10.5中的预期执行,但也会在10.11.6中向字符串添加


我一直在练习C语言,并决定在工作中编写一个文本解析器。它从文件中读取数据,并根据传递给它的不同参数对其执行操作。一个选择是让它将文件中的所有文本转换为Pig Latin,这在OS X 10.10.5上运行得很好,但我让我的女朋友在家(OS X 10.11.6)(她正在帮助我从简单的脚本语言Python/JS迁移),它开始在每个单词的同一位置的字符之间放置随机字节。这是我的10.10机器上的代码和输出

main.c

#include "parser.h"

int main(int argc, char *argv[])
{
    if(argc < 2){err("Program Requires at Least One Argument.");}
    if(argc > 6){err("Too many arguments.");}

    int i = 1;      // for indexing args (don't care about arg 0)

    /* loop over the arguments to find any parameters
    we don't look at last argument, as it should be
    the file name */
    for(i = 1; i < argc - 1; i++) {
        if(strcmp(argv[i], "-v") == 0) {v = 1;}
        else if(strcmp(argv[i], "-p") == 0) {p = 1;}
        else if(strcmp(argv[i], "-c") == 0) {c = 1;}
        else {err("Invalid Argument.");}
    }

    strcpy(flnm, argv[argc-1]);

    parse(flnm);

    return 0;
}
void parse(const char *fn)
{
    FILE *f = fopen(fn, "r");
    if(!f) {err("File does not exist.");}

    if(v == 1) {
        while(xfscanf(f) != 0) {
            vwlCnt(buff);
            printf("%s\n", buff);
        }

        printf("File \"%s\" contains %d vowels.\n", fn, v_cnt);
    }

    if(p == 1) {
        FILE *fp = fopen("out.txt", "w+");
        if(!fp) {err("File write error.");}

        while(xfscanf(f) != 0) {
            pigLat(buff);
            fputs(buff, fp);
        }

        fclose(fp);
    }

    if(c == 1 && v + p != 2) {
        if(p == 1) {FILE *fp = fopen("out.txt", "r"); cat(fp);}
        else {cat(f);}
    }

    fclose(f);
}

void pigLat(char *str)
{
    int con = 0;
    char *c[1];
    char *tmp = TEMP(str);

    switch(tmp[0]) {
        case 'a':
        case 'A':
            con = 0;
            break;
        case 'e':
        case 'E':
            con = 0;
            break;
        case 'i':
        case 'I':
            con = 0;
            break;
        case 'o':
        case 'O':
            con = 0;
            break;
        case 'u':
        case 'U':
            con = 0;
            break;
        default:
            con = 1;
            break;
    }

    if(con == 1) {
        printf("%c\n", tmp[0]);
        strncpy((char *)c, tmp, 1);

        for(int i = 0; i < sizeof(tmp); i++) {
            tmp[i] = tmp[i+1];
        }

        strcat(tmp, (char *)c);
        strcat(tmp, "ay ");

        strcpy(buff, tmp);
    } else {strcat(tmp, "ay "); strcpy(buff, tmp);}

    printf("%s\n", tmp);
}
char c[2] = {'\0'};
#包括“parser.h”
int main(int argc,char*argv[])
{
if(argc<2){err(“程序至少需要一个参数。”);}
如果(argc>6){err(“参数太多”);}
int i=1;//用于索引参数(不关心参数0)
/*循环参数以查找任何参数
我们不看最后一个论点,应该是这样的
文件名*/
对于(i=1;i
parser.c

#include "parser.h"

int main(int argc, char *argv[])
{
    if(argc < 2){err("Program Requires at Least One Argument.");}
    if(argc > 6){err("Too many arguments.");}

    int i = 1;      // for indexing args (don't care about arg 0)

    /* loop over the arguments to find any parameters
    we don't look at last argument, as it should be
    the file name */
    for(i = 1; i < argc - 1; i++) {
        if(strcmp(argv[i], "-v") == 0) {v = 1;}
        else if(strcmp(argv[i], "-p") == 0) {p = 1;}
        else if(strcmp(argv[i], "-c") == 0) {c = 1;}
        else {err("Invalid Argument.");}
    }

    strcpy(flnm, argv[argc-1]);

    parse(flnm);

    return 0;
}
void parse(const char *fn)
{
    FILE *f = fopen(fn, "r");
    if(!f) {err("File does not exist.");}

    if(v == 1) {
        while(xfscanf(f) != 0) {
            vwlCnt(buff);
            printf("%s\n", buff);
        }

        printf("File \"%s\" contains %d vowels.\n", fn, v_cnt);
    }

    if(p == 1) {
        FILE *fp = fopen("out.txt", "w+");
        if(!fp) {err("File write error.");}

        while(xfscanf(f) != 0) {
            pigLat(buff);
            fputs(buff, fp);
        }

        fclose(fp);
    }

    if(c == 1 && v + p != 2) {
        if(p == 1) {FILE *fp = fopen("out.txt", "r"); cat(fp);}
        else {cat(f);}
    }

    fclose(f);
}

void pigLat(char *str)
{
    int con = 0;
    char *c[1];
    char *tmp = TEMP(str);

    switch(tmp[0]) {
        case 'a':
        case 'A':
            con = 0;
            break;
        case 'e':
        case 'E':
            con = 0;
            break;
        case 'i':
        case 'I':
            con = 0;
            break;
        case 'o':
        case 'O':
            con = 0;
            break;
        case 'u':
        case 'U':
            con = 0;
            break;
        default:
            con = 1;
            break;
    }

    if(con == 1) {
        printf("%c\n", tmp[0]);
        strncpy((char *)c, tmp, 1);

        for(int i = 0; i < sizeof(tmp); i++) {
            tmp[i] = tmp[i+1];
        }

        strcat(tmp, (char *)c);
        strcat(tmp, "ay ");

        strcpy(buff, tmp);
    } else {strcat(tmp, "ay "); strcpy(buff, tmp);}

    printf("%s\n", tmp);
}
char c[2] = {'\0'};
void解析(const char*fn)
{
文件*f=fopen(fn,“r”);
如果(!f){err(“文件不存在”);}
如果(v==1){
而(xfscanf(f)!=0){
vwlCnt(buff);
printf(“%s\n”,浅黄色);
}
printf(“文件\%s\”包含%d个元音。\n”,fn,v_cnt);
}
如果(p==1){
文件*fp=fopen(“out.txt”,“w+”);
如果(!fp){err(“文件写入错误”);}
而(xfscanf(f)!=0){
皮格拉特(浅黄色);
fput(buff,fp);
}
fclose(fp);
}
如果(c==1&&v+p!=2){
如果(p==1){FILE*fp=fopen(“out.txt”,“r”);cat(fp);}
else{cat(f);}
}
fclose(f);
}
空pigLat(字符*str)
{
int con=0;
char*c[1];
char*tmp=TEMP(str);
开关(tmp[0]){
案例“a”:
案例“A”:
con=0;
打破
案例“e”:
案例“E”:
con=0;
打破
案例“i”:
案例“I”:
con=0;
打破
案例“o”:
案例“O”:
con=0;
打破
案例“u”:
案例“U”:
con=0;
打破
违约:
con=1;
打破
}
如果(con==1){
printf(“%c\n”,tmp[0]);
strncpy((char*)c,tmp,1);
对于(int i=0;i
parser.h

#ifndef __parser_h__
#define __parser_h__

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

int p;              // pig latin argument;
int v;              // vowel count argument
int c;              // cat arg
int v_cnt;          // vowel count
char vwl;           // vowel char
char flnm[64];      // file name
char buff[32];      // file scan buffer


void err(const char *msg);      // error function
char *newTemp(char *str);       // creates a 32B temp pointer on heap
int  xfscanf(FILE *f);          // simple fscan
void cat(FILE *f);
void parse(const char *fn);     // file parsing function
void vwlCnt(char *str);         // counts vowels in string
void pigLat(char *str);         // pig latinize string

#define TEMP(X) newTemp(X)

#endif
\ifndef\uuu解析器\uh__
#定义语法分析器__
#包括
#包括
#包括
#包括
int p;//猪拉丁语论证;
int v;//元音计数参数
int c;//猫精蛋白
int v_cnt;//元音计数
char vwl;//元音字符
字符flnm[64];//文件名
字符buff[32];//文件扫描缓冲区
void err(const char*msg);//误差函数
char*newTemp(char*str);//在堆上创建32B临时指针
int xfscanf(文件*f);//简单fscan
作废类别(档案*f);
void parse(const char*fn);//文件解析函数
void vwlCnt(char*str);//计算字符串中的元音
void pigLat(char*str);//清管拉丁字符串
#定义温度(X)新温度(X)
#恩迪夫
输入文件

2016年里约热内卢奥运会的比赛只剩下不到一周的时间了,空荡荡的体育场座位似乎正在夺走一些世界上装饰最华丽的运动员的聚光灯。奥运会组织者周三表示,到目前为止,600多万张门票中有88%在里约热内卢售出。这比2012年的伦敦奥运会和2008年的北京奥运会都要少,这两届奥运会的门票都售出了96%。但这比2004年雅典奥运会要好得多,当时只有67%的门票被购买。体操和足球、沙滩排球等巴西有望出类拔萃的体育项目的入场率一直很高。尽管如此,在许多场馆仍然可以看到大片的空座位,最明显的是在田径场

输出

它将于2016年12月1日在埃姆斯奥林匹克运动会上举行一场比赛,快乐的空荡荡的tadiumsay eatssay eemsay otay ebay Tealings说hetay Pottlights Away romfay omesay omesay hetay World Way ostmay可能会被评为当今的运动员,Jay Olympicay的组织者说,这是一个很好的选择,比如说,2012年汉泰和2012年汉泰、2012年汉泰和2008年汉泰、2008年汉威和奥斯贝,在所有继承人的ICKET中占6%的份额,比如说,2012年汉泰和0042年汉泰和雅典,在这里,我们每个人都有7%的时间都能在这里度过。在这里,我们每个人都有7%的时间都能在这里度过。在这里,我们每个人都有7%的时间都能在这里度过