Thread: Adding spaces to existing file

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    135

    Adding spaces to existing file

    Can anyone help? For some reason this code won't work. The file remains unchanged.

    Code:
    #include <stdio.h>
    
    int main ()
    {
    	FILE *f1;
    	int ch;
    
    	f1 = fopen("file2.txt", "r+");
    
    	if (f1 == NULL)
    	{
    		printf("File access error.\n");
    		return;
    	}
    
    	while((ch = fgetc(f1)) != EOF)
    	{
    		if (ch == '\n')
    		fputc('\n', f1);
    	}
    
    	printf("File processing complete.\n");
    
        if (fclose(f1) != 0)
        printf("File close error.\n");
    
    	return 0;
    }

  2. #2
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    This kind of question is asked not very long ago.
    Next time, make sure that you search the forum before posting.
    Question 19.14
    Modifying a file.

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    135
    I'm sorry, but those links don't help. I know the algorithm, but the reason the code won't work is most likely due to careless mistakes.

  4. #4
    Novice
    Join Date
    Jul 2009
    Posts
    568
    Code:
    while((ch = fgetc(f1)) != EOF)
    {
        // replace all '\n' with '#'
        if (ch == '\n')
            //fputc('\n', f1);
            unget('#', f1);
    }
    Disclaimer: This post shows my ignorance at the time of its making. I claim ownership of but not responsibility for all errors in it. Reference at your own peril.

  5. #5
    Novice
    Join Date
    Jul 2009
    Posts
    568
    Never mind. That won't work.
    Disclaimer: This post shows my ignorance at the time of its making. I claim ownership of but not responsibility for all errors in it. Reference at your own peril.

  6. #6
    Registered User
    Join Date
    Oct 2010
    Posts
    135
    Is that C++? I'm using C, so unget will not work.

  7. #7
    Novice
    Join Date
    Jul 2009
    Posts
    568
    It's actually ungetc(). I made a typo.
    Disclaimer: This post shows my ignorance at the time of its making. I claim ownership of but not responsibility for all errors in it. Reference at your own peril.

  8. #8
    Registered User
    Join Date
    Oct 2010
    Posts
    135
    What were you trying to do?

  9. #9
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    This is a task where you need to open two files... one as your input file which is read sequentially and the other as an output file that is written sequentially. The problem being that you can't insert text into a sequential file without overwriting some part of it's existing content.

  10. #10
    Registered User
    Join Date
    Oct 2010
    Posts
    135
    I thought that's what r+ was supposed to do? I wanted the program to insert the spaces while going through the file sequentially. Even though it's supposed to modify the file in some way, I fail to see any change at all. Or is there no other choice but to open up another file for writing?

    EDIT: I just checked and the program indeed works if I choose to output a second file.
    Last edited by 843; 11-26-2010 at 10:17 AM.

  11. #11
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    The thing is that when you have a sequential file, there is no way to push characters back to make room for your insertion... you end up overwriting it's contents with your insertions.

    Conceptually...
    Code:
    aaaaaaaaaaaaaabzzzbbbbbbbbbbbbbbbbbbbcccccccccccccccddddddddddd
                   ^ file pointer here... insert ///
    
    aaaaaaaaaaaaaab///bbbbbbbbbbbbbbbbbbbcccccccccccccccddddddddddd
                      ^ file pointer now here.
    Where'd your zzz go?

    The only way around this is to open a second "output" file....
    To clean things up you can delete the original and rename the new one.

  12. #12
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    No need for a separate output file. I would use fread and fwrite... but anyways, you’d need to do an fseek to backup one byte before overwriting with a substitute character. Although your first post indicates you are substituting with the SAME character.

  13. #13
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by nonoob View Post
    No need for a separate output file. I would use fread and fwrite... but anyways, you’d need to do an fseek to backup one byte before overwriting with a substitute character. Although your first post indicates you are substituting with the SAME character.
    I'd still go with the two file system... that way if the program crashes, the input file isn't trashed.

  14. #14
    Registered User
    Join Date
    Oct 2010
    Posts
    135
    I did try using fseek, in addition to printing an extra newline, but both did not manage to alter the file at all. Here's the section I changed:

    Code:
    	while((ch = fgetc(f1)) != EOF)
    	{
    	    fputc(ch, f1);
    		if (ch == '\n')
    		{
    		    fseek(f1, -1L, SEEK_CUR);
                        fputc('\n', f1);
                        fputc('\n', f1);
    		}
    	}

  15. #15
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Oh wow, man. Reading a char, then immediately writing it... I have no idea if the system keeps separate file positions for read vs. write. I doubt it. So you may be duplicating a character into the NEXT position. Then you're backing up one, writing it again (why?), then writing a subsequent one. I think you need to relax and figure out what you're trying to accomplish.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  2. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  3. Need Help Fixing My C Program. Deals with File I/O
    By Matus in forum C Programming
    Replies: 7
    Last Post: 04-29-2008, 07:51 PM
  4. Simple File encryption
    By caroundw5h in forum C Programming
    Replies: 2
    Last Post: 10-13-2004, 10:51 PM
  5. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM