Thread: Ignoring white spaces and putting all copied characters on 1 line.

  1. #1
    Registered User
    Join Date
    May 2012
    Posts
    5

    Ignoring white spaces and putting all copied characters on 1 line.

    Hello. Currently my code is supposed to be copying the 21st character or the last character if it's less than 21 onto a new file. I need to ignore spaces, and it's not. In addition, I need it to place all these character copied on one line. Which I can't figure out what to do. Here's my code:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main (void){
    
        FILE* sp1;
        FILE* sp2;
        int curCh; // variable 1
        int preCh; // variable 2
        int countCh = 0; // variable 3;
        int whiteSpace = 0; // var4
        int closeStatus;
    
        printf("File copy has started\n");
        if (!(sp1 = fopen ("FILE1.DAT", "r"))){
            printf("Error opening FILES1.DAT for reading");
            return (1);
        }//if open
        if (!(sp2 = fopen ("FILES2.DAT", "w"))){
            printf("Error opening FILES2 for writing");
            return (2);
        }
    
        while ((curCh = fgetc(sp1)) != EOF){
            countCh++;
            if ( curCh == ' ')
                    whiteSpace = 0;
            if(curCh == '\n' ){
                if (countCh < 21 && whiteSpace != 0)
                    fputc (preCh, sp2);
                    fputc (' ', sp2);
                countCh = 0;
            }
    
            whiteSpace = 1;
            if ( countCh == 21 && whiteSpace != 0){
                fputc(curCh, sp2);
                fputc (' ', sp2);
    
            }
            preCh = curCh; 
        }        
        fputc ('\n', sp2);
        fclose(sp1);
        closeStatus = fclose(sp2);
        if (closeStatus == EOF)
        {
            printf("File close error.\a\n");
            return 201;
        } // if close error 
    
        printf("File successfully created\n");
        system ("pause");
        return 0;
    }]
    Last edited by code_bunny; 05-25-2012 at 06:45 PM.

  2. #2
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    Another one of these guys?

    What is it with people and being deliberately unhelpful even to themselves?

    *shrug*

    I can see from looking at your code that you haven't included most any of the changes you've been told to make in any of the forums where you've posted this problem.

    Do try to help yourself. If you try to help yourself we will be happy to help you in turn. We aren't going to do it for you.

    For example, let's see you incorporate the changes that has already been suggested for exactly this code.

    Soma

  3. #3
    Registered User
    Join Date
    May 2012
    Posts
    5
    Thanks ..........

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 05-25-2012, 09:27 PM
  2. Replies: 7
    Last Post: 10-03-2009, 10:58 PM
  3. compare function ignoring leading white space
    By mc72 in forum C Programming
    Replies: 5
    Last Post: 11-23-2008, 01:33 PM
  4. Ignoring New Line Characters
    By Spencer Wallace in forum C Programming
    Replies: 2
    Last Post: 03-27-2006, 01:23 AM
  5. Ignoring spaces...
    By alvifarooq in forum C++ Programming
    Replies: 4
    Last Post: 11-05-2004, 12:30 AM