How to read text from a file?
Hey, I have a nice game engine working, but I cant figure out how to read text from a .txt file into my game.
I know it has stuff to do with ifstream.h but my "how to program c++" book only shows how to read binary data from file, not text data.
Can anyone help me?
Possibility
I prefer to use C style I/O functions on text files. So to read a text file line by line I''d use something like:
char buffer[512]; // hopefully your text file doesn''t have obscene line lengthsFILE * fp = fopen("filename.txt", "r");if (fp != NULL) { while (!feof(fp)) { fgets(buffer, 511, fp); // process string } fclose(fp);}
Ok, but what do header do I need to include to get the FILE and fopen and such?
Possibility
Possibility
ReactOS - an Open-source operating system compatible with Windows NT apps and drivers
Hmmm, that doesnt work.
here is the code i wrote:
and my test.txt just contains
but my output is 512 characters of pure jibberish.
Possibility
here is the code i wrote:
#include <iostream.h>#include <fstream.h>#include <iomanip.h>#include <stdlib.h>#include <windows.h>#include <stdio.h>int main(){ char buffer[512]; // hopefully your text file doesn''t have obscene line lengths FILE * fp = fopen("test.txt", "r"); if (fp != NULL) { while (!feof(fp)) { fgets(buffer, 511, fp); // process string } fclose(fp); } cout<<buffer<< endl; return 0;}
and my test.txt just contains
abcd1234
but my output is 512 characters of pure jibberish.
Possibility
#include <iostream.h>#include <fstream.h>int main(){char line[256];fstream fin;fin.open("File.txt", ios::in);fin>>line;fin.close();return(0);}
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
Argg, nothing is working for me
and here is my output
Possibility
#include <iostream.h>#include <fstream.h>#include <iomanip.h>#include <stdlib.h>#include <windows.h>#include <stdio.h>int main(){ char line[20]; fstream fin; fin.open("file.txt", ios::in); fin>>line; fin.close(); cout<<line<<endl; return(0);}
and here is my output
¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦(_fPress any key to continue
Possibility
Try this.
fread(buffer, sizeof(buffer), 1, fp);
Here's another way to do it in C++:
#include
ifstream infile;
char buffer[512];
infile.open("test.txt");
infile >> buffer;
infile.close();
Either way should read 512 bytes into buffer.
Edited by - dark chrono4 on October 21, 2000 2:32:53 PM
fread(buffer, sizeof(buffer), 1, fp);
Here's another way to do it in C++:
#include
ifstream infile;
char buffer[512];
infile.open("test.txt");
infile >> buffer;
infile.close();
Either way should read 512 bytes into buffer.
Edited by - dark chrono4 on October 21, 2000 2:32:53 PM
"I can't work like this! Where are my fuzzy dice?"
Ok, for some reason I can''t edit the post above anymore. The include should be [fstream.h].
"I can't work like this! Where are my fuzzy dice?"
"I can't work like this! Where are my fuzzy dice?"
"I can't work like this! Where are my fuzzy dice?"
I figured out the problem, i had the test.txt in the debug folder in vc++ workspace, as would have assumed it should be there since that is where the executable is, but i copied the file to the same folder as the source files, and then the damned thing worked!
but why in the heck is it not reading it from the debug folder when I run it from vc++ but when I go to the command prompt and run the program from there, it then does read it from the debug folder. Stupid visual studio.
Possibility
Edited by - Possibility on October 21, 2000 3:42:05 PM
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement