Thread: Beginner: Copy from 1 file to another, excluding spaces.

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

    Beginner: Copy from 1 file to another, excluding spaces.

    Hi,
    I'm trying to write a program that will ask the user for a file which the wish to copy the contents of and then copy it to another file whilst stripping out the spaces.

    but initially i cant seem to be able to get it to copy at all, my window crashes on entering the output file name. can anyone suggest where im going wrong?

    Code:
    #include<stdio.h>
    
     int main(void)
     {
    	/* Declare Variables */ 
    	char inputFile[20];
    	char outputFile[20];
    	char copy[256];
    	
    
    	/* Ask user to input file name(including extension)*/
    	printf("Please enter the file name you want to copy: ");
    	scanf("%s", inputFile);
    
    	/* Ask user to output file name(including extension)*/
    	printf("Please enter the file name you want to output: ");
    	scanf("%s", outputFile);
    
    	/* Opens file on a read txt only basis */
    	FILE *file_pointer=NULL;
    	file_pointer=fopen(inputFile, "rt");
    
    	/* Opens file on a write basis */
    	FILE *file_pointer2=NULL;
    	file_pointer2=fopen(outputFile, "w");
    
    	/* Copies the string into a buffer named 'copy' */
    	 while (fgets(copy,256,file_pointer));
    	{
    		fputs(copy,file_pointer2);
    	}
    	
    	/* A declaration of success */
    	printf("File %s has been successfully copied to file %s", inputFile, outputFile);
    
     }
    Kind Regards
    Ryan
    Last edited by RyanLeonard; 10-27-2010 at 09:08 AM.

  2. #2
    Registered User
    Join Date
    Oct 2010
    Posts
    42
    A thousand apologies, it actually does what i wanted it to do, but now i just need to find how filter the spaces. Any suggestions? should i use an fgetc() funtion instead of the fgets()?

  3. #3
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    How many characters did you enter? max should only be 19...
    Check fopen() return value also,

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    If you're stripping all spaces, a simpler method would be to process the file character by character. Then you can simply see if the current character is a space and refrain from writing it if so:
    Code:
    int ch;
    
    while ((ch = getc(in)) != EOF) {
        if (ch == ' ')
            putc(ch, out);
    }
    The way you're doing it now would require trimming spaces from a string, which isn't terribly difficult, but quite a bit harder than the above if you're a beginner.
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    Oct 2010
    Posts
    42
    Waow, Thanks Prelude, wasnt sure how to use an if statement for a space. the only this i had to change was the if (ch == ' ') to if (ch != ' ') but that was probably a test i passed, thanks so much again. Hope someday I can help people with their programming.

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >the only this i had to change was the if (ch == ' ') to if (ch != ' ')
    Ha! My bad. I originally used isspace and figured that probably would be too much, then replaced it with the wrong manual test. Gotta pay more attention to what I'm doing...
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File Copy problem
    By reenact12321 in forum C Programming
    Replies: 2
    Last Post: 02-18-2010, 03:08 AM
  2. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  3. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  4. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  5. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM