I need help with reading text files
I''m trying to create a program that will read a txt file from the hard drive,and run a switch statement based on the string of text it finds in there. How would I accomplish this with the windows api? or any other method. Thankyou for your time.
- Travis
switch statements only work with integer equality. Use if..else f..else if..else blocks.
Don''t listen to me. I''ve had too much coffee.
Don''t listen to me. I''ve had too much coffee.
ok then, at any rate I still need to learn how to get the strings into the program with a read function or something
- Travis
- Travis
You don''t have to use the windows API, use ifstream.h its standard, and fairly easy.
ifstream inputFile;
inputFile.open("Text.txt";
char * switchString << inputFile;
....
ifstream inputFile;
inputFile.open("Text.txt";
char * switchString << inputFile;
....
do you have any clear documentation on ifstream?
In my AP computer science book it says things about it but really dosen''t give us much info, I need to know how to look up the strings in the file with it, I''ll try playing with it, thankyou very much.
- Travis
In my AP computer science book it says things about it but really dosen''t give us much info, I need to know how to look up the strings in the file with it, I''ll try playing with it, thankyou very much.
- Travis
first of all, I don''t think switch() can handle strings.
First of all, include , ,
[script]
// create file pointer, open file as "r"ead.
FILE * FP = fopen("YourFileNameHere.txt", "r");
// create string
char CommandReadFromFile[64] = " ";
// read string
// (Anyone correct me if I''m wrong)
freads(CommandReadFromFile, FP);
// compare string to predefined ones
// use strcmp(A, B), returns 0 if A = B
if(0 == strcmp("First String", CommandReadFromFile))
{
//The file contains "First String"
}
else
//Not yet, check other values...
[/script]
Methinks this works on any dos-based API...
Extatica is coming soon!
Check it out on:
http://www.extatica.com02.com
Nexeruza Studios:
http://nexeruza.ionichost.com/home.html
First of all, include , ,
[script]
// create file pointer, open file as "r"ead.
FILE * FP = fopen("YourFileNameHere.txt", "r");
// create string
char CommandReadFromFile[64] = " ";
// read string
// (Anyone correct me if I''m wrong)
freads(CommandReadFromFile, FP);
// compare string to predefined ones
// use strcmp(A, B), returns 0 if A = B
if(0 == strcmp("First String", CommandReadFromFile))
{
//The file contains "First String"
}
else
//Not yet, check other values...
[/script]
Methinks this works on any dos-based API...
Extatica is coming soon!
Check it out on:
http://www.extatica.com02.com
Nexeruza Studios:
http://nexeruza.ionichost.com/home.html
Produced the following errors:
Compiling...
main.cpp
C:\Documents and Settings\Owner\Desktop\DirectX\main.cpp(18) : error C2501: ''freads'' : missing storage-class or type specifiers
C:\Documents and Settings\Owner\Desktop\DirectX\main.cpp(18) : error C2078: too many initializers
C:\Documents and Settings\Owner\Desktop\DirectX\main.cpp(18) : error C2440: ''initializing'' : cannot convert from ''struct _iobuf *'' to ''int''
This conversion requires a reinterpret_cast, a C-style cast or function-style cast
C:\Documents and Settings\Owner\Desktop\DirectX\main.cpp(22) : error C2143: syntax error : missing '';'' before ''if''
C:\Documents and Settings\Owner\Desktop\DirectX\main.cpp(23) : error C2143: syntax error : missing '';'' before ''{''
C:\Documents and Settings\Owner\Desktop\DirectX\main.cpp(23) : error C2447: missing function header (old-style formal list?)
C:\Documents and Settings\Owner\Desktop\DirectX\main.cpp(26) : error C2143: syntax error : missing '';'' before ''else''
Error executing cl.exe.
- Travis
Compiling...
main.cpp
C:\Documents and Settings\Owner\Desktop\DirectX\main.cpp(18) : error C2501: ''freads'' : missing storage-class or type specifiers
C:\Documents and Settings\Owner\Desktop\DirectX\main.cpp(18) : error C2078: too many initializers
C:\Documents and Settings\Owner\Desktop\DirectX\main.cpp(18) : error C2440: ''initializing'' : cannot convert from ''struct _iobuf *'' to ''int''
This conversion requires a reinterpret_cast, a C-style cast or function-style cast
C:\Documents and Settings\Owner\Desktop\DirectX\main.cpp(22) : error C2143: syntax error : missing '';'' before ''if''
C:\Documents and Settings\Owner\Desktop\DirectX\main.cpp(23) : error C2143: syntax error : missing '';'' before ''{''
C:\Documents and Settings\Owner\Desktop\DirectX\main.cpp(23) : error C2447: missing function header (old-style formal list?)
C:\Documents and Settings\Owner\Desktop\DirectX\main.cpp(26) : error C2143: syntax error : missing '';'' before ''else''
Error executing cl.exe.
- Travis
A reference to the iostream library:
http://www.cplusplus.com/ref/iostream/
By the way, the correct (or at least standard) header file to #include is <fstream>, not <fstream.h> or <ifstream.h> as mentioned in some other posts.
If you want to use the fread() (not freads() btw) family of functions, you need to include stdio.h, but if you're using C++ I strongly suggest using IOStreams for text input.
[edited by - Stary on November 7, 2002 5:00:26 PM]
http://www.cplusplus.com/ref/iostream/
By the way, the correct (or at least standard) header file to #include is <fstream>, not <fstream.h> or <ifstream.h> as mentioned in some other posts.
If you want to use the fread() (not freads() btw) family of functions, you need to include stdio.h, but if you're using C++ I strongly suggest using IOStreams for text input.
[edited by - Stary on November 7, 2002 5:00:26 PM]
quote:Original post by ToohrVyk
first of all, I don''t think switch() can handle strings.
First of all, include , ,
// create file pointer, open file as "r"ead.FILE * FP = fopen("YourFileNameHere.txt", "r");// create stringchar CommandReadFromFile[64] = " ";// read string// (Anyone correct me if I''m wrong)freads(CommandReadFromFile, FP);
freads doesn''t exist
it''s
fread(void* data,int size,int count,FILE* fptr)
// size is the size of one one element
to read this char array you need to call it like this
fread(CommandReadFromFile,sizeof(char),64,FP);
but that''s not the only function to read data from a file(google fgets,fscanf ... and you will find what you need)
quote:
// compare string to predefined ones// use strcmp(A, B), returns 0 if A = Bif(0 == strcmp("First String", CommandReadFromFile)){//The file contains "First String"}else//Not yet, check other values...
Methinks this works on any dos-based API...
this works on everything (with a C/C++ compiler)
quote:
Extatica is coming soon!
Check it out on:
http://www.extatica.com02.com
Nexeruza Studios:
http://nexeruza.ionichost.com/home.html
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement