Thread: Adding spaces to existing file

  1. #16
    Registered User
    Join Date
    Nov 2010
    Location
    In my house
    Posts
    32
    I'm with CommonTater, two files should be used. here is some code that will read, find and replace, then remove the input file and rename the output file to the input.
    Disclaimer: i haven't tested this yet. Correct me if i'm wrong
    Code:
    #include <stdio.h>
    
    int main(int argc, char** argv) /* you don't need these args, but i like putting them in regardless */
    {
    	char line[255]; /* or however long you want */
    	FILE* in = fopen("file2.txt", "r");
    	FILE* out = fopen("_file2_.txt", "w");
    	if (in == NULL) 
    	{ 
    		printf("File access error.\n");
    		return 0;
    	}
    	while(fgets(line, sizeof line, INSTREAM) != NULL) /* if you only have 1 line, you can comment this out, but if the file contains multiple lines, leave this in */
    	{
    		for(i = 0; line[i] != NULL; i++)
    		{
    			if (line[i] == "#") /* finds "#" */
    			{
    				fprintf(out, "%c", "*";	/* writes "*" */
    			}
    			else
    			{
    				fprintf(out, "%c", line[i]); /* writes the letter that isn't "#" */
    			}
    		}
    	}
    	fclose(in); fclose(out);
    	remove("file2.txt"); /* deletes the input file */
    	rename("_file2_.txt", "file2.txt"); /* names the output file, what the input file was */
    	return 0;
    }

  2. #17
    Registered User
    Join Date
    Oct 2010
    Posts
    135
    Quote Originally Posted by nonoob View Post
    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.
    Reading and writing uses the same position? That still doesn't explain why the file doesn't change. I mean, it still should have changed (new lines, spaces, deletions, etc.) but it doesn't.

  3. #18
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Reading and writing uses the same position? That still doesn't explain why the file doesn't change. I mean, it still should have changed (new lines, spaces, deletions, etc.) but it doesn't.
    The file is not changed at all?
    I don't believe that. I compiled and run your code(post #1 code). The file is *changed*.

  4. #19
    Registered User
    Join Date
    Oct 2010
    Posts
    135
    My eyes must be deceiving me. Which part changed?

  5. #20
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Code:
    $ cat file2.txt
    this is a test file
    ust for testing only
    bye bye...
    
    $ cc rw.c
    $ ./a.out
    File processing complete.
    $ cat file2.txt
    this is a test file
    
    st for testing only
    
    ye bye...
    rw.c source code is exactly your code. copy and pasted.
    It's time to post your program output...
    Last edited by Bayint Naung; 11-27-2010 at 02:07 AM.

  6. #21
    Registered User
    Join Date
    Oct 2010
    Posts
    135
    Are you on Linux? Perhaps it's a compiler problem.

    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)
    	{
            printf("%c", ch);
    	}
    
    	rewind(f1);
    
    	while((ch = fgetc(f1)) != EOF)
    	{
    		if (ch == '\n')
    		fputc('\n', f1);
    	}
    
    	printf("\n\nFile processing complete.\n\n");
    
    	rewind(f1);
    
    	while((ch = fgetc(f1)) != EOF)
    	{
            printf("%c", ch);
    	}
    
        if (fclose(f1) != 0)
        printf("File close error.\n");
    
    	return 0;
    }
    The output:

    Code:
    aaaaa
    aaaaa
    aaaaa
    
    File processing complete.
    
    aaaaa
    aaaaa
    aaaaa
    Process returned 0 (0x0)   execution time : 0.011 s
    Press any key to continue.

  7. #22
    Registered User
    Join Date
    Oct 2010
    Posts
    135
    When I switch to the two-file system, the algorithm works correctly, so I suppose I'll do just that, now that I know how to remove and rename files. Thanks, guys.

  8. #23
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by nonoob View Post
    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.
    It doesn't.

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