Solved

reading file streams

Posted on 1998-07-29
3
181 Views
Last Modified: 2010-04-02
I am reading a standard text file which has been opened using
stream = fopen("MyFile.txt", "a+");
No problems there.
I then want to find a specific text string. eg:
char T[256];
char S[]="My Name";
fscanf(stream, "%s", &T);
compare = strcmp(T,S);

using fscanf however only retrieves a string up to the next whitespace.
How can I look for a "sentance" as it were like 'My Name' that includes a whitespace.
Thanks for any help.
dsch
0
Comment
Question by:dsch
3 Comments
 

Accepted Solution

by:
jufer earned 30 total points
ID: 1168947
Hi,
try to read the hole line with fgets(T,256,stream);  

The fgets function reads a string from the input stream argument and stores it in string. fgets reads characters from the current stream position to and including the first newline character, to the end of the stream, or until the number of characters read is equal to n – 1, whichever comes first. The result stored in string is appended with a null character. The newline character, if read, is included in the string.
The fscanf end at a whitespace as you saw it, I don't know another solution with fscanf!


or take the easier version with CStdioFile, CString and so....

like :
 CStdioFile stream;
 CString cstr;

 //open the file
  if( !stream.Open( "MyFile.txt", CFile::modeCreate  | CFile::modeReadWrite |
         CFile::typeText ) )
    {
       #ifdef _DEBUG
         afxDump << "Unable to open file" << "\n";
      #endif
      exit( 1 );
    }
 else {
   while (stream.ReadString(cstr) )  //read the hole file, not only one line
   {
      if (cstr.Find("MyName")!=-1)  {
       //found the String
       }
   }
}

With the CString you can also cut the leading / ending spaces with cstr.TrimLeft(); or cstr.TrimRight();  but see the helppage for more information.


Cheers
jufer
 
 

0
 
LVL 11

Expert Comment

by:alexo
ID: 1168948
fscanf() and fgets() is the C way of doing things.
CStdioFile, CString, etc. are MFC abominations.

The standard C++ solution is using iostreams.
istream::getline will retrieve a line of text..
0
 
LVL 1

Author Comment

by:dsch
ID: 1168949
Used the fgets function, this was good enough for what I needed.
Ta

0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now