Solved

reading file streams

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
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 viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

867 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

19 Experts available now in Live!

Get 1:1 Help Now