Home > OS >  Program doesn't work after it loops the second time
Program doesn't work after it loops the second time

Time:09-26

My program below is trying to add the usernames from the "Usernames.text" to the API url. The first username works and makes the url "https://api.1234ng.com/users/profiles/planeraft/Username1" but once it loops back it makes the url always "https://api.1234ng.com/users/profiles/planeraft/" instead of adding the next username in the file.

My code:

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

int Counter;
int UsernameSize;
char Username[255];
char Request[255]="https://api.1234ng.com/users/profiles/planeraft/";
FILE *UsernamesFile;

int main() {
    UsernamesFile = fopen("Usernames.text", "r");
  
    while (Counter < 100) {
        fgets(Username, 255, UsernamesFile);
        UsernameSize = strlen(Username);
        
        for (int z; z < UsernameSize; z  ) {
            Request[48   z] = Username[z];
        }
    
        printf("%s", "\n");
        printf(Request);
        
        for (int a; a < UsernameSize; a  ) {
            Request[48   a] = '\0';
        }
    
        Counter  ;
    }
 
    fclose(UsernamesFile);
    return 0;
}

CodePudding user response:

Variables a and z in the for loops are not initialized and thanks to DiKetarogg for some improvements to the code.

CodePudding user response:

Several errors fixed below, more improvements are possible, see comments above and in code below:

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

int Counter = 0; // need to init
int UsernameSize;
char Username[255];
char Request[255]="https://api.test.com/users/profiles/test/";
//                 01234567890123456789012345678901234567890
//                           1         2         3         4
FILE *UsernamesFile;

int main() {
  UsernamesFile = fopen("Usernames.text","r");
  while(Counter < 3) {
    fgets(Username, 255, UsernamesFile);
    strcat(Request, Username);
    UsernameSize = strlen(Username);
    Username[UsernameSize-1] = '\0'; // remove <cr>
    for(int z=0; z < UsernameSize; z  ) {
      Request[41 z]=Username[z];
    }
    printf("%s\n", Request);
    Counter  ;
    strcpy(Request, "https://api.test.com/users/profiles/test/");
  }
  fclose(UsernamesFile);
  return 0;
}
  •  Tags:  
  • c
  • Related