tags:

views:

152

answers:

2

I'm reading characters from the input. Once I find that I've moved to the next row, by checking that the character is a special token, I want the code to put that character back so that it is as if none of the characters on that row have been read.

How do I do that?

This is what I have so far.

 char nm;   int i=0;
 double thelow, theupp; double numbers[200];

 for(i=0;i<4;++i)
 {
     {
         char nm;
         double thelow, theupp; /*after erased, created again*/

         scanf("%c %lf %lf", &nm, &thelow, &theupp);
            for (k = 0; ; ++k) ;
               {
                   scanf("%lf", numbers[k]);
                   if(numbers[k] == '\n')
                       break;
               }
         /* calling function and sending data(nm,..) to it */
      } /*after } is seen (nm ..) is erased*/
      ;
  }



input;

D -1.5 0.5 .012 .025 .05 .1 .1 .1 .025 .012 0 0 0 .012 .025 .1 .2 .1 .05 .039 .025 .025
B 1 3 .117 .058 .029 .015 .007 .007 .007 .015 .022 .029 .036 .044 .051 .058 .066 .073 .080 .088 .095 .103
A: 

I think that the function ungetc will do what you want. Take a look at the MSDN docs for ungetc or the GNU C Programming Tutorial.

tomlogic
+2  A: 

you can call ungetc(), seek(), lseek(), fseek(), rewind(), fsetpos() (some of these are platform depdendent, they do not exist for all implementations of C, fseek() and ungetc() are C99) to move the file pointer.

ungetc() has issues. It does put back the character into the file buffer. The seek operations do not do that. Big difference.

So: 1. you can move freely back and forth in the file buffer, you don't have to call ungetc() 50 times to move the file pointer back 50 characters. Besides ungetc may fail badly after multiple calls. Check your documentation.

http://opengroup.org/onlinepubs/007908799/xsh/ungetc.html

  1. ungetc() may be affected by other operations on the file stream, and previous ungetc() calls. And it is not guaranteed reliable after multiple calls.

If you are just doing a one-off quick check with a possible ungetc call - that is perfectly fine.

For cartiage control files (where the end of line character marks a record) you should use fgets().

jim mcnamara