Thread: Can I rewind a string like I can a file?

  1. #1
    Third Eye Babkockdood's Avatar
    Join Date
    Apr 2010
    Posts
    352

    Can I rewind a string like I can a file?

    Think about this. printf, sprintf, and fprintf all work on streams, right? Just like scanf, sscanf, and fscanf work on streams?

    When I use a function like rewind() or fseek() with SEEK_END, it changes where the stream position is, it changes where fprintf begins outputting, and where sscanf begins inputting.

    So, since strings are streams, like files are, can I rewind a string like I can a file?

    I guess I could write my string to a file and then use fscanf as a last resort, but I'm just wondering if there's an easier way. How would I go about writing a function like this myself?
    Quote Originally Posted by The Jargon File
    Microsoft Windows - A thirty-two bit extension and graphical shell to a sixteen-bit patch to an eight-bit operating system originally coded for a four-bit microprocessor which was written by a two-bit company that can't stand one bit of competition.

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Give it a go and post some code. It would be pointless to write it to a file, so don't do that!

    BTW, C++ has a stringstream class.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    88
    Quote Originally Posted by Babkockdood View Post
    Think about this. printf, sprintf, and fprintf all work on streams, right? Just like scanf, sscanf, and fscanf work on streams?

    When I use a function like rewind() or fseek() with SEEK_END, it changes where the stream position is, it changes where fprintf begins outputting, and where sscanf begins inputting.

    So, since strings are streams, like files are, can I rewind a string like I can a file?

    I guess I could write my string to a file and then use fscanf as a last resort, but I'm just wondering if there's an easier way. How would I go about writing a function like this myself?
    To rewind a string all you have to do is put it back some where between where it is and the start of the string;

    Code:
    char buff[1024]; 
    char *b = buff;
    int      x;
    x = sprintf(b,"hello world %ld\n", (long)time(NULL));
    if(x <= 0)
      return;
    b += x ;
    x = sprintf(b,"hello world %d\n", b - buff);
    if(x <= 0)
      return;
    b += x;
    /*** to rewind the string pointed to by b  ***/
    b = buff;
    x = sprintf(b,"hello world %d\n", b - buff);
    if(x <= 0)
      return;
    /** will print "hello world 0\n" ***/
    printf("%s\n", buff);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 10
    Last Post: 12-09-2011, 02:52 AM
  2. how to find coomon data in 3 files without rewind..
    By transgalactic2 in forum C Programming
    Replies: 31
    Last Post: 03-29-2009, 03:23 PM
  3. rewind(stdin); ? Right or Wrong?
    By Antigloss in forum C Programming
    Replies: 3
    Last Post: 10-26-2006, 12:53 PM
  4. Problem comparing string from text file with string constant
    By XenoCodex Admin in forum C++ Programming
    Replies: 3
    Last Post: 07-25-2002, 10:17 AM
  5. forgot how to rewind in C++
    By ss3x in forum C++ Programming
    Replies: 2
    Last Post: 04-06-2002, 07:55 PM