How do I...

Im looking to reference a text file and make it into a variable but I cant figure out how... Would be a great help if someone could show me how. Im trying to use this.

#include <cstring>
#include <iostream>
#include <windows.h>

const char *typed_text = "Hello World!";

int main()
{
for(int idx = 0; idx < strlen(typed_text); ++idx)
{
std::cout << typed_text[idx];
Sleep(500); // half-second between "typing" letters
}
std::cout << std::endl;
return 0;
}


^ The above code I want to change is the variable. I want it to come from a text file. Also im using std namespace.
well... you have a pointer equal to a value not pointing to a const char
You would need to use the file stream

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/*
Only showing the additional code
*/
#include <fstream> //Add the file stream header

int main(){

char input_text[sizeoftext+1]; // sizeoftext is number of char's you want to read

char file_name[]="my_file.txt"; // the (ANSI) file you want to read from

ifstream file(file_name); // open the file for reading

file.getline(input_text, text_size); // put first text_size chars from the file in input_text

/*
your code here, using the input_text
*/

return 0;
}
Last edited on
@ Aramil Was just some code provided on another post to me.

@Danish Thanks! I'll see if i can get it working.
Topic archived. No new replies allowed.