Home > Blockchain >  String storing filename is changed in C and I cannot figure out why. Think it's related to /0 c
String storing filename is changed in C and I cannot figure out why. Think it's related to /0 c

Time:02-25

I'm writing a program that requires me to seed a random number to select a file from input_01.txt to input_10.txt. The first line of each of these input files have a 6-character series of letters I have to store for usage later. For some reason, the fname I generate from the random seed has the 6 characters I read from the file appended to it by the end of the following operations. I feel it has something to do with %s looking for a \0 character but am unsure of how to fix it:

void main()
{
    srand(time(NULL)); 
    int rng2 = 1; //(rand()%9) 1; seeding random number from 1 to 10 for input.txt set to 1 for testing, it won't generate 10 for some reason
    char rng2char[2];   
    sprintf(rng2char, "%d.txt", rng2);
    FILE *fileStream; 
    char letters [6];
    char fname[12] = "";
    printf("\nrng2 generated was %d",rng2);
    if (rng2==10)
        strcat(fname, "input_");
    else    
        strcat(fname, "input_0");
    strcat(fname, rng2char);
    printf("\nWe have chosen %s",fname);
    //below here fname is ruined
    fileStream = fopen (fname, "r");
    fgets (letters, 7, fileStream); 
    fclose(fileStream);
    //somewhere above here, fname is ruined
    printf("\nLETTERS ARE: %s",letters);

until the "below here" line, %s fname returns "input_01.txt" as expected. However, afterwards it returns "input_01.txtVHAGOI" where VHAGOI is the first line of input.txt I'm very very new to C and would appreciate any and all feedback on other aspects of my convoluted code as well, I am open to learning. Thank you for your time and help.

CodePudding user response:

seeding random number from 1 to 10 for input.txt set to 1 for testing , it won't generate 10 for some reason

    int rng2 = (rand()%9) 1; -> int rng2 = (rand()) 1;
    

Count your required number of characters:

    char rng2char[2]; -> char rng2char[7];

My final:

    #include <stddef.h>
    #include <stdio.h>
    #include <string.h>
    #include <time.h>
    #include <stdlib.h>
    
    void main ()
    {
        // count your characters
        char rng2char[7];
        char letters[6];
        char fname[12] = "";
        int rng2;
        FILE * fileStream;

        srand (time (NULL));
        // mod 10 rather than 9
        rng2 = (rand () % 10)   1;
    
        // no need for the if else
        sprintf (rng2char, "%d.txt", rng2);
        strcat (fname, "input_");
        strcat (fname, rng2char);
    
        fileStream = fopen (fname, "r");
        // check for successful open
        if ( fileStream != NULL)
        {
           fgets (letters, 7, fileStream);
           fclose (fileStream);
        }
        else
        {
            printf("fopen error");
        }
            // debug
            printf ("\nrng2 generated was %d", rng2);
            printf ("\nWe have chosen %s", fname);
            printf ("\nLETTERS ARE: %s", letters);
    }

My output, srand 10:

    rng2 generated was 10
    We have chosen input_10.txt
    LETTERS ARE: ABCDEF
  • Related