为什么赢了';这个printf()语句是否在C中打印两个字符串变量?
在学习C语言的过程中,我一直在疯狂地研究。我一直在调试一个C程序,我认为我遇到了一些重大问题。现在我有一些关键问题。我制作了一个虚拟程序,在一条语句中打印两个字符串,如下所示:为什么赢了';这个printf()语句是否在C中打印两个字符串变量?,c,string,debugging,printf,C,String,Debugging,Printf,在学习C语言的过程中,我一直在疯狂地研究。我一直在调试一个C程序,我认为我遇到了一些重大问题。现在我有一些关键问题。我制作了一个虚拟程序,在一条语句中打印两个字符串,如下所示: #include<stdio.h> int main(int argc, char* argv[]) { char *herp = "Derp"; char *derp = "Herp"; printf("Herp %s Derp %s\n", herp, derp);
#include<stdio.h>
int main(int argc, char* argv[])
{
char *herp = "Derp";
char *derp = "Herp";
printf("Herp %s Derp %s\n", herp, derp);
return 0;
}
所以,我想,让我通过做类似的事情来调试我自己的程序。我的程序中的以下行
printf("word is: %s and jumbled word is: %s\n", word, jumbleWord);
应该打印出类似于
Word is: word and jumbled word is: dowr
但它会打印出类似于
and jumbled word is: dowr
输出的第一部分到哪里去了?我需要能够打印在同一行调试这两个。而且,像这样的声明不起作用的事实告诉我,真的有奇怪的事情发生了,我因为把头发扯下来而秃顶了。正如我的链接帖子所指出的,我最终希望比较这些字符串值,但如果printf()工作不正常,我该怎么做呢
我在下面发布了整个程序,这样你就可以看到一切都发生在哪里了。我只是在学习如何使用指针。我本来有两个指针指向同一个内存,当我想把一个单词弄乱的时候,但效果不是很好!所以我修复了这个问题,得到了两个独立的内存空间,其中包含了我需要的单词。现在,我无法打印它们。考虑到以下代码,这一切都是有意义的:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAX_WORD_LENGTH 25
//Define global variables
int numWords;
//Preprocessed Functions
void jumblegame();
void readFile(char *[]);
void jumbleWord(char *);
void guess(char *,char *);
int main(int argc, char* argv[])
{
jumblegame();
return 0;
}
void jumblegame()
{
//Load File
int x = 5050; //Rows
char *words[x];
readFile(words);
//Define score variables
int totalScore = 0;
int currentScore = 0;
//Repeatedly pick a random work, randomly jumble it, and let the user guess what it is
srand((unsigned int)time(NULL));
int randomNum = rand() % numWords + 1;
char source[MAX_WORD_LENGTH + 1];
char jumble[MAX_WORD_LENGTH + 1];
strncpy(source, words[randomNum], MAX_WORD_LENGTH + 1);
strncpy(jumble, words[randomNum],MAX_WORD_LENGTH + 1);
jumbleWord(jumble);
guess(source, jumble);
//printf("Random word is: %s\n ", words[randomNum]);
//randomly jumble it
}
void readFile(char *array[5049])
{
char line[256]; //This is to to grab each string in the file and put it in a line.
int z = 0; //Indice for the array
FILE *file;
file = fopen("words.txt","r");
//Check to make sure file can open
if(file == NULL)
{
printf("Error: File does not open.");
exit(1);
}
//Otherwise, read file into array
else
{
while(!feof(file))//The file will loop until end of file
{
if((fgets(line,256,file))!= NULL)//If the line isn't empty
{
int len = strlen(line);
if (len > 0 && line[len - 1] == '\n') line[len - 1] = '\0';
array[z] = malloc(strlen(line) + 1);
strcpy(array[z],line);
z++;
}
}
}
fclose(file);
numWords = z;
}
void jumbleWord(char *word)
{
int wordSize = strlen(word) - 1;
//durstenfeld Implementation of Fischer-Yates Shuffle
int i;
int j;
char temp;
for(i = wordSize - 1; i > 0; i--)
{
j = rand() % (i + 1);
temp = word[j];
word[j] = word[i];
word[i] = temp;
}
}
void guess(char *word, char *jumbleWord)
{
printf("original word is: %s\n", word);
printf("jumbled word is: %s\n", jumbleWord);
printf("source is: %s and jumbled word is: %s\n", word, jumbleWord);
}
#包括
#包括
#包括
#定义最大单词长度25
//定义全局变量
国际货币基金组织;
//预处理函数
void jumblegame();
void readFile(char*[]);
无效jumbleWord(字符*);
无效猜测(char*,char*);
int main(int argc,char*argv[])
{
jumblegame();
返回0;
}
void jumblegame()
{
//加载文件
int x=5050;//行
字符*字[x];
读取文件(字);
//定义分数变量
整数总分=0;
int currentScore=0;
//重复地挑选一个随机的作品,随机地把它混在一起,让用户猜测它是什么
srand((无符号整数)时间(NULL));
int randomNum=rand()%numWords+1;
字符源[最大字长+1];
字符混乱[最大字长+1];
strncpy(源,单词[randomNum],最大单词长度+1);
strncpy(混乱,单词[randomNum],最大单词长度+1);
jumbleWord(jumble);
猜测(来源,混乱);
//printf(“随机字为:%s\n”,字[randomNum]);
//乱七八糟
}
无效读取文件(字符*数组[5049])
{
字符行[256];//这是为了获取文件中的每个字符串并将其放在一行中。
int z=0;//为数组标记
文件*文件;
file=fopen(“words.txt”、“r”);
//检查以确保文件可以打开
if(file==NULL)
{
printf(“错误:文件未打开。”);
出口(1);
}
//否则,将文件读入数组
其他的
{
while(!feof(file))//文件将循环直到文件结束
{
if((fgets(第256行,文件))!=NULL)//如果该行不是空的
{
int len=strlen(直线);
如果(len>0&&line[len-1]='\n')line[len-1]='\0';
数组[z]=malloc(strlen(line)+1);
strcpy(数组[z],行);
z++;
}
}
}
fclose(文件);
numWords=z;
}
无效jumbleWord(字符*字)
{
int-wordSize=strlen(word)-1;
//durstenfeld实施Fischer-Yates洗牌
int i;
int j;
焦炭温度;
对于(i=wordSize-1;i>0;i--)
{
j=rand()%(i+1);
temp=单词[j];
字[j]=字[i];
字[i]=温度;
}
}
无效猜测(字符*字,字符*字)
{
printf(“原始单词是:%s\n”,单词);
printf(“混杂字为:%s\n”,混杂字);
printf(“源为:%s,混杂字为:%s\n”,字,混杂字);
}
我想在这一点上,大多数人都会烧了C,并因为自己吸得太差而扇自己一巴掌。不过,我会继续用卡车运输。所以,让我为任何derp道歉,但请知道,我花了很多时间可能真的很愚蠢,盯着这个。我想说,“嘿,C是如此愚蠢,因为它不会做我告诉它的事情。”。不幸的是,我不能相信这一点。我认为它正在做我告诉它做的事情,但我离这个问题太近了,看不出我做错了什么
一如既往,谢谢你的帮助。我最深的敬意,
GeekyOmega您在单词末尾有一个回车符
'\r'
回车键将写光标移到屏幕左侧,这样它就可以写了,但它会覆盖已经存在的内容。您的Word文件实际上有
5049
条目吗?即使它没有,你也不应该假设它有。您的readFile
函数应该在读取文件后确定words
数组中实际存在多少单词,否则,对words
数组进行随机索引将导致访问未初始化的字符串,我怀疑这会导致内存损坏
因此,如果readFile
只看到10个单词,那么您应该只在索引0..9
之间选择一个随机单词。您已经在readFile
方法中保留了变量z
中的字数,请将其与其余代码共享
当您在数组中选择随机索引时,请注意,由于数组是基于0的,因此您应该获得随机值,并且只需通过有效数组元素的数量进行mod。所以使用intrandomnum=rand()%5049代码>不带+1
此外,所有这些strlen/+1/-1
内容都是不必要的和令人困惑的,对于字符串,您更喜欢strncpy
而不是memcpy
(和strcpy
),您不需要处理这些。请注意,strlen
的结果不包含空终止符,因此您不需要使用-1
来解释。我认为你的jumbleWord
功能
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAX_WORD_LENGTH 25
//Define global variables
int numWords;
//Preprocessed Functions
void jumblegame();
void readFile(char *[]);
void jumbleWord(char *);
void guess(char *,char *);
int main(int argc, char* argv[])
{
jumblegame();
return 0;
}
void jumblegame()
{
//Load File
int x = 5050; //Rows
char *words[x];
readFile(words);
//Define score variables
int totalScore = 0;
int currentScore = 0;
//Repeatedly pick a random work, randomly jumble it, and let the user guess what it is
srand((unsigned int)time(NULL));
int randomNum = rand() % numWords + 1;
char source[MAX_WORD_LENGTH + 1];
char jumble[MAX_WORD_LENGTH + 1];
strncpy(source, words[randomNum], MAX_WORD_LENGTH + 1);
strncpy(jumble, words[randomNum],MAX_WORD_LENGTH + 1);
jumbleWord(jumble);
guess(source, jumble);
//printf("Random word is: %s\n ", words[randomNum]);
//randomly jumble it
}
void readFile(char *array[5049])
{
char line[256]; //This is to to grab each string in the file and put it in a line.
int z = 0; //Indice for the array
FILE *file;
file = fopen("words.txt","r");
//Check to make sure file can open
if(file == NULL)
{
printf("Error: File does not open.");
exit(1);
}
//Otherwise, read file into array
else
{
while(!feof(file))//The file will loop until end of file
{
if((fgets(line,256,file))!= NULL)//If the line isn't empty
{
int len = strlen(line);
if (len > 0 && line[len - 1] == '\n') line[len - 1] = '\0';
array[z] = malloc(strlen(line) + 1);
strcpy(array[z],line);
z++;
}
}
}
fclose(file);
numWords = z;
}
void jumbleWord(char *word)
{
int wordSize = strlen(word) - 1;
//durstenfeld Implementation of Fischer-Yates Shuffle
int i;
int j;
char temp;
for(i = wordSize - 1; i > 0; i--)
{
j = rand() % (i + 1);
temp = word[j];
word[j] = word[i];
word[i] = temp;
}
}
void guess(char *word, char *jumbleWord)
{
printf("original word is: %s\n", word);
printf("jumbled word is: %s\n", jumbleWord);
printf("source is: %s and jumbled word is: %s\n", word, jumbleWord);
}