Solved

reading file streams

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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Would like to move button in a function 3 77
FMX enumerated colours 2 100
thread-safe code in c++ 2 102
gdb doesn't stop on breakpoint 2 68
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
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 technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

777 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