Archived

This topic is now archived and is closed to further replies.

TThom903

I need help with reading text files

Recommended Posts

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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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;

....

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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 string

char 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 = B

if(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


Share this post


Link to post
Share on other sites