Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help: Need to get string from data file

Status
Not open for further replies.

Insider1984

Technical User
Feb 15, 2002
132
0
0
US
Basically I have a data file that I want to analize.

Each line is a string.

What I know?
============
I know how to input a string from keyboard and use it once it is inside my main program.

I know how to input data char int floats etc from data files. (very very basic)



What do I think?
================
I think I need to use some sort of Char array buffer

for example:

while getchar() isn't Null
store char from file at end of char array buffer
when getchar == null
string named buffer gets analized and data is then send to file

What I don't know:
==================
how to tell when it's end of file (how do I look for null inside a char array while loop)

the actual code for this! =-)


Thanks for the help, I'm new to the forum and I hope to hear something back. I know it may be a simple problem but hopefully someone can get me going in the right direction.

 
Hi,

Ok, you don't use getchar() to read a string from a file. You need to use fgets()...looks like this,

char str[401];
while (fgets(str, 400, fpointer) != '\0')
...statments

(you can use dynamic memory alloctaion if you'd like instead of specifying 401 like that.)

Since you said each line is a string, this will read a string (terminated by '\0') from the file until end of file and store it in str[]. You don't need to search for the NULL in the string you read in because it should already be there. Be careful though, fgets() puts a '\n' (newline) in the string it reads in.

I'm not sure what your program needs to do exactly. You said something about putting the data back into a file. You would just need to do the processing on str[] that you want and then write to a file with fputs().

Hope that helps.

-Tyler
 
Thanks for the help. I actually decided to use MFC Visual Studio Libs to speed up my program. I quickly used a Find command and converted the strings to Cstrings.

something like this:

Cstring newstring = buffer; //buffer is a line from the file
Cstring search = "Test";

//Then I use the find command

newstring.Find(search, 0); //(part of Cstring)
 
You have to use functions feof() and ferror() in coherence to detect EOF correctly. Also after every file I/O function call is made , check for return value from that function. If it is NULL there may be error. You can use fread() and fwrite() if the file is a binary data file.
sathish babu
babusats@rediffmail.com

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top