Thread: Get random data list from a file txt

  1. #1
    Registered User
    Join Date
    Nov 2019
    Posts
    1

    Get random data list from a file txt

    Hello! I am starting in programming with C and I want to
    create my first program: I have a file (.txt) with names
    (peter, paul, etc) and another (surnames) and I wanted a
    program that took names and surnames randomly and displayed
    them on screen combined (random name1 + random last name1
    and so on).

    I do not know if this is possible, since the functions of c
    (rand and srand) are to obtain random numbers and I am
    looking for data to be extracted from a file.

    This is my first attempt:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
     int main() {
         
        char name; 
        FILE* fichero;
        fichero = fopen("names.txt", "rt");
        rand();    
        srand(names.txt);
        printf("Name list", name);    
    
        fclose(f);
        return 0;
    }
    I just wanted to get a random name and then program a repetition cycle that takes names until a key is pressed. However, I don't get results and I don't know how to implement it.

    Thanks for the help!

  2. #2
    Registered User
    Join Date
    Dec 2017
    Posts
    724
    It's probably best to initially read each of the files into arrays. Then you can randomly print the names from the arrays.
    Code:
    // rnd_names.c
     
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <time.h>
     
    #define MAXNAMES      100
    #define MAXNAMELEN    50
    #define NAMES_FILE    "names.txt"
    #define SURNAMES_FILE "surnames.txt"
     
    int read_file(const char *filename, char arr[][MAXNAMELEN]) {
        int size = 0;
        FILE *file = fopen(filename, "r");
        char line[MAXNAMELEN];
        while (fgets(line, sizeof line, file)) {
            line[strcspn(line, "\n")] = '\0';  // remove newline
            strcpy(arr[size++], line);
        }
        fclose(file);
        return size;
    }
     
    int rnd(int n) {
        return rand() % n;  // int from 0 to n - 1
    }
     
    int main() {
        srand(time(0)); // seed random number generator with current time
     
        char names[MAXNAMES][MAXNAMELEN];
        int nam_len = read_file(NAMES_FILE, names);
     
        char surnames[MAXNAMES][MAXNAMELEN];
        int sur_len = read_file(SURNAMES_FILE, surnames);
     
        char ch;
        do {
            printf("%s %s\n", names[rnd(nam_len)], surnames[rnd(sur_len)]);
     
            printf("Again (y/n)? ");
        } while (scanf(" %c", &ch) == 1 && (ch == 'y' || ch == 'Y'));
     
        return 0;
    }
    The world hangs on a thin thread, and that is the psyche of man. - Carl Jung

  3. #3
    Registered User
    Join Date
    Aug 2019
    Location
    Inside a Singularity
    Posts
    197
    If you are going to be reading line by line and if you know how many different lines(with different names, surnames etc) exist in your file, you could use a random number generated in a range (say your random number is N) to run a loop to read line by line N times to reach the N'th line. You could then print what you've read.
    If your NAMES file has 10 names on 10 different lines and your SURNAMES file has 20 names on 20 different lines, have two variables(M,N) assigned randomly in a range of 1-10 for first file and 1-20 for second file. Read the Mth line from first file, Nth from the second file. This gives you the Mth Name and the Nth Surname. You can then print them and also add recursion functionality such that the program runs until the user enters something like a 'No' as you mentioned you wanted.
    Last edited by Zeus_; 3 Weeks Ago at 09:52 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Writing Data Randomly to a Random-Access File
    By lmanukyan in forum C Programming
    Replies: 3
    Last Post: 02-12-2016, 08:12 AM
  2. Replies: 3
    Last Post: 11-10-2014, 09:53 AM
  3. reading then displaying some data from a random access file
    By kool_hamster in forum C Programming
    Replies: 5
    Last Post: 01-21-2010, 10:56 AM
  4. Adding data to random column in List View
    By Gravedigga in forum Windows Programming
    Replies: 6
    Last Post: 07-25-2004, 07:11 AM

Tags for this Thread