The previous array is appending to the array i want. How do i get just the first part of the array? (the first part of the output).
前一个数组附加到我想要的数组。我如何得到数组的第一部分? (输出的第一部分)。
int main(void) {
FILE *fIn, *csis;
if (fopen_s(&csis,"csis.txt","w") != 0) {
printf("Failed to open csis.txt for writing.\n");
}
if (fopen_s(&fIn,"congress.txt","r") != 0) {
printf("Failed to open congress.txt for reading.\n");
}
else {
char processTxt[500] , txtUpperCase[500] ;
processFile(fIn, processTxt, txtUpperCase);
printf(txtUpperCase);
fprintf(csis,"%s",txtUpperCase);
cipher(txtUpperCase, 13);
/*outputCode(txtUpperCase);*/
fclose(fIn);
fclose(csis);
}
return 0;
}
void processFile(FILE *fIn, char *processTxt, char *txtUpperCase) {
int i = 0, j = 0;
fgets(processTxt, g_size, fIn);
for (i = 0; i < g_size; i++) {
if (processTxt[i] == '\0')
break;
processTxt[i] = toUpper(processTxt[i]);
}
processTxt[i] = '\0';
for (i = 0; i < g_size; i++) {
if (processTxt[i] == '\0')
break;
if (isUpperCase(processTxt[i])) {
txtUpperCase[j] = processTxt[i];
++j;
}
}
char isLowerCase(char input) {
return (input>= 'a' && input <= 'z');
}
char isUpperCase(char input) {
return (input>= 'A' && input <= 'Z');
}
char toUpper(char input) {
char upperCase = input;
if (isLowerCase(input))
upperCase = (char)((int)input - 32);
return upperCase;
}
output: CONGRESSSHALLMAKENOLAWRESPECTINGANESTABLISHMENTOFRELIGIONORPROHIBITINGTHEFREEEXERCISETHERE FORABRIDGINGTHEFREEDOMOFSPEECHOROFTHEPRESSORTHERIGHTOFTHEPEOPLEPEACEABLYTOASSEMBLEANDTOPETITIONTHEGOVERNMENTFORAREDRESSOFGRIEVANCESÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌCONGRESS SHALL MAKE NO LAW RESPECTING AN ESTABLISHMENT OF RELIGION, OR PROHIBITING THE FREE EXERCISE THEREOF; OR ABRIDGING THE FREEDOM OF SPEECH, OR OF THE PRESS; OR THE RIGHT OF THE PEOPLE PEACEABLY TO ASSEMBLE, AND TO PETITION THE GOVERNMENT FOR A REDRESS OF GRIEVANCES.
输出:CONGRESSSHALLMAKENOLAWRESPECTINGANESTABLISHMENTOFRELIGIONORPROHIBITINGTHEFREEEXERCISETHEREFORABRIDGINGTHEFREEDOMOFSPEECHOROFTHEPRESSORTHERIGHTOFTHEPEOPLEPEACEABLYTOASSEMBLEANDTOPETITIONTHEGOVERNMENTFORAREDRESSOFGRIEVANCESÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌCONGRESS不得制定法律期尊重宗教的建立,或它们的禁止*行;或者压制言论*或新闻*;或者人民的权利可以合并,并为*提出申诉,以减轻申诉。
}
2 个解决方案
#1
1
You seem to be missing a terminating null character in txtUpperCase
. The specific behavior of your program is undefined; fprintf
will print whatever it finds in memory until the null character is reached, even beyond the end of your array. The arrays seems to lie next to each other in memory on your platform, therefore they are both printed.
您似乎在txtUpperCase中缺少终止空字符。您的程序的特定行为未定义; fprintf将打印它在内存中找到的任何内容,直到到达空字符,甚至超出数组的末尾。阵列似乎在您平台的内存中彼此相邻,因此它们都被打印出来。
#2
1
Let's take this step by step.
让我们一步一步来。
- You are only performing the
else
if the secondfopen
succeeds. There is no guarantee in the code that the first one will have succeeded - It is not at all clear what the question is. Assuming that you are asking how to know the size of the 1st array after the second one appended to it, the answer is that there is no way to do so after the fact without reopening the file and determining the size
- That's quite the nasty talkback you got going there :)
如果第二个fopen成功,你只是在执行else。代码中无法保证第一个会成功
目前还不清楚问题是什么。假设您询问如何知道第一个数组在附加第二个数组之后的大小,答案是没有办法在没有重新打开文件和确定大小的情况下这样做
这是你去那里的令人讨厌的对话:)
#1
1
You seem to be missing a terminating null character in txtUpperCase
. The specific behavior of your program is undefined; fprintf
will print whatever it finds in memory until the null character is reached, even beyond the end of your array. The arrays seems to lie next to each other in memory on your platform, therefore they are both printed.
您似乎在txtUpperCase中缺少终止空字符。您的程序的特定行为未定义; fprintf将打印它在内存中找到的任何内容,直到到达空字符,甚至超出数组的末尾。阵列似乎在您平台的内存中彼此相邻,因此它们都被打印出来。
#2
1
Let's take this step by step.
让我们一步一步来。
- You are only performing the
else
if the secondfopen
succeeds. There is no guarantee in the code that the first one will have succeeded - It is not at all clear what the question is. Assuming that you are asking how to know the size of the 1st array after the second one appended to it, the answer is that there is no way to do so after the fact without reopening the file and determining the size
- That's quite the nasty talkback you got going there :)
如果第二个fopen成功,你只是在执行else。代码中无法保证第一个会成功
目前还不清楚问题是什么。假设您询问如何知道第一个数组在附加第二个数组之后的大小,答案是没有办法在没有重新打开文件和确定大小的情况下这样做
这是你去那里的令人讨厌的对话:)