Solved

reading file streams

Posted on 1998-07-29
3
189 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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.

840 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