Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

reading file streams

Posted on 1998-07-29
3
Medium Priority
?
197 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 90 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Article by: evilrix
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
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 learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses
Course of the Month12 days, 21 hours left to enroll

971 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