randomname
randomname

Reputation: 275

previous array appending to current array

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.

}

Upvotes: 0

Views: 64

Answers (2)

Hilborn
Hilborn

Reputation: 754

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.

Upvotes: 1

levengli
levengli

Reputation: 1121

Let's take this step by step.

  1. You are only performing the else if the second fopen succeeds. There is no guarantee in the code that the first one will have succeeded
  2. 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
  3. That's quite the nasty talkback you got going there :)

Upvotes: 1

Related Questions