Read File Char by Char C++ Get Random Values

random character generate

im writing a program to aid me empathize on rand fucntion i dont get how to utilize it with a file that has a bunch of characters i recollect i would accept to do it in a loop so information technology could exist random and set srand (fourth dimension(Naught));

                          1
two
3
iv
5
6
vii
eight
9
10
11
12
13
fourteen
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
xxx
31
32
33
34
                                                      #include <iostream>                            #include <stdlib.h>                            #include <time.h>                            #include <fstream>                            #include <string.h>                            #include <cstdlib>                            using                            namespace                            std;                            struct                            characters {                            char                            setOfCharacter; };                            int                            main() {      characters charInfo[iv];      ifstream inFile;      inFile.open("grapheme.txt");                            while(!inFile)      {          cout <<                            "error"                            << endl;                            return                            0;       }                            for(int                            i =0; i<4; i++)      {          inFile >> charInfo[i].setOfCharacter;      }      inFile.close();                            return                            0; }                        

hither is my file
4
ABC
Abc
AbC
aBC

i got it random generate but i want to take hold of a just one line not all the lines

                          1
two
three
iv
5
six
7
8
9
10
11
12
xiii
xiv
fifteen
16
17
eighteen
nineteen
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
twoscore
41
42
43
44
45
46
                                                      #include <iostream>                            #include <stdlib.h>                            #include <time.h>                            #include <fstream>                            #include <string.h>                            #include <cstdlib>                            using                            namespace                            std;                            struct                            characters {                            char                            setOfCharacter1;                            char                            setOfCharacter2;                            char                            setofCharacter3; };                            int                            main() {                            int                            totalCharacter=four;      characters charInfo[totalCharacter];      srand(time(NULL));      ifstream inFile;      inFile.open("character.txt");                            while(!inFile)      {          cout <<                            "mistake"                            << endl;                            return                            0;       }                            for(int                            i =0; i<totalCharacter; i++)      {          inFile >> charInfo[i].setOfCharacter1;          inFile >> charInfo[i].setOfCharacter2;          inFile >> charInfo[i].setofCharacter3;      }      inFile.close();                            for(int                            i =0; i<totalCharacter; i++)       {                            int                            x =rand() %4;           cout << charInfo[10].setOfCharacter1 << charInfo[x].setOfCharacter2 << charInfo[x].setofCharacter3 <<endl;       }                                                  

Last edited on

empathise on rand fucntion

past all means practise so but the most upward-to-engagement way of generating random numbers in C++ is using the <random> library - search this forum for several examples final few weeks

use std::getline() to read the file by line into std::string, salve the lines in a std::vector<std::string>, std::random_shuffle the vector and pick-off any particular element similar vec[0], etc

i dont empathize what you mean

"use std::getline() to read the file by line into std::string, save the lines in a std::vector<std::cord>, std::random_shuffle the vector and pick-off any particular chemical element like vec[0], etc "

i want to employ rand() not the library
the things is i have character not numbers

                          i
2
3
4
five
6
7
8
9
x
11
12
xiii
14
15
16
17
18
nineteen
twenty
21
22
23
                          std::string random_line_from( std::string file_name ) {     std::string selected_line ;                            // the randomly chosen line                            std::ifstream file(file_name) ;                            // open the file for reading                            std::string current_line ;                            // the electric current line read from the file                            std::size_t line_number = one ;                            while( std::getline( file, current_line ) )                            // for each line in the file                            {                            // the current line becomes the selected line with a probability of 1 / line_number                            // the showtime line is selected with a probability of one                            // the second line replaces  the selected line with a probability of ane/2                            //     and so now, each line so far being the selected line has a probability of 1/ii                            // the tertiary line replaces  the selected line with a probability of 1/3                            //     so now, each of the three lines so far being the selected line has a probability of 1/3                            // etc.                            if( ( std::rand() % line_number ) == 0 ) selected_line = current_line ;         ++line_number ;     }                            render                            selected_line ; }                        

http://coliru.stacked-kleptomaniacal.com/a/8d76660426f8b27e

i have graphic symbol not numbers

who said anything about numbers?

why when it'due south not the most accurate option bachelor unless your professor insists

i just want to exist able to but to employ rand() srand(time(nothing))

cheers for the case @jlborgers
since it a cord could i practise it with a character

Concluding edited on

If C-fashion arrays must be used:

                          i
2
three
four
5
6
7
8
9
ten
eleven
12
13
xiv
15
16
17
eighteen
xix
20
21
22
23
24
25
26
                                                      // assumes that no line in the file is longer than 4095 characters                            const                            std::size_t MAX_LINE_SZ = 4096 ;                            // +1 for the zero-graphic symbol at the terminate                            char* random_line_from(                            const                            char* file_name,                            char                            selected_line[MAX_LINE_SZ] ) {     selected_line[0] = 0 ;                            // initialise to a zilch string                            std::ifstream file(file_name) ;                            // open up the file for reading                            char                            current_line[MAX_LINE_SZ] ;                            // the electric current line read from the file                            std::size_t line_number = 1 ;                            while( file.getline( current_line, MAX_LINE_SZ ) )                            // for each line in the file                            {                            // the current line becomes the selected line with a probability of one / line_number                            // the first line is selected with a probability of 1                            // the 2nd line replaces  the selected line with a probability of ane/2                            //     so now, each line so far being the selected line has a probability of 1/two                            // the 3rd line replaces  the selected line with a probability of 1/3                            //     so now, each of the 3 lines and so far existence the selected line has a probability of 1/3                            // etc.                            if( ( std::rand() % line_number ) == 0 ) std::strcpy( selected_line, current_line ) ;         ++line_number ;     }                            return                            selected_line ; }                        

http://coliru.stacked-crooked.com/a/4574c133474cd9bc

Topic archived. No new replies immune.

beemanharastat.blogspot.com

Source: https://www.cplusplus.com/forum/general/210722/

0 Response to "Read File Char by Char C++ Get Random Values"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel