sumfoxdude
sumfoxdude

Reputation: 41

C Code Wont Work, Appears To Be Skipping Certain Code

I've been having trouble. This is nowhere near my final product but I have tried to move the code inside the loop, outside, everywhere I can. Tried +=, var = var+var2 etc. Nothing seems to work. Putting the code inside the loop and having a few modifications made the variable go to like 290 even though it only had to add 5 numbers. Problem: Code is skipping RequestCounter=RequestCounter+UsernameSize;

#include <cstdio>

int x;
int UsernameSize;
int RequestCounter=104;
char Username[255];
char Request[255]="curl -X POST https://api.test.com/profiles/test -H 'Content-Type: application/json' --data-raw '[";
FILE *UsernamesFile;

int main() {
    UsernamesFile = fopen("Usernames.text","r");
    fscanf(UsernamesFile, "%s", Username);
    UsernameSize = sizeof(Username);
    Request[RequestCounter]='"';
    RequestCounter++;
    for(int z; z < UsernameSize; z++) {
        Request[RequestCounter+z]=Username[z];
    }
    RequestCounter=RequestCounter+UsernameSize;
    Request[RequestCounter]='"';
    printf(Request);
    printf("%d",RequestCounter);
    fclose(UsernamesFile);
    return 0;
}

Upvotes: 0

Views: 48

Answers (1)

sumfoxdude
sumfoxdude

Reputation: 41

Thanks to Cid I found the answer. Apparently sizeof gets the size of the char array not the size of the text inside of the array.

To fix my problem I replaced

UsernameSize = sizeof(Username);

with

UsernameSize = strlen(Username);

Upvotes: 1

Related Questions