Thread: inserting characters mid-stream?

  1. #1
    Codus Conjectus spongefreddie's Avatar
    Join Date
    Sep 2010

    inserting characters mid-stream?

    The question mark in the title is because I'm not even sure if I'm on the right track. Anyway, here's something that's been torturing me all night with no relief...

    I completed an exercise in my programming book where I was supposed to alter a program displayed in the chapter so besides just appending more words each time the program is run, it also displays sequential numbers for each new word, starting with the number 1.

    The code below is my completed exercise, but then (apart from the book) I wondered if I could successfully accomplish the same goal again, but this time actually *write* the numerical values to the file (instead of just sending them to stdout).

    Can anyone think of suggestions as to how I might accomplish this? I've spent hours fiddling with string functions, ctype functions, fseek, etc., with no joy at all.

    #include <stdio.h>
    #include <stdlib.h>
    #define MAX 200
    int main(void)
    	int num = 1;
    	FILE * fp;
    	char words[MAX];
    	if ((fp = fopen("wordy.txt", "a+")) == NULL)
    		fprintf(stdin, "Can't open \"wordy.txt\" file.\n");
    	puts("Enter words to add to the file; press the Enter");
    	puts("key at the beginning of a line to terminate.");
    	while (gets(words) != NULL && words[0] != '\0')
    		fprintf(fp, "%s ", words);
    	puts("File contents:");
    	while (fscanf(fp, "%s", words) == 1)
    		printf("%d ", num);	// these two lines were all I had
    		num++;			// to add to complete the exercise
    	if (fclose(fp) != 0)							
    		fprintf(stderr, "Error closing file.\n");
    	return 0;
    Running the program as is will get output like this:

    1 hello
    2 my
    3 name
    4 is
    5 freddie

    Running it again, with new input will append like this:

    1 hello
    2 my
    3 name
    4 is
    5 freddie
    6 what
    7 is
    8 your
    9 name?

    In this scenario, after running the program these two times, if you open the file 'wordy.txt', you get (no numbers, of course):

    hello my name is freddie what is your name?

    I would have just moved on to the next exercise, but I'm thinking that being able to insert data anywhere in a file is probably an important thing to know.

    Thank you in advance for any helpful ideas.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    You probably would not want to write those numbers to the original file since that would then screw up the next use of the program, i.e., those numbers would then be treated as new words.

    What you might want to do is to write to a new file instead of standard output. This can be done rather trivially by using output redirection on the command line, or you could modify the program to open a new file and write to it.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Breaking a binary stream
    By luckyvictor in forum C++ Programming
    Replies: 2
    Last Post: 12-09-2009, 05:02 PM
  2. ascii characters video displaying on tv screen
    By deian in forum C Programming
    Replies: 6
    Last Post: 10-12-2004, 09:46 PM
  3. Characters in a txt file.
    By tay_highfield in forum C Programming
    Replies: 3
    Last Post: 01-31-2003, 09:19 AM
  4. Replies: 9
    Last Post: 07-01-2002, 07:50 AM
  5. 2 questions - twice the fun!
    By face_master in forum C++ Programming
    Replies: 8
    Last Post: 12-22-2001, 03:34 PM