reading formatted strings: readf("%s", &stringvar)

Tyro[17] nospam at home.com
Sun Mar 25 06:00:15 PDT 2012


I am trying to figure out the cause of my problem in the 
following post:

      
http://forum.dlang.org/thread/qfbugjkrerfboqhvjttw@forum.dlang.org

and encountered something peculiar about reading strings. 
Whenever a distinct terminator is indicated in the input format 
(ex. " %s@", @ being the terminator), readf() leaves the 
terminator on the input buffer after reading the data. If no 
terminator is specified the only way to indicate end of input is 
to use ctrl-d (ctrl-z on windows), however that causes the eof 
indicator to be set to true and the stream is marked as empty for 
all future attempts to access stdin.

void main(string[] args)
{
     string s1;
     double d;
     string s2;

     writeln("Enter a @ terminated string (multiline ok):");
     readf(" %s@", &s1);
     auto arr = s1.split();

     if (!stdin.eof()) {
         writeln("The stream is not empty.");
     } else {
         writeln("The stream is empty.");
     }
     writeln("Enter another string (terminated with 
cntrl-d|ctrl-z):");
     readf(" %s", &s2);  // No matter how many read attempts
                                 // are made after this point, 
none of them will work.
                                 // Insert \n after %s to see 
difference.

     writeln("Enter a decimal value:");
     readf(" %s", &d);
     //readf(" %s", &d);
     //readf(" %s", &d);
     //readf(" %s", &d);
     //readf(" %s", &d);

     if (!stdin.eof()) {
         writeln("The stream is not empty.");
     } else {
         writeln("The stream is empty.");
     }

     writeln("d = ", d);
     writeln("arr = ", arr);
     writeln("s = ", s2);
}

Is there no way to indicate eof without marking the stream 
(buffer?) as empty for all future uses after encountering 
cntrl-d/cntrl-z during string input? I would expect to be able to 
terminate string input with cntrl-d/cntrl-z without rendering the 
input stream inaccessible.

Thanks,
Andrew


More information about the Digitalmars-d mailing list